2024-04-14 10:10:10 +00:00
|
|
|
class User < ApplicationRecord
|
2024-05-26 09:06:04 +00:00
|
|
|
has_and_belongs_to_many :site_roles
|
|
|
|
|
2024-04-14 10:10:10 +00:00
|
|
|
has_secure_password
|
|
|
|
generates_token_for :password_reset, expires_in: 4.hours do
|
|
|
|
password_salt.last(10) # Invalidates when password changed
|
|
|
|
end
|
2024-04-14 19:01:32 +00:00
|
|
|
generates_token_for :email_verification, expires_in: 1.week do
|
|
|
|
verified.to_s
|
|
|
|
end
|
2024-04-14 10:10:10 +00:00
|
|
|
|
|
|
|
validates :username,
|
|
|
|
presence: true,
|
|
|
|
uniqueness: true,
|
|
|
|
length: { minimum: 3, maximum: 20 }
|
|
|
|
normalizes :username, with: ->(username) { username.strip.downcase }
|
|
|
|
validates :email,
|
|
|
|
presence: true,
|
|
|
|
uniqueness: true,
|
|
|
|
length: { minimum: 5, maximum: 100 },
|
|
|
|
format: { with: URI::MailTo::EMAIL_REGEXP,
|
2024-04-14 19:01:32 +00:00
|
|
|
message: I18n.t("users.validations.email_format"),
|
2024-04-14 10:48:42 +00:00
|
|
|
}
|
2024-04-14 10:10:10 +00:00
|
|
|
normalizes :email, with: ->(email) { email.strip.downcase }
|
|
|
|
validates :first_name,
|
|
|
|
presence: true,
|
|
|
|
length: { maximum: 50 }
|
|
|
|
validates :last_name,
|
|
|
|
allow_nil: false,
|
|
|
|
length: { maximum: 50 }
|
|
|
|
|
2024-04-14 19:01:32 +00:00
|
|
|
scope :verified, -> { where(verified: true) }
|
|
|
|
scope :unverified, -> { where(verified: false) }
|
|
|
|
|
2024-04-14 10:10:10 +00:00
|
|
|
def full_name
|
|
|
|
return first_name if last_name.blank?
|
|
|
|
|
|
|
|
"#{first_name} #{last_name}"
|
|
|
|
end
|
2024-05-26 09:06:04 +00:00
|
|
|
|
|
|
|
def admin?
|
|
|
|
site_roles.include? SiteRole.find_by(name: "Admin")
|
|
|
|
end
|
2024-04-14 10:10:10 +00:00
|
|
|
end
|