retrospring/app/models/user.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

73 lines
2.3 KiB
Ruby
Raw Normal View History

2022-06-26 11:00:09 +02:00
# frozen_string_literal: true
class User < ApplicationRecord
2020-10-18 10:39:46 +02:00
include ActiveModel::OneTimePassword
2014-08-01 12:07:16 +02:00
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
2015-01-01 18:17:34 +01:00
devise :database_authenticatable, :async, :registerable,
2014-08-01 15:27:08 +02:00
:recoverable, :rememberable, :trackable,
2023-01-02 11:16:47 +01:00
:validatable, :confirmable, authentication_keys: [:login]
2014-11-30 19:43:22 +01:00
2020-10-18 10:39:46 +02:00
has_one_time_password
enum otp_module: { disabled: 0, enabled: 1 }, _prefix: true
2020-10-18 10:39:46 +02:00
attr_accessor :otp_attempt, :otp_validation
2023-01-02 11:16:47 +01:00
attr_writer :login
2020-10-18 10:39:46 +02:00
rolify
has_many :totp_recovery_codes, dependent: :destroy_async
2015-04-21 03:12:11 +02:00
has_one :profile, dependent: :destroy
2024-01-14 13:14:04 +01:00
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]+\z/
2023-01-02 11:16:47 +01:00
WEBSITE_REGEX = /https?:\/\/([A-Za-z.-]+)\/?(?:.*)/i
2014-11-30 19:43:22 +01:00
before_validation do
screen_name.strip!
end
validates :email, fake_email: true, typoed_email: true
validates :sharing_custom_url, allow_blank: true, valid_url: true
validates :screen_name,
presence: true,
format: { with: SCREEN_NAME_REGEX, message: I18n.t("activerecord.validation.user.screen_name.format") },
length: { minimum: 1, maximum: 16 },
uniqueness: { case_sensitive: false },
screen_name: true
2020-05-02 00:43:46 +02:00
mount_uploader :profile_picture, ProfilePictureUploader, mount_on: :profile_picture_file_name
2020-05-17 20:58:27 +02:00
process_in_background :profile_picture
2020-05-02 00:43:46 +02:00
mount_uploader :profile_header, ProfileHeaderUploader, mount_on: :profile_header_file_name
2020-05-17 20:58:27 +02:00
process_in_background :profile_header
2014-12-29 10:18:12 +01:00
after_destroy do
Retrospring::Metrics::USERS_DESTROYED.increment
end
after_create do
Profile.create(user_id: id) if Profile.where(user_id: id).count.zero?
Retrospring::Metrics::USERS_CREATED.increment
end
# use the screen name as parameter for url helpers
def to_param = screen_name
2023-01-02 11:16:47 +01:00
def login = @login || screen_name || email
2014-11-30 19:43:22 +01:00
2014-08-01 15:27:08 +02:00
def self.find_first_by_auth_conditions(warden_conditions)
2015-04-22 00:33:55 +02:00
conditions = warden_conditions.dup
2023-01-02 11:16:47 +01:00
if (login = conditions.delete(:login))
where(conditions).where(["lower(screen_name) = :value OR lower(email) = :value", { value: login.downcase }]).first
2014-08-01 15:27:08 +02:00
else
where(conditions).first
end
end
2014-12-28 19:32:08 +01:00
# @return [Boolean] is the user a moderator?
2023-01-02 11:16:47 +01:00
def mod? = has_cached_role?(:moderator) || has_cached_role?(:administrator)
2022-08-21 15:02:49 +02:00
2023-01-02 11:16:47 +01:00
def admin? = has_cached_role?(:administrator)
2014-08-01 12:07:16 +02:00
end