Class: Kanal::Plugins::UserSystem::UserSystemPlugin

Inherits:
Core::Plugins::Plugin
  • Object
show all
Includes:
Helpers, Models
Defined in:
lib/kanal/plugins/user_system/user_system_plugin.rb

Overview

User system plugin serves as variant of user authentication, registration and removal system

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUserSystemPlugin

Returns a new instance of UserSystemPlugin.



19
20
21
22
# File 'lib/kanal/plugins/user_system/user_system_plugin.rb', line 19

def initialize
  super
  @auto_create = AutoCreateEnabler.new
end

Instance Attribute Details

#auto_createObject (readonly)

Returns the value of attribute auto_create.



17
18
19
# File 'lib/kanal/plugins/user_system/user_system_plugin.rb', line 17

def auto_create
  @auto_create
end

Instance Method Details

#nameObject



24
25
26
# File 'lib/kanal/plugins/user_system/user_system_plugin.rb', line 24

def name
  :user_system
end

#setup(core) ⇒ void

This method returns an undefined value.

Returns <description>.

Parameters:

  • core (Kanal::Core::Core)

    <description>



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kanal/plugins/user_system/user_system_plugin.rb', line 33

def setup(core)
  unless core.plugin_registered? :active_record
    raise "Cannot setup UserSystem without :active_record plugin installed!"
  end

  active_record_plugin = core.get_plugin :active_record

  active_record_plugin.add_migrations_directory File.join(__dir__, "migrations")

  setup_user_storage core
  setup_user_state core

  AutoCreator.enable_telegram(core) if @auto_create.telegram_enabled
end

#setup_user_state(core) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kanal/plugins/user_system/user_system_plugin.rb', line 48

def setup_user_state(core)
  user_exists = ->(i) { i.user.is_a? KanalUser }

  core.add_condition_pack :user_state do
    add_condition :is do
      with_argument

      met? do |input, core, argument|
        return false unless user_exists.call input

        check_for_state = argument

        check_for_state = check_for_state.to_s if check_for_state.is_a? Symbol

        current_state = input.user.state

        current_state == check_for_state
      end
    end

    add_condition :not_set do
      met? do |input, core, argument|
        return false unless user_exists.call input

        input.user.state.nil?
      end
    end
  end
end

#setup_user_storage(core) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/kanal/plugins/user_system/user_system_plugin.rb', line 78

def setup_user_storage(core)
  core.register_input_parameter :user

  user_exists = ->(i) { i.user.is_a? KanalUser }

  core.add_condition_pack :user do
    add_condition :exists do
      met? do |input, _, _|
        user_exists.call input
      end
    end

    add_condition :has_property_value do
      with_argument

      met? do |input, c, argument|
        return false unless user_exists.call input

        property_name = argument[0]
        value = argument[1]

        property = input.user.get_property_by_name property_name

        return false if property.nil?

        property_value = property.value

        return property_value == value
      end
    end

    add_condition :phone_one_of do
      with_argument

      met? do |input, _, argument|
        return false unless user_exists.call input

        argument.include? input.user.phone
      end
    end

    add_condition :email_one_of do
      with_argument

      met? do |input, _, argument|
        return false unless user_exists.call input

        argument.include? input.user.email
      end
    end

    add_condition :username_one_of do
      with_argument

      met? do |input, _, argument|
        return false unless user_exists.call input

        return argument.include? input.user.username
      end
    end
  end
end