Files
astuto/app/models/user.rb

99 lines
2.2 KiB
Ruby
Raw Normal View History

2019-08-18 18:51:25 +02:00
class User < ApplicationRecord
2022-07-18 10:47:54 +02:00
include TenantOwnable
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-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]
enum status: [:active, :blocked, :deleted]
2019-08-19 10:37:45 +02:00
after_initialize :set_default_role, if: :new_record?
after_initialize :set_default_status, if: :new_record?
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
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')
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
2024-05-03 18:11:07 +02:00
CreateStripeCustomer.new().run
TenantMailer.trial_start(tenant: tenant).deliver_later
2022-07-18 10:47:54 +02:00
end
end
def skip_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
def moderator?
2023-01-18 21:11:27 +01:00
admin? || role == 'moderator'
end
def active?
status == 'active'
end
def blocked?
status == 'blocked'
end
2024-01-22 14:45:48 +01:00
def generate_oauth_token
self.oauth_token = SecureRandom.urlsafe_base64
self.save!
oauth_token
end
def invalidate_oauth_token
self.oauth_token = nil
self.save!
end
2019-08-18 18:51:25 +02:00
end