2018-12-01 21:03:19 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Account < ApplicationRecord
|
2019-02-01 22:48:23 -05:00
|
|
|
include Rolify::Role
|
|
|
|
extend Rolify::Dynamic if Rolify.dynamic_shortcuts
|
|
|
|
|
2019-01-31 20:33:08 -05:00
|
|
|
USERNAME_RE = /\A[a-z][_a-z0-9]*[a-z0-9]\z/.freeze
|
2019-01-31 19:47:44 -05:00
|
|
|
|
2019-02-01 22:48:23 -05:00
|
|
|
self.role_cname = 'Role'
|
|
|
|
self.role_table_name = 'roles'
|
|
|
|
self.strict_rolify = false
|
|
|
|
|
|
|
|
self.adapter = Rolify::Adapter::Base.create 'role_adapter', role_cname, name
|
|
|
|
|
2019-02-01 23:18:22 -05:00
|
|
|
has_many :account_roles,
|
2019-02-01 23:35:08 -05:00
|
|
|
-> { active },
|
2019-02-01 23:27:42 -05:00
|
|
|
inverse_of: :account,
|
|
|
|
dependent: :restrict_with_exception
|
2019-02-01 22:48:23 -05:00
|
|
|
|
|
|
|
has_many :roles, through: :account_roles
|
2018-12-01 21:28:34 -05:00
|
|
|
|
2018-12-09 22:36:09 -05:00
|
|
|
belongs_to :person, optional: true
|
|
|
|
|
2018-12-01 21:50:10 -05:00
|
|
|
has_one :user, dependent: :restrict_with_exception
|
|
|
|
|
2018-12-11 19:15:40 -05:00
|
|
|
has_many :account_telegram_contacts,
|
|
|
|
dependent: :restrict_with_exception
|
|
|
|
|
2018-12-13 00:36:00 -05:00
|
|
|
has_one :own_membership_app,
|
|
|
|
class_name: 'MembershipApp',
|
|
|
|
dependent: :restrict_with_exception
|
2018-12-02 05:54:56 -05:00
|
|
|
|
2018-12-01 21:28:34 -05:00
|
|
|
has_many :passport_confirmations, dependent: :restrict_with_exception
|
2018-12-04 20:49:26 -05:00
|
|
|
|
|
|
|
scope :guests, -> { includes(:user).where(users: { id: nil }) }
|
|
|
|
|
2019-01-31 19:47:44 -05:00
|
|
|
after_initialize :generate_username
|
|
|
|
|
2019-01-31 20:19:31 -05:00
|
|
|
before_create :generate_guest_token
|
2018-12-09 21:52:34 -05:00
|
|
|
|
2019-01-31 23:03:03 -05:00
|
|
|
validates :person_id, allow_nil: true, uniqueness: true
|
|
|
|
|
2019-01-31 19:47:44 -05:00
|
|
|
validates :username,
|
|
|
|
presence: true,
|
|
|
|
length: { in: 3..36 },
|
|
|
|
format: USERNAME_RE,
|
|
|
|
uniqueness: { case_sensitive: false }
|
|
|
|
|
2019-02-01 16:46:57 -05:00
|
|
|
validates :public_name,
|
|
|
|
allow_nil: true,
|
|
|
|
allow_blank: false,
|
|
|
|
presence: true
|
|
|
|
|
2019-01-31 23:02:51 -05:00
|
|
|
validates :biography, length: { maximum: 10_000 }
|
|
|
|
|
2019-02-01 16:36:10 -05:00
|
|
|
def to_param
|
|
|
|
username
|
|
|
|
end
|
|
|
|
|
2018-12-04 20:49:26 -05:00
|
|
|
def guest?
|
|
|
|
user.nil?
|
|
|
|
end
|
2018-12-05 20:55:35 -05:00
|
|
|
|
2019-02-08 02:26:08 -05:00
|
|
|
def add_role(role_name, resource = nil)
|
2018-12-05 20:55:35 -05:00
|
|
|
raise 'can not add role to guest account' if guest?
|
2018-12-05 20:56:52 -05:00
|
|
|
|
2019-02-08 02:35:35 -05:00
|
|
|
role = self.class.role_class.make! role_name, resource
|
|
|
|
|
|
|
|
return role if roles.include? role
|
|
|
|
|
|
|
|
if Rolify.dynamic_shortcuts
|
|
|
|
self.class.define_dynamic_method role.name, resource
|
|
|
|
end
|
|
|
|
|
|
|
|
roles << role
|
|
|
|
|
|
|
|
role
|
2018-12-05 20:55:35 -05:00
|
|
|
end
|
2018-12-11 22:15:03 -05:00
|
|
|
|
2019-02-01 23:18:22 -05:00
|
|
|
def remove_role(role_name, resource = nil)
|
|
|
|
role = self.class.role_class.find_by name: role_name, resource: resource
|
|
|
|
return if role.nil?
|
2019-02-01 23:27:42 -05:00
|
|
|
|
2019-02-01 23:18:22 -05:00
|
|
|
account_roles.where(role: role).update_all(deleted_at: Time.zone.now)
|
|
|
|
end
|
|
|
|
|
2018-12-11 22:15:03 -05:00
|
|
|
def can_access_sidekiq_web_interface?
|
|
|
|
is_superuser?
|
|
|
|
end
|
2019-01-31 19:47:44 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def generate_username
|
|
|
|
self.username = "noname_#{SecureRandom.hex(8)}" if username.nil?
|
|
|
|
end
|
2019-01-31 20:19:31 -05:00
|
|
|
|
|
|
|
def generate_guest_token
|
|
|
|
self.guest_token = SecureRandom.hex
|
|
|
|
end
|
2018-12-01 21:03:19 -05:00
|
|
|
end
|