RailsAdmin没有使用Pundit - Pundit :: AuthorizationNotPerformedError at / RailsAdmin :: MainController

时间:2016-07-13 12:59:10

标签: ruby-on-rails rails-admin pundit

我在Ruby on Rails应用程序中使用Devise和Pundit gems。 我尝试实施RailsAdmin,以便为我的非技术团队成员设置一个简单的管理界面,在URL' myapp.com/admin'上,但是转到此URL会引发以下情况错误:

  

Pundit :: AuthorizationNotPerformedError at /   RailsAdmin :: MainController

文件lib / pundit.rb在以下方法中引发此错误:



def verify_authorized
    raise AuthorizationNotPerformedError, self.class unless pundit_policy_authorized?
  end




我知道这与Pundit的政策有关,但无法找到完成更改的方式。

以下是我迄今为止所尝试的内容:

在我的Gemfile中,我安装了:

  

gem" rails_admin"

  

gem" rails_admin_pundit",:github => " sudosu / rails_admin_pundit"

然后,在我的终端,我跑了

  

'捆绑'

在我的初始化程序中,我有:



RailsAdmin.config do |config|
  ## == Devise ==
  config.authenticate_with do
    warden.authenticate! scope: :user
  end
  config.current_user_method(&:current_user)

  ## == Pundit ==
  config.authorize_with :pundit
end




在我的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.
  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters, if: :devise_controller?
  before_action :authenticate_user!
  include Pundit

  after_action :verify_authorized, except: :index, unless: :devise_controller?
  after_action :verify_policy_scoped, only: :index, unless: :devise_controller?

  rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

  def user_not_authorized
    flash[:alert] = "You are not authorized to perform this action."
    redirect_to(root_path)
  end

  def default_url_options
    { host: ENV['HOST'] || 'localhost:3000' }
  end

    private

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << [:first_name, :last_name, :bio, :email, :password, :password_confirmation, :photo, :photo_cache]
    devise_parameter_sanitizer.for(:account_update) << [:first_name, :last_name, :bio, :email, :password, :password_confirmation, :photo, :photo_cache]
  end

end
&#13;
&#13;
&#13;

最后,在我的policies / application_policy.rb中(请参阅最后的&#39; def rails_admin?&#39;方法),我有:

&#13;
&#13;
class ApplicationPolicy
  attr_reader :user, :record

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

  def index?
    false
  end

  def show?
    scope.where(:id => record.id).exists?
  end

  def create?
    false
  end

  def new?
    create?
  end

  def update?
    false
  end

  def edit?
    update?
  end

  def destroy?
    false
  end

  def scope
    Pundit.policy_scope!(user, record.class)
  end

  class Scope
    attr_reader :user, :scope

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

    def resolve
      scope
    end
  end

  def rails_admin?(action)
    case action
      when :dashboard
        user.admin?
      when :index
        user.admin?
      when :show
        user.admin?
      when :new
        user.admin?
      when :edit
        user.admin?
      when :destroy
        user.admin?
      when :export
        user.admin?
      when :history
        user.admin?
      when :show_in_app
        user.admin?
      else
        raise ::Pundit::NotDefinedError, "unable to find policy #{action} for #{record}."
    end
  end
end
&#13;
&#13;
&#13;

你对我遗失的东西有什么看法吗? 谢谢你们。

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题。 好像rails_admin_pundit gem中有一个小错误。 在rails_admin_pundit gem的GitHub存储库中进行的提交中发现了以下更改,但直到现在才被忽略!

在我的Gemfile中,而不是调用:

  

gem“rails_admin_pundit”,:github =&gt; “sudosu / rails_admin_pundit”

我致电:

  

gem“rails_admin_pundit”,:github =&gt; “萨米-阿玛尔/ rails_admin_pundit”

基本上,提交在

中添加了一行
  

LIB / rails_admin /扩展/权威人士/ authorization_adapter.rb

您可以在此页面上看到此微小变化的详细信息:https://github.com/Samy-Amar/rails_admin_pundit/commit/4017395e5e715e2f1fd95918bc4bb9b76d272b25

希望这会有所帮助!