2023-05-21 17:59:36 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
|
#
|
|
|
|
|
# Table name: users
|
|
|
|
|
#
|
|
|
|
|
# id :bigint not null, primary key
|
2023-12-30 00:37:03 +02:00
|
|
|
# archived_at :datetime
|
2023-09-16 01:06:29 +03:00
|
|
|
# consumed_timestep :integer
|
2023-05-21 17:59:36 +03:00
|
|
|
# current_sign_in_at :datetime
|
|
|
|
|
# current_sign_in_ip :string
|
|
|
|
|
# email :string not null
|
|
|
|
|
# encrypted_password :string not null
|
|
|
|
|
# failed_attempts :integer default(0), not null
|
2023-08-19 21:05:54 +03:00
|
|
|
# first_name :string
|
|
|
|
|
# last_name :string
|
2023-05-21 17:59:36 +03:00
|
|
|
# last_sign_in_at :datetime
|
|
|
|
|
# last_sign_in_ip :string
|
|
|
|
|
# locked_at :datetime
|
2023-09-16 01:06:29 +03:00
|
|
|
# otp_required_for_login :boolean default(FALSE), not null
|
|
|
|
|
# otp_secret :string
|
2023-05-21 17:59:36 +03:00
|
|
|
# remember_created_at :datetime
|
|
|
|
|
# reset_password_sent_at :datetime
|
|
|
|
|
# reset_password_token :string
|
|
|
|
|
# role :string not null
|
|
|
|
|
# sign_in_count :integer default(0), not null
|
|
|
|
|
# unlock_token :string
|
2023-08-04 00:40:13 +03:00
|
|
|
# uuid :text not null
|
2023-05-21 17:59:36 +03:00
|
|
|
# created_at :datetime not null
|
|
|
|
|
# updated_at :datetime not null
|
|
|
|
|
# account_id :bigint not null
|
|
|
|
|
#
|
|
|
|
|
# Indexes
|
|
|
|
|
#
|
|
|
|
|
# index_users_on_account_id (account_id)
|
|
|
|
|
# index_users_on_email (email) UNIQUE
|
|
|
|
|
# index_users_on_reset_password_token (reset_password_token) UNIQUE
|
|
|
|
|
# index_users_on_unlock_token (unlock_token) UNIQUE
|
2023-08-04 00:40:13 +03:00
|
|
|
# index_users_on_uuid (uuid) UNIQUE
|
2023-05-21 17:59:36 +03:00
|
|
|
#
|
|
|
|
|
# Foreign Keys
|
|
|
|
|
#
|
|
|
|
|
# fk_rails_... (account_id => accounts.id)
|
|
|
|
|
#
|
|
|
|
|
class User < ApplicationRecord
|
2023-08-20 12:42:47 +03:00
|
|
|
ROLES = [
|
|
|
|
|
ADMIN_ROLE = 'admin'
|
|
|
|
|
].freeze
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2023-09-17 09:27:41 +03:00
|
|
|
EMAIL_REGEXP = /[^@;,<>\s]+@[^@;,<>\s]+/
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2024-10-24 23:16:03 +03:00
|
|
|
FULL_EMAIL_REGEXP =
|
|
|
|
|
/\A[a-z0-9][\.']?(?:(?:[a-z0-9_-]+[\.\+'])*[a-z0-9_-]+)*@(?:[a-z0-9]+[\.-])*[a-z0-9]+\.[a-z]{2,}\z/i
|
|
|
|
|
|
2023-12-30 15:12:00 +02:00
|
|
|
has_one_attached :signature
|
2024-06-02 17:06:09 +03:00
|
|
|
has_one_attached :initials
|
2023-12-30 15:12:00 +02:00
|
|
|
|
2023-05-21 17:59:36 +03:00
|
|
|
belongs_to :account
|
2023-08-06 17:33:08 +03:00
|
|
|
has_one :access_token, dependent: :destroy
|
2023-08-19 21:05:54 +03:00
|
|
|
has_many :templates, dependent: :destroy, foreign_key: :author_id, inverse_of: :author
|
2023-11-17 02:23:55 +02:00
|
|
|
has_many :template_folders, dependent: :destroy, foreign_key: :author_id, inverse_of: :author
|
2023-09-22 21:50:39 +03:00
|
|
|
has_many :user_configs, dependent: :destroy
|
2023-09-25 01:57:10 +03:00
|
|
|
has_many :encrypted_configs, dependent: :destroy, class_name: 'EncryptedUserConfig'
|
2023-12-10 21:59:50 +02:00
|
|
|
has_many :email_messages, dependent: :destroy, foreign_key: :author_id, inverse_of: :author
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2024-01-16 00:12:55 +02:00
|
|
|
devise :two_factor_authenticatable, :recoverable, :rememberable, :validatable, :trackable, :lockable
|
2023-11-21 00:35:03 +02:00
|
|
|
|
2023-08-20 12:42:47 +03:00
|
|
|
attribute :role, :string, default: ADMIN_ROLE
|
2023-08-04 00:40:13 +03:00
|
|
|
attribute :uuid, :string, default: -> { SecureRandom.uuid }
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2023-12-30 00:37:03 +02:00
|
|
|
scope :active, -> { where(archived_at: nil) }
|
2024-04-19 16:08:12 +03:00
|
|
|
scope :archived, -> { where.not(archived_at: nil) }
|
2023-08-20 12:42:47 +03:00
|
|
|
scope :admins, -> { where(role: ADMIN_ROLE) }
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2023-08-06 17:33:08 +03:00
|
|
|
def access_token
|
|
|
|
|
super || build_access_token.tap(&:save!)
|
|
|
|
|
end
|
|
|
|
|
|
2023-05-21 17:59:36 +03:00
|
|
|
def active_for_authentication?
|
2024-04-28 10:27:44 +03:00
|
|
|
super && !archived_at? && !account.archived_at?
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
|
2023-07-27 23:33:42 +03:00
|
|
|
def remember_me
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
2024-06-18 16:51:42 +03:00
|
|
|
def sidekiq?
|
|
|
|
|
return true if Rails.env.development?
|
|
|
|
|
|
|
|
|
|
role == 'admin'
|
|
|
|
|
end
|
|
|
|
|
|
2023-10-21 17:44:04 +03:00
|
|
|
def self.sign_in_after_reset_password
|
|
|
|
|
if PasswordsController::Current.user.present?
|
|
|
|
|
!PasswordsController::Current.user.otp_required_for_login
|
|
|
|
|
else
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-05-29 23:26:17 +03:00
|
|
|
def initials
|
2023-08-19 21:05:54 +03:00
|
|
|
[first_name&.first, last_name&.first].compact_blank.join.upcase
|
2023-05-29 23:26:17 +03:00
|
|
|
end
|
|
|
|
|
|
2023-05-21 17:59:36 +03:00
|
|
|
def full_name
|
2023-08-19 21:05:54 +03:00
|
|
|
[first_name, last_name].compact_blank.join(' ')
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def friendly_name
|
2023-08-19 21:05:54 +03:00
|
|
|
if full_name.present?
|
2023-11-15 23:42:48 +02:00
|
|
|
%("#{full_name.delete('"')}" <#{email}>)
|
2023-08-19 21:05:54 +03:00
|
|
|
else
|
|
|
|
|
email
|
|
|
|
|
end
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
end
|