ruby-on-rails - rails 4 + 专家 : join model authorization in has_many :through association

标签 ruby-on-rails ruby-on-rails-4 authorization has-many-through pundit

在我的 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 的角色——Owner、Editor 或 Viewer > 对于给定的 calendar

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

我使用 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

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

  • 如果用户是日历的所有者,那么他可以执行IndexShowCreateNew编辑更新销毁 对此日历管理的操作。
  • 但是,如果用户是日历的编辑者或查看者,那么他只能做两件事:1. 执行 Index 操作以查看日历的所有用户,以及 2. 执行 销毁 对他自己的行政当局“离开日历”的行动。

我的问题如下:

  • 管理实例仅作为 用户日历 之间的连接存在,如上所述。
  • 因此,要对 Administration 实例执行操作,我需要三个上下文:administration_iduser_idcalendar_id
  • 但是,Pundit 只接受策略中的两个上下文,通常是 user 和实际的 record(此处为管理)。

在 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 关联是否构成上述“非常罕见的情况”之一,或者是否有更简单的方法来为我的Administration 加入模型实现授权?

最佳答案

我认为这不是特例。 打破良好做法的原因是什么,在您提供的链接中如此明确说明?

您可以在 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(我错了,说 Calendar policy is 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

关于ruby-on-rails - rails 4 + 专家 : join model authorization in has_many :through association,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32528462/

相关文章:

ruby-on-rails - 两个表上的 Rails where 子句

cakephp 3 中的授权和 ACL

database - 如何扩展此查询

c - 如何将对象的安全描述符重置为默认值?

javascript - 同时使用 dom 和 sDom 选项

css - 将学区 Logo 与 URL 学区 ID 相匹配

ruby-on-rails - 如何使用 Rails 4 在 savon 2.7 中传递安全 header

ruby-on-rails - 玩 Scala : Scala object as DAO

ruby-on-rails - .select 和或 .where 是否负责在 Rails 中引起 N+1 查询?

ruby-on-rails - rescue_from ActionController::RoutingError 不起作用