2021-02-04 21:08:20 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UserRole < ApplicationRecord
|
2021-09-22 21:28:32 +08:00
|
|
|
validate :prevent_update, on: :update, if: :predefined?
|
2021-02-04 21:08:20 +08:00
|
|
|
validates :name,
|
|
|
|
presence: true,
|
|
|
|
length: { minimum: Constants::NAME_MIN_LENGTH,
|
|
|
|
maximum: Constants::NAME_MAX_LENGTH },
|
|
|
|
uniqueness: { case_sensitive: false }
|
|
|
|
validates :permissions, presence: true, length: { minimum: 1 }
|
2021-02-18 16:54:53 +08:00
|
|
|
validates :created_by, presence: true, unless: :predefined?
|
|
|
|
validates :last_modified_by, presence: true, unless: :predefined?
|
2021-02-04 21:08:20 +08:00
|
|
|
|
2021-09-30 17:32:11 +08:00
|
|
|
belongs_to :created_by, class_name: 'User', optional: true
|
|
|
|
belongs_to :last_modified_by, class_name: 'User', optional: true
|
2021-02-04 21:08:20 +08:00
|
|
|
has_many :user_assignments, dependent: :destroy
|
2021-02-18 16:54:53 +08:00
|
|
|
|
2021-11-16 04:36:06 +08:00
|
|
|
scope :predefined, -> { where(predefined: true) }
|
|
|
|
|
2021-02-18 16:54:53 +08:00
|
|
|
def self.owner_role
|
|
|
|
new(
|
|
|
|
name: I18n.t('user_roles.predefined.owner'),
|
2021-09-30 17:32:11 +08:00
|
|
|
permissions: PredefinedRoles::OWNER_PERMISSIONS,
|
2021-02-18 16:54:53 +08:00
|
|
|
predefined: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.normal_user_role
|
|
|
|
new(
|
|
|
|
name: I18n.t('user_roles.predefined.normal_user'),
|
2021-09-30 17:32:11 +08:00
|
|
|
permissions: PredefinedRoles::NORMAL_USER_PERMISSIONS,
|
2021-02-18 16:54:53 +08:00
|
|
|
predefined: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.technician_role
|
|
|
|
new(
|
|
|
|
name: I18n.t('user_roles.predefined.technician'),
|
2021-09-30 17:32:11 +08:00
|
|
|
permissions: PredefinedRoles::TECHNICIAN_PERMISSIONS,
|
2021-02-18 16:54:53 +08:00
|
|
|
predefined: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.viewer_role
|
|
|
|
new(
|
|
|
|
name: I18n.t('user_roles.predefined.viewer'),
|
2021-09-30 17:32:11 +08:00
|
|
|
permissions: PredefinedRoles::VIEWER_PERMISSIONS,
|
2021-03-01 21:28:21 +08:00
|
|
|
predefined: true
|
2021-02-18 16:54:53 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-04-30 19:26:06 +08:00
|
|
|
def owner?
|
2021-09-15 20:39:47 +08:00
|
|
|
name == I18n.t('user_roles.predefined.owner')
|
2021-04-30 19:26:06 +08:00
|
|
|
end
|
|
|
|
|
2021-02-18 16:54:53 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def prevent_update
|
2021-09-22 21:28:32 +08:00
|
|
|
errors.add(:base, I18n.t('user_roles.predefined.unchangable_error_message'))
|
2021-02-18 16:54:53 +08:00
|
|
|
end
|
2021-02-04 21:08:20 +08:00
|
|
|
end
|