Class: User

Inherits:
RailsBase::ApplicationRecord show all
Includes:
RailsBase::UserConstants, RailsBase::UserHelper::Totp
Defined in:
app/models/user.rb

Overview

Schema Information

Table name: users

id                         :bigint           not null, primary key
first_name                 :string(255)      default(""), not null
last_name                  :string(255)      default(""), not null
phone_number               :string(255)
last_mfa_sms_login         :datetime
email_validated            :boolean          default(FALSE)
mfa_sms_enabled            :boolean          default(FALSE), not null
active                     :boolean          default(TRUE), not null
admin                      :string(255)
last_known_timezone        :string(255)
last_known_timezone_update :datetime
email                      :string(255)      default(""), not null
encrypted_password         :string(255)      default(""), not null
reset_password_token       :string(255)
reset_password_sent_at     :datetime
remember_created_at        :datetime
sign_in_count              :integer          default(0), not null
current_sign_in_at         :datetime
last_sign_in_at            :datetime
current_sign_in_ip         :string(255)
last_sign_in_ip            :string(255)
created_at                 :datetime         not null
updated_at                 :datetime         not null
otp_secret                 :string(255)
temp_otp_secret            :string(255)
consumed_timestep          :integer
mfa_otp_enabled            :boolean          default(FALSE)
otp_backup_codes           :text(65535)
last_mfa_otp_login         :datetime

Constant Summary

Constants included from RailsBase::UserConstants

RailsBase::UserConstants::ADMIN_ENUMS, RailsBase::UserConstants::SAFE_AUTOMAGIC_UPGRADE_COLS, RailsBase::UserConstants::SOFT_DESTROY_PARAMS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RailsBase::UserHelper::Totp

included, #reset_otp!

Methods inherited from RailsBase::ApplicationRecord

_magically_defined_time_objects

Class Method Details

._def_admin_convenience_method!(admin_method:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/user.rb', line 47

def self._def_admin_convenience_method!(admin_method:)
  types = RailsBase.config.admin.admin_types
  #### methods on the instance
  define_method("at_least_#{admin_method}?") do
    i = types.find_index(admin.to_sym)
    i >= types.find_index(admin_method.to_sym)
  end

  define_method("admin_#{admin_method}?") do
    admin.to_sym == admin_method
  end

  define_method("admin_#{admin_method}!") do
    update!(admin: admin_method)
  end

  #### metods on the class
  define_singleton_method("admin_#{admin_method}s") do
    where(admin: admin_method)
  end

  define_singleton_method("admin_#{admin_method}") do
    arr = [admin_method]
    arr = [admin_method, '', nil] if ADMIN_ROLE_NONE == admin_method
    where(admin: arr)
  end
end

.masked_number(phone_number) ⇒ Object



75
76
77
78
79
# File 'app/models/user.rb', line 75

def self.masked_number(phone_number)
  return nil unless phone_number

  "(#{phone_number[0]}**) ****-**#{phone_number[-2..-1]}"
end

.readable_phone_number(phone_number) ⇒ Object



81
82
83
84
85
# File 'app/models/user.rb', line 81

def self.readable_phone_number(phone_number)
  return nil unless phone_number

  "(#{phone_number[0..2]}) #{phone_number[3..5]}-#{phone_number[6..-1]}"
end

Instance Method Details

#adminObject



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

def admin
  (self[:admin].presence || ADMIN_ROLE_NONE).to_sym
end

#convert_time(time:) ⇒ Object



134
135
136
# File 'app/models/user.rb', line 134

def convert_time(time:)
  time.in_time_zone(timezone)
end

#destroy_user!Object



115
116
117
# File 'app/models/user.rb', line 115

def destroy_user!
  self.delete
end

#full_nameObject



91
92
93
# File 'app/models/user.rb', line 91

def full_name
	"#{first_name} #{last_name}"
end

#inspect_nameObject



119
120
121
# File 'app/models/user.rb', line 119

def inspect_name
  "[#{id}]: #{full_name}"
end

#masked_phoneObject



103
104
105
# File 'app/models/user.rb', line 103

def masked_phone
  User.masked_number(phone_number)
end

#readable_phoneObject



107
108
109
# File 'app/models/user.rb', line 107

def readable_phone
   User.readable_phone_number(phone_number)
end

#set_last_mfa_otp_login!(time: Time.zone.now) ⇒ Object



99
100
101
# File 'app/models/user.rb', line 99

def set_last_mfa_otp_login!(time: Time.zone.now)
  update(last_mfa_otp_login: time)
end

#set_last_mfa_sms_login!(time: Time.zone.now) ⇒ Object



95
96
97
# File 'app/models/user.rb', line 95

def set_last_mfa_sms_login!(time: Time.zone.now)
  update(last_mfa_sms_login: time)
end

#soft_destroy_user!Object



111
112
113
# File 'app/models/user.rb', line 111

def soft_destroy_user!
  update(SOFT_DESTROY_PARAMS)
end

#timezoneObject



130
131
132
# File 'app/models/user.rb', line 130

def timezone
  RailsBase.config.user.user_timezone(self)
end

#update_tz(tz_name:) ⇒ Object



123
124
125
126
127
128
# File 'app/models/user.rb', line 123

def update_tz(tz_name:)
  return if last_known_timezone == tz_name

  Rails.logger.info { "#{id}: Setting tz_name: #{tz_name}"  }
  update(last_known_timezone: tz_name, last_known_timezone_update: Time.now )
end