I'm using Rails 5.2, Pundit 1.1, and rails_admin 1.2
I have the following in my application_controller.rb:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include Pundit
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
private
def user_not_authorized(exception)
flash[:alert] = "You are not authorized to perform this action."
redirect_to root_path
end
end
I have the following in my application.policy.rb:
class ApplicationPolicy
.
.
.
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
In my config/initializers/rails_admin.rb I have the following:
RailsAdmin.config do |config|
### Popular gems integration
## == Devise ==
config.authenticate_with do
warden.authenticate! scope: :user
end
config.current_user_method(&:current_user)
## == Pundit ==
config.authorize_with :pundit
end
module RailsAdmin
module Extensions
module Pundit
class AuthorizationAdapter
def authorize(action, abstract_model = nil, model_object = nil)
record = model_object || abstract_model && abstract_model.model
if action && !policy(record).send(*action_for_pundit(action))
raise ::Pundit::NotAuthorizedError.new("not allowed to #{action} this #{record}")
end
@controller.instance_variable_set(:@_pundit_policy_authorized, true)
end
def authorized?(action, abstract_model = nil, model_object = nil)
record = model_object || abstract_model && abstract_model.model
policy(record).send(*action_for_pundit(action)) if action
end
def action_for_pundit(action)
[:rails_admin?, action]
end
end
end
end
end
If a user is an admin then they can access the admin dashboard. However, if a user isn't an admin then they should be redirected to the home page with a flash message.
When I try to access the Rails Admin dashboard for a non-admin user I get the following:
Why is ApplicationController not rescuing the Pundit::NotAuthorizedError?
I have created a sample app that reproduces this error: https://github.com/helphop/sampleapp