权威策略限制索引操作和查看特定角色

时间:2017-10-02 19:08:18

标签: ruby-on-rails pundit

我正在尝试使用此权威政策,不允许具有角色临床医生的用户访问患者控制器上的索引操作。范围部分目前正如我所希望的那样工作,但根据目前编写的政策,我仍然可以作为临床医生用户访问/患者。我究竟做错了什么?谢谢你的帮助!

以下是我的角色定义:

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]

1 个答案:

答案 0 :(得分:1)

自己的目标:我需要在我的病人控制器中为我的索引操作添加授权行:

def index
    authorize Patient
    @patients = policy_scope(Patient)
end
相关问题