我是Rails的新手,我对以下策略有问题(使用 专家): 我想比较两个对象。@record
和 @foo
,你可以在这里看到。
class BarPolicy < ApplicationPolicy
def show?
@record.foo_id == @foo
end
end
我找不到一个好的方法来传递第二个参数给pundit方法(@foo).
我想做这样的事情。
class BarsController < ApplicationController
def test
authorize bar, @foo, :show? # Throws ArgumentError
...
end
end
但是Pundit authorize方法只允许两个参数.有办法解决这个问题吗?
谢谢!我是Rails的新手。
我在下面找到了答案 此处.
这是我的方法。
加一个 pundit_user
作用于 ApplicationController
:
class ApplicationController < ActionController::Base
include Pundit
def pundit_user
CurrentContext.new(current_user, foo)
end
创建 CurrentContext
类,更新初始化Pundit方法。
/lib/pundit/current_context.rb
class CurrentContext
attr_reader :user, :foo
def initialize(user, foo)
@user = user
@foo = foo
end
end
更新初始化Pundit方法。
class ApplicationPolicy
attr_reader :user, :record, :foo
def initialize(context, record)
@user = context.user
@foo = context.foo
@record = record
end
end
依靠的不仅仅是当前的用户和领域模型,而是代码的味道,但万一真的需要,那么你可以使用一个自定义的查询方法,有任意数量的参数,如果不满足要求,就会引发异常。
class BarPolicy < ApplicationPolicy
def authorize_test?(foo)
raise Pundit::NotAuthorizedError, "not authorized to test" unless record.foo_id == foo
end
end
class BarsController < ApplicationController
def test
skip_authorization && BarPolicy.new(current_user, @record).authorize_test?(@foo)
...
end
end
异常的情况下,可以使用任意数量的参数自定义查询方法,并在不满足要求的情况下引发异常。skip_authorization &&
在以下情况下,不需要该部分 after_action :verify_authorized
不被使用,我只是想展示一个单行本,在这种情况下,可以使用它来摆脱未授权的异常,同时仍有授权操作的要求。