Files
docuseal/app/models/user.rb
T

87 lines
2.4 KiB
Ruby
Raw Normal View History

2023-05-21 17:59:36 +03:00
# frozen_string_literal: true
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# 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
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
# 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
ROLES = %w[admin].freeze
2023-08-05 21:39:59 +03:00
EMAIL_REGEXP = /[^@,\s]+@[^@,\s]+/
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-05-21 17:59:36 +03:00
2023-06-07 12:04:11 +03:00
devise :database_authenticatable, :recoverable, :rememberable, :validatable, :trackable
2023-07-14 22:33:42 +03:00
devise :registerable, :omniauthable, omniauth_providers: [:google_oauth2] if Docuseal.multitenant?
2023-05-21 17:59:36 +03:00
attribute :role, :string, default: 'admin'
2023-08-04 00:40:13 +03:00
attribute :uuid, :string, default: -> { SecureRandom.uuid }
2023-05-21 17:59:36 +03:00
scope :active, -> { where(deleted_at: nil) }
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?
!deleted_at?
end
2023-07-27 23:33:42 +03:00
def remember_me
true
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?
"#{full_name} <#{email}>"
else
email
end
2023-05-21 17:59:36 +03:00
end
end