# frozen_string_literal: true # == Schema Information # # Table name: users # # id :bigint not null, primary key # consumed_timestep :integer # current_sign_in_at :datetime # current_sign_in_ip :string # deleted_at :datetime # email :string not null # encrypted_password :string not null # failed_attempts :integer default(0), not null # first_name :string # last_name :string # last_sign_in_at :datetime # last_sign_in_ip :string # locked_at :datetime # otp_required_for_login :boolean default(FALSE), not null # otp_secret :string # 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 # uuid :text not null # 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 # index_users_on_uuid (uuid) UNIQUE # # Foreign Keys # # fk_rails_... (account_id => accounts.id) # class User < ApplicationRecord ROLES = [ ADMIN_ROLE = 'admin' ].freeze EMAIL_REGEXP = /[^@;,<>\s]+@[^@;,<>\s]+/ belongs_to :account has_one :access_token, dependent: :destroy has_many :templates, dependent: :destroy, foreign_key: :author_id, inverse_of: :author devise :two_factor_authenticatable, :recoverable, :rememberable, :validatable, :trackable devise :registerable, :omniauthable, omniauth_providers: [:google_oauth2] if Docuseal.multitenant? attribute :role, :string, default: ADMIN_ROLE attribute :uuid, :string, default: -> { SecureRandom.uuid } scope :active, -> { where(deleted_at: nil) } scope :admins, -> { where(role: ADMIN_ROLE) } def access_token super || build_access_token.tap(&:save!) end def active_for_authentication? !deleted_at? end def remember_me true end def initials [first_name&.first, last_name&.first].compact_blank.join.upcase end def full_name [first_name, last_name].compact_blank.join(' ') end def friendly_name if full_name.present? "#{full_name} <#{email}>" else email end end end