Class: AnoubisSsoServer::User

Inherits:
ApplicationRecord show all
Defined in:
app/models/anoubis_sso_server/user.rb

Overview

Default user class

Constant Summary collapse

VALID_EMAIL_REGEX =

Regexp validation mask for email

/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_cache(uuid) ⇒ Hash

Procedure returns User model data from the cache or database. If user data isn’t present in cache then call procedure that cache User model data.

Parameters:

  • uuid (String)
    • User private UUID

Returns:

  • (Hash)

    User data



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/anoubis_sso_server/user.rb', line 94

def self.load_cache(uuid)
  begin
    data = JSON.parse self.redis.get("#{self.redis_prefix}user:#{uuid}"), { symbolize_names: true }
  rescue
    data = nil
  end

  unless data
    user = self.where(uuid: uuid).first

    return nil unless user

    user.save_cache
    data = JSON.parse(user.to_json(except: [:password_digest]), { symbolize_names: true })
  end

  data
end

Instance Method Details

#after_destroy_sso_server_userObject

Fires before delete User from database. Procedure call procedure for clear Redis cache data for the user.



67
68
69
# File 'app/models/anoubis_sso_server/user.rb', line 67

def after_destroy_sso_server_user
  clear_cache
end

#before_save_sso_server_userObject

Fires before save User data to database. Procedure setup email, timezone and call procedure for clear Redis cache data for the user.



58
59
60
61
62
# File 'app/models/anoubis_sso_server/user.rb', line 58

def before_save_sso_server_user
  self.timezone = 'GMT' if !self.timezone
  self.email = self.email.downcase
  self.clear_cache
end

#before_validation_sso_server_user_on_createObject

Fires before create any User on the server. Procedure generates internal UUID and setup timezone to GMT. Public user identifier is generated also if not defined.



34
35
36
37
38
39
# File 'app/models/anoubis_sso_server/user.rb', line 34

def before_validation_sso_server_user_on_create
  self.uuid = setup_private_user_id
  self.public = setup_public_user_id unless public

  self.timezone = 'GMT' if !self.timezone
end

#clear_cacheObject

Procedure clear cached User model data.



79
80
81
# File 'app/models/anoubis_sso_server/user.rb', line 79

def clear_cache
  redis.del("#{redis_prefix}user:#{uuid}") if redis
end

#password_changed?Boolean

Procedure checks if password was changed.

Returns:

  • (Boolean)

    return true if password was changed



86
87
88
# File 'app/models/anoubis_sso_server/user.rb', line 86

def password_changed?
  !password.blank?
end

#save_cacheObject

Procedure saves cached User model data to Redis database for improve access speed.



73
74
75
# File 'app/models/anoubis_sso_server/user.rb', line 73

def save_cache
  redis.set("#{redis_prefix}user:#{uuid}", self.to_json(except: [:password_digest])) if redis
end

#setup_private_user_idString

Procedure setup private user identifier. Procedure can be redefined.

Returns:

  • (String)

    public user identifier



44
45
46
# File 'app/models/anoubis_sso_server/user.rb', line 44

def setup_private_user_id
  SecureRandom.uuid
end

#setup_public_user_idString

Procedure setup public user identifier. Used for open API. Procedure can be redefined.

Returns:

  • (String)

    public user identifier



51
52
53
# File 'app/models/anoubis_sso_server/user.rb', line 51

def setup_public_user_id
  SecureRandom.uuid
end