Class: Anoubis::Tenant::User
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- Core::ApplicationRecord
- ApplicationRecord
- Anoubis::Tenant::User
- Defined in:
- app/models/anoubis/tenant/user.rb
Overview
Main User model. Stores inforation about all users that can access to the portal.
Constant Summary collapse
- VALID_EMAIL_REGEX =
Email must be valid email address
/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
Instance Attribute Summary collapse
-
#auth_key ⇒ Integer
User’s session timeout.
-
#email ⇒ String
User’s email.
-
#groups_access ⇒ Object
Array of user access group.
-
#login ⇒ String
User’s full login with Tenant#ident suffix.
-
#name ⇒ String
User’s name.
-
#patronymic ⇒ String
User’s patronymic.
-
#status ⇒ 'enabled', 'disabled'
The status of menu element.
-
#surname ⇒ String
User’s surname.
-
#tenant ⇒ Tenant
The tenant that owns this user.
-
#uuid ⇒ String
UUID representations in string format.
-
#uuid_bin ⇒ Blob
User’s binary representation of UUID.
Attributes inherited from Core::ApplicationRecord
#can_delete, #can_edit, #can_new, #created_at, #current_user, #need_refresh, #redis, #updated_at
Instance Method Summary collapse
-
#after_destroy_anubis_user ⇒ Object
Is called after user was deleted from database.
-
#as_json(options = {}) ⇒ Object
Attach groups to user json outputs.
-
#before_destroy_anubis_user ⇒ Object
Is called before delete user from database.
-
#before_save_anubis_user ⇒ Object
Is called before user will be stored in database.
-
#before_validation_anubis_user_on_create ⇒ Object
Is called before validation when new user is being created.
-
#before_validation_anubis_user_on_update ⇒ Object
Is called before validation when user is being updated.
- #email_unique ⇒ Object
-
#get_groups_access ⇒ Object
Generate user access groups.
- #name_presence ⇒ Object
-
#password_changed? ⇒ Boolean
Checks if password has been changed.
-
#sys_title ⇒ String
Returns user model system title.
-
#to_json(options = {}) ⇒ Object
Excludes password from json output and uuid_bin from.
-
#validate_anubis_user(is_new) ⇒ Object
Validates users element.
Methods inherited from ApplicationRecord
#before_create_tenant_anoubis_model, #before_update_tenant_anoubis_model, get_where
Methods inherited from Core::ApplicationRecord
#after_initialize_core_anubis_model, #can_destroy?, #current_locale, #current_locale=, #default_locale, #get_locale, #get_locale_field, get_where, #is_field_localized, #new_uuid, redis, #redis_prefix, redis_prefix, #set_locale_field
Instance Attribute Details
#auth_key ⇒ Integer
Returns user’s session timeout. Default vaule is 3600.
46 |
# File 'app/models/anoubis/tenant/user.rb', line 46 validates :auth_key, length: { maximum: 32 } |
#email ⇒ String
Returns user’s email.
26 |
# File 'app/models/anoubis/tenant/user.rb', line 26 validates :email, presence: true, length: { maximum: 50 }, format: { with: VALID_EMAIL_REGEX } |
#groups_access ⇒ Object
Array of user access group
69 70 71 |
# File 'app/models/anoubis/tenant/user.rb', line 69 def groups_access @groups_access end |
#login ⇒ String
Returns user’s full login with Tenant#ident suffix.
10 |
# File 'app/models/anoubis/tenant/user.rb', line 10 has_secure_password |
#name ⇒ String
Returns user’s name.
31 |
# File 'app/models/anoubis/tenant/user.rb', line 31 validates :name, length: { maximum: 100 } |
#patronymic ⇒ String
Returns user’s patronymic.
|
# File 'app/models/anoubis/tenant/user.rb', line 4
|
#status ⇒ 'enabled', 'disabled'
Returns the status of menu element.
-
‘enabled’ — user is enabled and can login to system.
-
‘disabled’ — user is disabled and can’t login to system.
-
‘pending’ — user isn’t confirmed.
61 |
# File 'app/models/anoubis/tenant/user.rb', line 61 enum status: {enabled: 0, disabled: 1, pending: 2} |
#surname ⇒ String
Returns user’s surname.
36 |
# File 'app/models/anoubis/tenant/user.rb', line 36 validates :surname, length: { maximum: 100 } |
#tenant ⇒ Tenant
Returns the tenant that owns this user.
73 |
# File 'app/models/anoubis/tenant/user.rb', line 73 belongs_to :tenant, class_name: 'Anoubis::Tenant::Tenant' |
#uuid ⇒ String
UUID representations in string format
65 66 67 |
# File 'app/models/anoubis/tenant/user.rb', line 65 def uuid @uuid end |
#uuid_bin ⇒ Blob
Returns user’s binary representation of UUID.
54 |
# File 'app/models/anoubis/tenant/user.rb', line 54 validates :uuid_bin, uniqueness: { case_sensitive: false, scope: [:tenant_id] } |
Instance Method Details
#after_destroy_anubis_user ⇒ Object
Is called after user was deleted from database. Also deletes all cache for this user from Redis database.
153 154 155 156 157 158 159 160 |
# File 'app/models/anoubis/tenant/user.rb', line 153 def after_destroy_anubis_user if self.redis self.redis.del self.uuid self.redis.keys(self.uuid+'_*').each do |data| self.redis.del data end end end |
#as_json(options = {}) ⇒ Object
Attach groups to user json outputs
193 194 195 196 197 198 199 200 |
# File 'app/models/anoubis/tenant/user.rb', line 193 def as_json(={}) h = super() h[:groups_access] = [] self.user_groups.each do |ug| h[:groups_access].push ug.group.full_ident end h end |
#before_destroy_anubis_user ⇒ Object
Is called before delete user from database. Destroys all access groups for this user
142 143 144 145 146 147 148 149 |
# File 'app/models/anoubis/tenant/user.rb', line 142 def before_destroy_anubis_user if self.id == 1 errors.add(:base, I18n.t('users.errors.cant_destroy_tenant_admin')) throw(:abort, __method__) end Anoubis::Tenant::UserGroup.where(user_id: self.id).delete_all end |
#before_save_anubis_user ⇒ Object
126 127 128 129 130 131 |
# File 'app/models/anoubis/tenant/user.rb', line 126 def before_save_anubis_user self.timezone = 'GMT' if !self.timezone self.email = self.email.downcase self.login = self.email+'.'+self.tenant.ident self.redis.del(self.redis_prefix + 'user:' + self.uuid) if self.redis end |
#before_validation_anubis_user_on_create ⇒ Object
Is called before validation when new user is being created. Checks user parameters before create new user. Generates new UUID. Sets default system parameters for first user.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/anoubis/tenant/user.rb', line 81 def before_validation_anubis_user_on_create self.uuid = self.new_uuid if self.id if self.id == 1 self.password = 'admin' self.password_confirmation = 'admin' self.status = 0 self.tenant_id = 1 self.timeout = 3600 return true end end validate_anubis_user true end |
#before_validation_anubis_user_on_update ⇒ Object
Is called before validation when user is being updated. Checks user parameters before create new user. Generates new UUID. Sets default system parameters for first user.
99 100 101 |
# File 'app/models/anoubis/tenant/user.rb', line 99 def before_validation_anubis_user_on_update validate_anubis_user false end |
#email_unique ⇒ Object
222 223 224 |
# File 'app/models/anoubis/tenant/user.rb', line 222 def email_unique true end |
#get_groups_access ⇒ Object
Generate user access groups
210 211 212 213 214 215 216 |
# File 'app/models/anoubis/tenant/user.rb', line 210 def get_groups_access h = [] self.user_groups.each do |ug| h[:groups_access].push ug.group.full_ident end h end |
#name_presence ⇒ Object
218 219 220 |
# File 'app/models/anoubis/tenant/user.rb', line 218 def name_presence true end |
#password_changed? ⇒ Boolean
Checks if password has been changed
136 137 138 |
# File 'app/models/anoubis/tenant/user.rb', line 136 def password_changed? !password.blank? end |
#sys_title ⇒ String
Returns user model system title
187 188 189 |
# File 'app/models/anoubis/tenant/user.rb', line 187 def sys_title self.title end |
#to_json(options = {}) ⇒ Object
Excludes password from json output and uuid_bin from
179 180 181 182 |
# File 'app/models/anoubis/tenant/user.rb', line 179 def to_json(={}) [:except] ||= [:password_digest, :uuid_bin] super() end |
#validate_anubis_user(is_new) ⇒ Object
Validates users element. Sets default missing parameters. Prevents changing tenant for existing user.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/models/anoubis/tenant/user.rb', line 106 def validate_anubis_user(is_new) self.locale = 'ru-RU' if !self.locale self.timeout = 3600 if !self.timeout self.tenant_id = self.tenant_id_was if !is_new # Can't change tenant for existing user self.status = 0 if self.id == 1 # Can't disable Main Administrator if !password.blank? if password != password_confirmation errors.add(:password, I18n.t('users.errors.different_passwords')) errors.add(:password_confirmation, I18n.t('users.errors.different_passwords')) return false end end return true end |