[英]Pundit Policy Scope Not implemented properly
I have a model called Referral Request and associated table and views. 我有一个名为Referral Request的模型以及关联的表和视图。 I have three user roles defined using enums:
我使用枚举定义了三个用户角色:
enum role: { staff: 0, clinician: 1, admin: 2 }
Staff users belong to universities, and universities have many staff users. 员工用户属于大学,而大学有很多员工用户。 My intention in many parts of my application is to use pundit policies to only show staff users records that are associated with other users from their university.
我打算在应用程序的许多部分中使用专家策略仅向员工用户显示与大学中其他用户相关联的记录。 I am trying to do that for referral requests for example, but I have something configured incorrectly, because it shows any given user all referral requests regardless of whether they were created by another user who belongs to their university or not.
例如,我尝试对推荐请求执行此操作,但是我配置不正确,因为它会向任何给定用户显示所有推荐请求,无论它们是否由属于其大学的另一个用户创建。 What am I doing wrong?
我究竟做错了什么?
Referral Request Policy: 推荐请求政策:
class ReferralRequestPolicy < 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
Referral Request Model: 推荐请求模型:
class ReferralRequest < ApplicationRecord
belongs_to :user, -> { where role: :staff }
belongs_to :patient
has_many :dispatches
has_many :clinicians, through: :dispatches
has_and_belongs_to_many :languages
has_and_belongs_to_many :races
has_and_belongs_to_many :genders
validates :user_id, presence: true
enum status: { created: 0, sent: 1, shared: 2, closed_under_care: 3, closed_not_seeking_care: 4, closed_unresponsive: 5 }
end
staff user concern: 工作人员用户关注:
require 'active_support/concern'
module StaffUser
extend ActiveSupport::Concern
included do
belongs_to :university
has_many :patients
has_many :referral_requests
validates :university_id, presence: true, if: :staff?
end
class_methods do
end
end
University Model 大学模式
class University < ApplicationRecord
has_many :staffs, -> { where role: :staff}, class_name: "User"
has_many :clinicians, through: :lists
has_many :whitelists
belongs_to :market
validates :market_id, presence: true
end
I forgot to change my index action in my Referral Requests Controller. 我忘记了在“推荐请求控制器”中更改索引操作。 This resolved it.
这样解决了。 Details.
细节。
def index
@referral_requests = policy_scope(ReferralRequest)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.