Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 主动管理、设计和审核(审核::PolicyScopingNotPerformedError)_Ruby On Rails_Devise_Activeadmin_Pundit - Fatal编程技术网

Ruby on rails 主动管理、设计和审核(审核::PolicyScopingNotPerformedError)

Ruby on rails 主动管理、设计和审核(审核::PolicyScopingNotPerformedError),ruby-on-rails,devise,activeadmin,pundit,Ruby On Rails,Devise,Activeadmin,Pundit,我有一个现有的Rails应用程序,带有design验证用户模型和Pundit验证注册模型,该模型将用户链接到我的公司模型。User和Company都在公寓gem的公共模式中。我不怀疑公寓是问题的一部分,但我想我会提到它 我在AdminUser类中添加了Active Admin-我想将我的Admin用户与应用程序用户分开 如果我试图访问/admin或/admin/dashboard我会得到: Pundit::PolicyScopingNotPerformedError at /admin/user

我有一个现有的Rails应用程序,带有
design
验证
用户
模型和
Pundit
验证
注册
模型,该模型将
用户
链接到我的
公司
模型。
User
Company
都在公寓gem的公共模式中。我不怀疑公寓是问题的一部分,但我想我会提到它

我在AdminUser类中添加了Active Admin-我想将我的Admin用户与应用程序用户分开

如果我试图访问
/admin
/admin/dashboard
我会得到:

Pundit::PolicyScopingNotPerformedError at /admin/users
Pundit::PolicyScopingNotPerformedError
如果我尝试像
/admin/users
这样的模式,Pundit似乎会忽略活动的\u管理策略,而转到主应用程序策略。在我的例子中,应用程序抛出了一个异常,因为它期望
注册
而不是
管理员用户

如果我禁用:

##/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]
这一切工作,但然后我失去了权威等在我的主要应用程序

以下是我的代码要点:

以下是关于这个问题的相关帖子:

我想在这篇文章()中禁用Pundit,但如果能做到这一点就好了

更新

我已经解决了,但我仍然不知道这是否应该开箱即用,我有一些奇怪的问题导致了这一切。更新要点

我最终使用了:

还有一点:

下面是一些答案。我塞进了一个过滤器,迫使AA对AA内部的资源和集合进行授权。下一步是增加政策范围,但我的大脑现在太痛了

我还不得不添加另一个过滤器来绕过仪表板上的身份验证,因为它是无头的。到目前为止似乎有效

更新2


嗯。。。我想我说得太快了。只有当我以普通用户身份登录时,这一切才起作用。如果我再次注销,这一切都会崩溃。

你能用
跳过策略范围跳过活动管理控制器中的范围吗

见:

如果您正在控制器中使用
验证\u authorized
,但需要 有条件地绕过验证,您可以使用
跳过授权
。对于 绕过
验证策略范围
,使用
跳过策略范围


@dan tappin我想您已经根据您的评论找到了类似的解决方案,但以下是我最终添加到每个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_过滤器发生在inherrited_资源过滤器之后,所以我们可以访问“资源”,我们可以对其进行授权,就像您通常对任何模型实例进行授权一样。见:

首先需要策略作用域的原因是,正常的pundit安装需要在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
#app/controllers/application_controller.rb:
类ApplicationController

它期望调用策略范围模型以执行所有索引操作。Dashboard controller默认呈现索引操作,因此需要在过滤黑客之前执行此操作。

我可能会这样做,但这样会完全消除范围功能,也无法解决未加载策略的问题。嗯。。。我能做到这一点的唯一方法似乎是使用#1。从现在起,我得到了
Pundit::authorization notperformederror
错误。谢谢。除了在admin/{resource}.rb中过滤之前的第一个
,您可以在以下操作之后修改ApplicationController:
:验证\u策略\u作用域,仅::索引,除非::设计\u或\u验证\u控制器?
<代码>def设计或权威控制器?设计控制器self.class.name.match(/^Admin/)end
很好,很干净!