Class: Masks::Rails::Actor

Inherits:
ApplicationRecord show all
Includes:
Actor
Defined in:
app/models/masks/rails/actor.rb

Instance Method Summary collapse

Methods included from Actor

#actor_id, #anonymous?, #authenticate, #backup?, #nickname, #password=, #session=, #session_key, #version

Methods included from Scoped

#role?, #role_records, #scope?

Instance Method Details

#assign_role(record, **opts) ⇒ Object



128
129
130
131
132
133
134
# File 'app/models/masks/rails/actor.rb', line 128

def assign_role(record, **opts)
  saved_roles
    .find_by!(record:)
    .tap { |role| role.assign_attributes(**opts) }
rescue ActiveRecord::RecordNotFound
  saved_roles.build(record:, **opts)
end

#assign_role!(record, **opts) ⇒ Object



136
137
138
# File 'app/models/masks/rails/actor.rb', line 136

def assign_role!(record, **opts)
  assign_role(record, **opts).save!
end

#assign_scopes(*list) ⇒ Object



116
117
118
# File 'app/models/masks/rails/actor.rb', line 116

def assign_scopes(*list)
  list.map { |scope| saved_scopes.find_or_initialize_by(name: scope) }
end

#assign_scopes!(*list) ⇒ Object



120
121
122
# File 'app/models/masks/rails/actor.rb', line 120

def assign_scopes!(*list)
  list.map { |scope| saved_scopes.find_or_create_by(name: scope) }
end

#changed_during_mask?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'app/models/masks/rails/actor.rb', line 169

def changed_during_mask?
  changed? && changes.keys != ["session"]
end

#email_addressesObject



72
73
74
# File 'app/models/masks/rails/actor.rb', line 72

def email_addresses
  emails.pluck(:email)
end

#factor2?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'app/models/masks/rails/actor.rb', line 76

def factor2?
  phone_number || totp_secret
end

#logout!Object



88
89
90
91
# File 'app/models/masks/rails/actor.rb', line 88

def logout!
  reset_version
  save!
end

#mask!Object



157
158
159
# File 'app/models/masks/rails/actor.rb', line 157

def mask!
  save # sub-classes are encouraged to override
end

#primary_emailObject



68
69
70
# File 'app/models/masks/rails/actor.rb', line 68

def primary_email
  emails.where(verified: true).first
end

#random_totpObject



108
109
110
# File 'app/models/masks/rails/actor.rb', line 108

def random_totp
  ROTP::TOTP.new(random_totp_secret, issuer: Masks.configuration.issuer)
end

#random_totp_secretObject



112
113
114
# File 'app/models/masks/rails/actor.rb', line 112

def random_totp_secret
  @random_totp_secret ||= ROTP::Base32.random
end

#remove_factor2!Object



80
81
82
83
84
85
86
# File 'app/models/masks/rails/actor.rb', line 80

def remove_factor2!
  self.added_totp_secret_at = nil
  saved_backup_codes_at
  self.totp_secret = nil
  self.backup_codes = nil
  save!
end

#remove_role!(record) ⇒ Object



140
141
142
# File 'app/models/masks/rails/actor.rb', line 140

def remove_role!(record)
  saved_roles.where(record:).destroy_all
end

#remove_scopes!(*list) ⇒ Object



124
125
126
# File 'app/models/masks/rails/actor.rb', line 124

def remove_scopes!(*list)
  saved_scopes.where(name: list).destroy_all
end

#roles(record, **opts) ⇒ Object



148
149
150
151
152
153
154
155
# File 'app/models/masks/rails/actor.rb', line 148

def roles(record, **opts)
  case record
  when Class, String
    saved_roles.where(record_type: record.to_s, **opts).includes(:record)
  else
    saved_roles.where(record:, **opts)
  end
end

#saved_backup_codes?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'app/models/masks/rails/actor.rb', line 165

def saved_backup_codes?
  factor2? && saved_backup_codes_at.present?
end

#scopesObject



144
145
146
# File 'app/models/masks/rails/actor.rb', line 144

def scopes
  @scopes ||= saved_scopes.order(created_at: :desc).pluck(:name)
end

#should_save_backup_codes?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'app/models/masks/rails/actor.rb', line 161

def should_save_backup_codes?
  factor2? && saved_backup_codes_at.blank?
end

#to_paramObject



64
65
66
# File 'app/models/masks/rails/actor.rb', line 64

def to_param
  nickname
end

#totpObject



102
103
104
105
106
# File 'app/models/masks/rails/actor.rb', line 102

def totp
  return unless totp_secret

  ROTP::TOTP.new(totp_secret, issuer: Masks.configuration.issuer)
end

#totp_svg(**opts) ⇒ Object



97
98
99
100
# File 'app/models/masks/rails/actor.rb', line 97

def totp_svg(**opts)
  qrcode = RQRCode::QRCode.new(totp_uri)
  qrcode.as_svg(**opts)
end

#totp_uriObject



93
94
95
# File 'app/models/masks/rails/actor.rb', line 93

def totp_uri
  (totp || random_totp).provisioning_uri(nickname)
end