scinote-web/app/models/user_role.rb

66 lines
1.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class UserRole < ApplicationRecord
validate :prevent_update, on: :update, if: :predefined?
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 }
validates :created_by, presence: true, unless: :predefined?
validates :last_modified_by, presence: true, unless: :predefined?
belongs_to :created_by, class_name: 'User', optional: true
belongs_to :last_modified_by, class_name: 'User', optional: true
has_many :user_assignments, dependent: :destroy
scope :predefined, -> { where(predefined: true) }
def self.owner_role
new(
name: I18n.t('user_roles.predefined.owner'),
permissions: PredefinedRoles::OWNER_PERMISSIONS,
predefined: true
)
end
def self.normal_user_role
new(
name: I18n.t('user_roles.predefined.normal_user'),
permissions: PredefinedRoles::NORMAL_USER_PERMISSIONS,
predefined: true
)
end
def self.technician_role
new(
name: I18n.t('user_roles.predefined.technician'),
permissions: PredefinedRoles::TECHNICIAN_PERMISSIONS,
predefined: true
)
end
def self.viewer_role
new(
name: I18n.t('user_roles.predefined.viewer'),
permissions: PredefinedRoles::VIEWER_PERMISSIONS,
predefined: true
)
end
def self.find_predefined_owner_role
predefined.find_by(name: UserRole.public_send('owner_role').name)
end
2021-04-30 19:26:06 +08:00
def owner?
name == I18n.t('user_roles.predefined.owner')
2021-04-30 19:26:06 +08:00
end
private
def prevent_update
errors.add(:base, I18n.t('user_roles.predefined.unchangable_error_message'))
end
end