Class: Anoubis::Tenant::User

Inherits:
ApplicationRecord show all
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

Attributes inherited from Core::ApplicationRecord

#can_delete, #can_edit, #can_new, #created_at, #current_user, #need_refresh, #redis, #updated_at

Instance Method Summary collapse

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_keyInteger

Returns user’s session timeout. Default vaule is 3600.

Returns:

  • (Integer)

    user’s session timeout. Default vaule is 3600



46
# File 'app/models/anoubis/tenant/user.rb', line 46

validates :auth_key, length: { maximum: 32 }

#emailString

Returns user’s email.

Returns:

  • (String)

    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_accessObject

Array of user access group



69
70
71
# File 'app/models/anoubis/tenant/user.rb', line 69

def groups_access
  @groups_access
end

#loginString

Returns user’s full login with Tenant#ident suffix.

Returns:

  • (String)

    user’s full login with Tenant#ident suffix.



10
# File 'app/models/anoubis/tenant/user.rb', line 10

has_secure_password

#nameString

Returns user’s name.

Returns:

  • (String)

    user’s name



31
# File 'app/models/anoubis/tenant/user.rb', line 31

validates :name, length: { maximum: 100 }

#patronymicString

Returns user’s patronymic.

Returns:

  • (String)

    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.

Returns:

  • ('enabled', 'disabled')

    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}

#surnameString

Returns user’s surname.

Returns:

  • (String)

    user’s surname



36
# File 'app/models/anoubis/tenant/user.rb', line 36

validates :surname, length: { maximum: 100 }

#tenantTenant

Returns the tenant that owns this user.

Returns:

  • (Tenant)

    the tenant that owns this user.



73
# File 'app/models/anoubis/tenant/user.rb', line 73

belongs_to :tenant, class_name: 'Anoubis::Tenant::Tenant'

#uuidString

UUID representations in string format

Returns:

  • (String)

    string representaion of user UUID



65
66
67
# File 'app/models/anoubis/tenant/user.rb', line 65

def uuid
  @uuid
end

#uuid_binBlob

Returns user’s binary representation of UUID.

Returns:

  • (Blob)

    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_userObject

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(options={})
  h = super(options)
  h[:groups_access] = []
  self.user_groups.each do |ug|
    h[:groups_access].push ug.group.full_ident
  end
  h
end

#before_destroy_anubis_userObject

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_userObject

Is called before user will be stored in database. Changes #login and #email values to lower case. Deletes user cache in Redis database when user’s parameters has been changed.



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. = self.email+'.'+self.tenant.ident
  self.redis.del(self.redis_prefix + 'user:' + self.uuid) if self.redis
end

#before_validation_anubis_user_on_createObject

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_updateObject

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_uniqueObject



222
223
224
# File 'app/models/anoubis/tenant/user.rb', line 222

def email_unique
  true
end

#get_groups_accessObject

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_presenceObject



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

Returns:

  • (Boolean)

    return true if password has been changed



136
137
138
# File 'app/models/anoubis/tenant/user.rb', line 136

def password_changed?
  !password.blank?
end

#sys_titleString

Returns user model system title

Returns:

  • (String)

    user 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

Parameters:

  • options (Hash) (defaults to: {})

    additional options



179
180
181
182
# File 'app/models/anoubis/tenant/user.rb', line 179

def to_json(options={})
  options[:except] ||= [:password_digest, :uuid_bin]
  super(options)
end

#validate_anubis_user(is_new) ⇒ Object

Validates users element. Sets default missing parameters. Prevents changing tenant for existing user.

Parameters:

  • is_new (Boolean)

    sets into true when user is being created



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