【问题标题】:Pundit Policy Restrict Index action and view to specific rolesPundit 政策限制索引操作和查看特定角色
【发布时间】:2017-10-02 19:08:17
【问题描述】:

我正在尝试使用此权威政策来禁止具有临床医生角色的用户访问患者控制器上的索引操作。范围部分目前正在按我希望的方式工作,但是根据当前编写的政策,我仍然可以以临床医生用户的身份访问 /patients。我究竟做错了什么?谢谢你的帮助!

这是我的角色定义:

enum role: { staff: 0, clinician: 1, admin: 2 }

患者政策

class PatientPolicy < ApplicationPolicy
  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user  = user
      @scope = scope
    end

    def resolve
      if user.admin?
        scope.all
      else
        scope.joins(:user).merge(User.where(university: user.university))
      end
    end
  end

def index?
  user.staff? or user.admin?
end

end

患者控制者:

def index
    @patients = policy_scope(Patient)
end
[rest of controller]

【问题讨论】:

    标签: ruby-on-rails pundit


    【解决方案1】:

    自己的目标:我需要将授权行添加到患者控制器中的索引操作中:

    def index
        authorize Patient
        @patients = policy_scope(Patient)
    end
    

    【讨论】:

      猜你喜欢
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 2019-12-13
      • 2014-08-24
      相关资源
      最近更新 更多