我有一个现有的Rails应用程序,它有Devise认证User模型,Pundit根据Enrollment模型进行身份验证,该模型将User链接到Company模型。User和Company都在公寓创业板的公共模式中。我不怀疑公寓是问题的一部分,但我想我应该提一下。
我在AdminUser类中添加了Active Admin --我希望将我的管理用户与应用程序用户分开。
如果我试图访问/admin或/admin/dashboard,就会得到:
Pundit::PolicyScopingNotPerformedError at /admin/users
Pundit::PolicyScopingNotPerformedError如果我尝试一下我的模型,比如/admin/users,专家似乎忽略了active_admin策略,转而使用主要的应用程序策略。在我的例子中,应用程序抛出了一个异常,因为它期待的是Enrollment和AdminUser。
如果我禁用:
##/config/initializers/active_admin.rb
config.authorization_adapter = ActiveAdmin::PunditAdapter
##/controllers/application_controller
after_action :verify_authorized, except: [:landing, :dashboard], unless: :devise_controller?
after_action :verify_policy_scoped, only: [:index]所有的工作,但然后我失去了专家等在我的主要应用程序。
下面是我的代码要点:
https://gist.github.com/jasper502/4b2f1b8b6f21a26c64a5
以下是有关这一问题的相关帖子:
https://gorails.com/forum/using-pundit-with-activeadmin
我想在这篇文章(你能用设计和主动管理来禁用专家吗?)中一起禁用所有的专家,但这将是一个很好的工作。
更新
我有工作,但我仍然不知道这是否应该发挥作用,我有一些奇怪的问题导致了这一切。Gist更新了。
最后我用了:
https://viget.com/extend/8-insanely-useful-activeadmin-customizations
还有一点:
下面给出一些答案。我在一个过滤器里钉上角,迫使AA调用授权,授权AA内部的资源和集合。接下来是增加政策范围,但现在我的脑子太痛了。
我还必须添加另一个过滤器来绕过仪表板上的身份验证,因为它是无头的。到目前为止似乎还有效。
更新2
嗯..。我想我说得太早了。只有当我作为一个普通的User登录时,这一切才能起作用--如果我注销了,所有这些都会再次崩溃。
发布于 2016-01-24 20:16:13
@dan我想您已经根据您的评论找到了一个类似的解决方案,但是下面是我最后在我的AA模型注册中添加的内容:
#app/admin/user.rb
ActiveAdmin.register User do
controller do
before_filter :authorize_index, only: :index
def authorize_index
policy_scope(User)
end
before_filter :authorize_show_edit_destroy, only: [:show, :edit, :destroy]
def authorize_show_edit_destroy
authorize resource
end
end
end基本上,这利用了使用普通rails before_filter语法在控制器作用域中执行的能力,从而将执行限制为:only。然后,因为before_filter发生在inherrited_resources过滤器之后,所以我们可以访问“资源”,我们可以对它进行授权,就像通常对任何模型实例一样。请参阅:https://github.com/activeadmin/activeadmin/issues/1108#issuecomment-14711733
首先,需要策略范围的原因是,正常的专家安装需要application_controller.rb中的以下内容
#app/controllers/application_controller.rb:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
include Pundit
protect_from_forgery with: :exception
#before_action :authenticate_is_admin!
after_action :verify_authorized, except: [:index, :dashboard], unless: :devise_controller?
after_action :verify_policy_scoped, only: :index, unless: :devise_controller?
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
private
def authenticate_admin!
redirect_to new_user_session_path unless current_user.admin?
end
private
def pundit_user
current_user
end
private
def user_not_authorized
flash[:error] = "You are not authorized to perform this action."
redirect_to(request.referrer || new_user_session_path)
end
end它期望对策略范围的调用--所有索引操作的模型。仪表板控制器在默认情况下呈现索引操作,因此需要进行此before_filter攻击。
发布于 2016-01-07 21:52:19
您能跳过使用skip_policy_scope的活动管理控制器的作用域吗?
见文档
如果在控制器中使用
verify_authorized,但需要有条件地绕过验证,则可以使用skip_authorization。要绕过verify_policy_scoped,请使用skip_policy_scope。
https://stackoverflow.com/questions/34664645
复制相似问题