在 Rails 中使用 Pundit 限制整个控制器的 DRY 方法是什么?

Lee*_*lly 4 ruby authorization ruby-on-rails pundit

我正在使用带有 Rails 的Pundit,并且我有一个控制器,我需要完全限制特定用户角色。我的角色是“员工”和“消费者”。工作人员应该拥有对控制器的完全访问权限,但消费者应该没有访问权限。

有没有一种方法可以做到这一点,而不是一个一个地限制每个动作?

例如,这是我的政策:

class MaterialPolicy < ApplicationPolicy
  attr_reader :user, :material

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

  def index?
    user.staff?
  end

  def show?
    index?
  end

  def new?
    index?
  end

  def edit?
    index?
  end

  def create?
    index?
  end

  def update?
    create?
  end

  def destroy?
    update?
  end
end
Run Code Online (Sandbox Code Playgroud)

还有我的控制器:

class MaterialsController < ApplicationController
  before_action :set_material, only: [:show, :edit, :update, :destroy]

  # GET /materials
  def index
    @materials = Material.all
    authorize @materials
  end

  # GET /materials/1
  def show
    authorize @material
  end

  # GET /materials/new
  def new
    @material = Material.new
    authorize @material
  end

  # GET /materials/1/edit
  def edit
    authorize @material
  end

  # POST /materials
  def create
    @material = Material.new(material_params)
    authorize @material

    respond_to do |format|
      if @material.save
        format.html { redirect_to @material, notice: 'Material was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end

  # PATCH/PUT /materials/1
  def update
    authorize @material
    respond_to do |format|
      if @material.update(material_params)
        format.html { redirect_to @material, notice: 'Material was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  end

  # DELETE /materials/1
  def destroy
    authorize @material
    @material.destroy
    respond_to do |format|
      format.html { redirect_to materials_url, notice: 'Material was successfully destroyed.' }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_material
      @material = Material.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def material_params
      params.require(:material).permit(:name)
    end
end
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点,我不理解,或者 Pundit 的设计方式是否要求您明确?

max*_*max 5

第一步只是将调用移动到授权您的回调:

def set_material
  @material = Material.find(params[:id])
  authorize @material
end
Run Code Online (Sandbox Code Playgroud)

您还可以写下@material = authorize Material.find(params[:id])您的 Pundit 版本是否是最新的(以前的版本返回真/假而不是记录)。

Pundit 在您选择使用它的方式方面具有很大的灵活性。例如,您可以创建一个单独的无头策略

class StaffPolicy < ApplicationPolicy
  # the second argument is just a symbol (:staff) and is not actually used
  def initialize(user, symbol)
    @user = user
  end
  def access?
    user.staff?
  end
end
Run Code Online (Sandbox Code Playgroud)

然后在回调中使用它来授权整个控制器:

class MaterialsController < ApplicationController
  before_action :authorize_staff
  # ...

  def authorize_staff
    authorize :staff, :access?
  end
end
Run Code Online (Sandbox Code Playgroud)

或者你可以只使用继承或混合来干燥你的策略类:

class StaffPolicy < ApplicationPolicy
  %i[ show? index? new? create? edit? update? delete? ].each do |name|
    define_method name do
      user.staff?
    end
  end
end

class MaterialPolicy < StaffPolicy
  # this is how you would add additional restraints in a subclass
  def show?
    super && some_other_condition
  end
end
Run Code Online (Sandbox Code Playgroud)

Pundit 毕竟只是普通的老式 Ruby OOP。