Rails 4 + Pundit : 加入模型授权 has_many :through association

Rails 4 + Pundit : join model authorization in has_many :through association

在我的 Rails 应用程序中,有 3 个模型,由 has_many :through 关联定义:

class User < ActiveRecord::Base
  has_many :administrations
  has_many :calendars, through: :administrations
end

class Calendar < ActiveRecord::Base
  has_many :administrations
  has_many :users, through: :administrations
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

加入 Administration 模型有一个 role 属性,我们用它来定义给定 user 给定 [=18] 的角色——所有者、编辑者或查看者=].

事实上,在应用程序中,用户可以是日历的所有者,例如另一个日历的查看者。

我使用 Devise 实现了身份验证。

我也开始使用 Pundit 实现授权:授权目前正在为 calendars 工作,其中 users 可以根据他们的 roles.

执行不同的操作

更新:这里是当前的 CalendarPolicy:

class CalendarPolicy < ApplicationPolicy

  attr_reader :user, :calendar

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

  def index?
    user.owner?(calendar) || user.editor?(calendar) || user.viewer?(calendar)
  end

  def create?
    true
  end

  def show?
    user.owner?(calendar) || user.editor?(calendar) || user.viewer?(calendar)
  end

  def update?
    user.owner?(calendar) || user.editor?(calendar)
  end

  def edit?
    user.owner?(calendar) || user.editor?(calendar)
  end

  def destroy?
    user.owner?(calendar)
  end

end

现在,我想为 Administration 模型实施一个 Pundit 策略,如下所示:

我的问题如下:

在 Pundit 的 GitHub 页面上,在 Additional context 部分,我们可以阅读以下内容:

Additional context

Pundit strongly encourages you to model your application in such a way that the only context you need for authorization is a user object and a domain model that you want to check authorization for. If you find yourself needing more context than that, consider whether you are authorizing the right domain model, maybe another domain model (or a wrapper around multiple domain models) can provide the context you need.

Pundit does not allow you to pass additional arguments to policies for precisely this reason.

However, in very rare cases, you might need to authorize based on more context than just the currently authenticated user. Suppose for example that authorization is dependent on IP address in addition to the authenticated user. In that case, one option is to create a special class which wraps up both user and IP and passes it to the policy.

has_many :through 关联是否构成上述 "very rare cases" 之一,或者是否有更简单的方法来为我的 Administration 加入模型实施授权?

是的,这是极少数情况之一。

# calendar controller show
@calendar = something    
@administration = @calendar.administration_of_current_user
authorize CalendarAdministrationContext

# pundit CalendarAdministrationContext
def initialize(user, administration, calendar)
  @user = user
  @administration = administration
  @calendar = calendar
end

我认为这不是特例。 在您提供的 link 中明确说明的违反良好做法的原因是什么?

您可以在 CalendarController 中添加 add_vieweradd_editorremove_viewerremove_editor 操作。

前两个可以用你的旧CalendarPolicy授权。

class CalendarPolicy
  # old staff here

  def add_viewer?
    user.is_owner?(calendar)
  end

  def add_editor?
    user.is_owner?(calendar)
  end
end

虽然您需要 AdministrationPolicy 来进行删除操作(我错了,说日历政策是enogh):

class AdministrationPolicy
  attr_reader :user, :authorization

  def remove_viwer?
    authorization.viewer? and authorization.user == user
  end

  def remove_editor?
    authorization.editor? and authorization.user == user
  end
end