如何使用rolify gem检查角色?

时间:2019-01-03 07:04:30

标签: ruby-on-rails rolify

我有一个用户模型,其中有用于检查角色的方法。我一共有5到6个角色。超级管理员应该有权查看具有所有角色的用户。我正在使用rolify宝石(has_role?)检查管理员角色。有人可以指导我如何使用它吗?到目前为止,我遇到了undefined method include?错误。

class User < ActiveRecord::Base
  extend FriendlyId
  self.table_name = "DIM_USER"
  self.primary_key = "user_id"
  self.sequence_name = 'DIM_USER_ID_SEQ'

  rolify
  devise :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable, :timeoutable

  before_validation :strip_whitespace, :only => [:email]
  default_scope {where(clean_up_flag: false)}
  has_many :store_user_assignments
  has_many :stores, through: :store_user_assignments
  has_and_belongs_to_many :clients, join_table: :clients_users
  has_many :store_user_assignments
  has_many :stores, through: :store_user_assignments
  belongs_to :role

  before_save {self.email = email.downcase}
  before_save :update_full_name
  after_create :create_slug

  friendly_id :slug, use: :slugged

  NAME_MIN_LENGTH = 1
  NAME_MAX_LENGTH = 100
  EMAIL_MAX_LENGTH = 100
  NAME_RANGE = NAME_MIN_LENGTH..NAME_MAX_LENGTH

  validate :password_check
  validates :encrypted_password, presence: true

  scope :admin, -> {joins(:users_roles, :DIM_ROLE).where("users_roles.role_id = DIM_ROLE.role_id AND DIM_ROLE.name = 'super_administrator'").order(:last_name)}
  scope :pmt_ptl_accnt_manager, -> {joins(:users_roles, :DIM_ROLE).where("users_roles.role_id = DIM_ROLE.role_id AND DIM_ROLE.name = 'Portal-Account-Manager-Client'").order(:last_name)}
  scope :inactive_pmt_ptl_accnt_manager_with_no_stores, -> {joins(:users_roles, :DIM_ROLE).where("users.active=? AND users_roles.role_id = DIM_ROLE.role_id AND DIM_ROLE.name = ? AND users.user_id NOT IN (select user_id from stores_users)", false, 'Portal-Account-Manager-Client').order(:last_name)}

  def active_for_authentication?
    super && self.active?
  end

  def inactive_message
    :invalid
  end

  def update_full_name
    self.full_name = "#{first_name} #{last_name}"
  end

  def admin?
    self.has_role?(:super_administrator)
  end

  def vt_user?
    self.has_role?(:Virtual-Terminal-User)
  end


  def pmt_ptl_accnt_manager?
    self.role.include?(Role.where(:name => 'Portal-Account-Manager-Client').first) ||
        self.role.include?(Role.where(:name => 'Radial-Account-Manager').first)
  end

  def radial_account_manager?
    self.role.include?(Role.where(:name => 'Radial-Account-Manager').first)
  end

  def payments_portal_readonly?
    self.role.include?(Role.where(:name => 'Portal-Account-Manager-Read-Only-Client').first)
  end

  def search_user?
    self.role.include?(Role.where(:name => 'Transaction-Search-Only').first)
  end

  def radial_readonly?
    self.role.include?(Role.where(:name => 'Radial_ReadOnly').first)
  end

end

1 个答案:

答案 0 :(得分:1)

根据documentation,您应该致电has_role?方法。

 def pmt_ptl_accnt_manager?
    self.has_role?('Portal-Account-Manager-Client') || self.has_role?('Radial-Account-Manager')
 end