简体   繁体   中英

How to authorize from an especific policy in pundit (Rails)

Is there a way to specify the policy class in the authorization method in Pundit ? When you do

    authorize @user, :show

It uses the UserPolicy class, because @user is a User (Model) instance. Does anybody know a way to perform the authorize method on another policy class? like CustomerPolicy , without the existence of the Customer model class.

You can use a symbol instead of a model instance to invoke a "headless" policy (a policy without a backing model).

authorize :customer, :show
# or for a namespaced policy
authorize [:people, :customer] 

Another option is to set the policy on the model class:

class User < ActiveRecord::Base
  def self.policy_class
    CustomerPolicy
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM