【问题标题】:How to check for roles using rolify gem?如何使用 rolify gem 检查角色?
【发布时间】:2019-01-03 07:04:29
【问题描述】:

我有一个用户模型,其中有检查角色的方法。我总共有 5 到 6 个角色。超级管理员应该有权查看具有所有角色的用户。我正在使用rolify gem (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

【问题讨论】:

  • self.role 是错误背后的原因。使用 has_role?我希望您彻底阅读文档,这将对您有很大帮助

标签: ruby-on-rails rolify


【解决方案1】:

根据documentation,你应该调用has_role吗?方法。

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

【讨论】:

  • 如果我使用或调节它会引发错误..如何使用或调节它
  • sure.." 语法错误,意外的 tSTRING_BEG,期待 keyword_end ...ger-Client' || self.has_role?'Radial-Account-Manager' ... ^): app/models/ user.rb:110: 语法错误,意外 tSTRING_BEG,期待关键字_end" 这是我得到的错误
  • @AliaTabassum 我已经更新了答案,现在试试,如果现在可以正常工作,请接受答案。
猜你喜欢
  • 1970-01-01
  • 2015-12-10
  • 1970-01-01
  • 1970-01-01
  • 2015-12-29
  • 2019-06-17
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
相关资源
最近更新 更多