Class: DecisionAgent::Auth::User
- Inherits:
-
Object
- Object
- DecisionAgent::Auth::User
- Defined in:
- lib/decision_agent/auth/user.rb
Instance Attribute Summary collapse
-
#active ⇒ Object
Returns the value of attribute active.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Instance Method Summary collapse
- #assign_role(role) ⇒ Object
- #authenticate(password) ⇒ Object
- #has_role?(role) ⇒ Boolean
-
#initialize(email:, password: nil, password_hash: nil, roles: [], active: true, id: nil) ⇒ User
constructor
A new instance of User.
- #remove_role(role) ⇒ Object
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
- #update_password(new_password) ⇒ Object
Constructor Details
#initialize(email:, password: nil, password_hash: nil, roles: [], active: true, id: nil) ⇒ User
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/decision_agent/auth/user.rb', line 12 def initialize(email:, password: nil, password_hash: nil, roles: [], active: true, id: nil) @id = id || SecureRandom.uuid @email = email @roles = Array(roles) @active = active @created_at = Time.now.utc @updated_at = Time.now.utc if password_hash @password_hash = password_hash elsif password @password_hash = BCrypt::Password.create(password) else raise ArgumentError, "Either password or password_hash must be provided" end end |
Instance Attribute Details
#active ⇒ Object
Returns the value of attribute active.
10 11 12 |
# File 'lib/decision_agent/auth/user.rb', line 10 def active @active end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
9 10 11 |
# File 'lib/decision_agent/auth/user.rb', line 9 def created_at @created_at end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
9 10 11 |
# File 'lib/decision_agent/auth/user.rb', line 9 def email @email end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/decision_agent/auth/user.rb', line 9 def id @id end |
#roles ⇒ Object (readonly)
Returns the value of attribute roles.
9 10 11 |
# File 'lib/decision_agent/auth/user.rb', line 9 def roles @roles end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
9 10 11 |
# File 'lib/decision_agent/auth/user.rb', line 9 def updated_at @updated_at end |
Instance Method Details
#assign_role(role) ⇒ Object
35 36 37 38 39 |
# File 'lib/decision_agent/auth/user.rb', line 35 def assign_role(role) role_symbol = role.to_sym @roles << role_symbol unless @roles.include?(role_symbol) @updated_at = Time.now.utc end |
#authenticate(password) ⇒ Object
29 30 31 32 33 |
# File 'lib/decision_agent/auth/user.rb', line 29 def authenticate(password) return false unless @active BCrypt::Password.new(@password_hash) == password end |
#has_role?(role) ⇒ Boolean
47 48 49 |
# File 'lib/decision_agent/auth/user.rb', line 47 def has_role?(role) @roles.include?(role.to_sym) end |
#remove_role(role) ⇒ Object
41 42 43 44 45 |
# File 'lib/decision_agent/auth/user.rb', line 41 def remove_role(role) role_symbol = role.to_sym @roles.delete(role_symbol) @updated_at = Time.now.utc end |
#to_h ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/decision_agent/auth/user.rb', line 56 def to_h { id: @id, email: @email, roles: @roles.map(&:to_s), active: @active, created_at: @created_at.iso8601, updated_at: @updated_at.iso8601 } end |
#to_json(*args) ⇒ Object
67 68 69 |
# File 'lib/decision_agent/auth/user.rb', line 67 def to_json(*args) to_h.to_json(*args) end |
#update_password(new_password) ⇒ Object
51 52 53 54 |
# File 'lib/decision_agent/auth/user.rb', line 51 def update_password(new_password) @password_hash = BCrypt::Password.create(new_password) @updated_at = Time.now.utc end |