2019-08-18 18:51:25 +02:00
|
|
|
class User < ApplicationRecord
|
2022-07-18 10:47:54 +02:00
|
|
|
include TenantOwnable
|
|
|
|
|
|
2019-08-21 16:13:39 +02:00
|
|
|
devise :database_authenticatable, :registerable,
|
2022-07-18 10:47:54 +02:00
|
|
|
:recoverable, :rememberable, :confirmable
|
2023-03-19 19:57:53 +01:00
|
|
|
|
|
|
|
|
validates_confirmation_of :password
|
2019-09-23 15:52:00 +02:00
|
|
|
|
2019-09-24 21:16:51 +02:00
|
|
|
has_many :posts, dependent: :destroy
|
2019-09-27 12:32:30 +02:00
|
|
|
has_many :likes, dependent: :destroy
|
2019-09-24 21:16:51 +02:00
|
|
|
has_many :comments, dependent: :destroy
|
2019-09-16 18:02:52 +02:00
|
|
|
|
2023-01-18 21:11:27 +01:00
|
|
|
enum role: [:user, :moderator, :admin, :owner]
|
2022-06-24 14:39:35 +02:00
|
|
|
enum status: [:active, :blocked, :deleted]
|
|
|
|
|
|
2019-08-19 10:37:45 +02:00
|
|
|
after_initialize :set_default_role, if: :new_record?
|
2022-06-24 14:39:35 +02:00
|
|
|
after_initialize :set_default_status, if: :new_record?
|
2022-07-16 12:36:33 +02:00
|
|
|
|
|
|
|
|
before_save :skip_confirmation
|
2019-08-19 10:37:45 +02:00
|
|
|
|
2019-08-23 15:58:43 +02:00
|
|
|
validates :full_name, presence: true, length: { in: 2..32 }
|
2022-07-18 10:47:54 +02:00
|
|
|
validates :email,
|
|
|
|
|
presence: true,
|
|
|
|
|
uniqueness: { scope: :tenant_id, case_sensitive: false },
|
|
|
|
|
format: { with: URI::MailTo::EMAIL_REGEXP }
|
|
|
|
|
validates :password, allow_blank: true, length: { in: 6..128 }
|
|
|
|
|
validates :password, presence: true, on: :create
|
2019-08-19 15:45:44 +02:00
|
|
|
|
2019-08-19 10:37:45 +02:00
|
|
|
def set_default_role
|
|
|
|
|
self.role ||= :user
|
|
|
|
|
end
|
|
|
|
|
|
2022-06-24 14:39:35 +02:00
|
|
|
def set_default_status
|
|
|
|
|
self.status ||= :active
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def active_for_authentication?
|
|
|
|
|
super && active?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def inactive_message
|
2022-07-23 13:32:40 +02:00
|
|
|
active? ? super : I18n.t('errors.user_blocked_or_deleted')
|
2022-06-24 14:39:35 +02:00
|
|
|
end
|
|
|
|
|
|
2022-07-18 10:47:54 +02:00
|
|
|
# Override Devise::Confirmable#after_confirmation
|
|
|
|
|
# Used to change tenant status from pending to active on owner email confirmation
|
|
|
|
|
def after_confirmation
|
|
|
|
|
tenant = self.tenant
|
|
|
|
|
|
|
|
|
|
if tenant.status == "pending" and tenant.users.count == 1
|
|
|
|
|
tenant.status = "active"
|
|
|
|
|
tenant.save
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-09-24 12:57:32 +02:00
|
|
|
def skip_confirmation
|
|
|
|
|
return if Rails.application.email_confirmation?
|
|
|
|
|
skip_confirmation!
|
|
|
|
|
skip_confirmation_notification!
|
|
|
|
|
skip_reconfirmation!
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-19 17:42:13 +02:00
|
|
|
def gravatar_url
|
|
|
|
|
gravatar_id = Digest::MD5::hexdigest(email.downcase)
|
|
|
|
|
"https://secure.gravatar.com/avatar/#{gravatar_id}"
|
|
|
|
|
end
|
2019-09-16 19:38:56 +02:00
|
|
|
|
2023-01-18 21:11:27 +01:00
|
|
|
def owner?
|
|
|
|
|
role == 'owner'
|
2019-09-16 19:38:56 +02:00
|
|
|
end
|
2022-05-01 18:00:38 +02:00
|
|
|
|
|
|
|
|
def admin?
|
2023-01-18 21:11:27 +01:00
|
|
|
owner? || role == 'admin'
|
2022-05-01 18:00:38 +02:00
|
|
|
end
|
2022-06-24 14:39:35 +02:00
|
|
|
|
|
|
|
|
def moderator?
|
2023-01-18 21:11:27 +01:00
|
|
|
admin? || role == 'moderator'
|
2022-06-24 14:39:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def active?
|
|
|
|
|
status == 'active'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def blocked?
|
|
|
|
|
status == 'blocked'
|
|
|
|
|
end
|
2019-08-18 18:51:25 +02:00
|
|
|
end
|