Handle AccessDenied in management sessions

We were raising a `CanCan::AcessDenied` and were getting a 500 Internal
Server Error.

I've chosen to do the same thing we do in the ApplicationController.
There are other options to handle this request, like redirecting to the
login page or returning a 401 Unauthorized HTTP status.
This commit is contained in:
Javi Martín
2019-04-24 15:44:05 +02:00
parent b33401ca0f
commit 286e0ca878
4 changed files with 23 additions and 11 deletions

View File

@@ -3,6 +3,7 @@ require "application_responder"
class ApplicationController < ActionController::Base
include HasFilters
include HasOrders
include AccessDeniedHandler
protect_from_forgery with: :exception
@@ -17,13 +18,6 @@ class ApplicationController < ActionController::Base
check_authorization unless: :devise_controller?
self.responder = ApplicationResponder
rescue_from CanCan::AccessDenied do |exception|
respond_to do |format|
format.html { redirect_to main_app.root_url, alert: exception.message }
format.json { render json: {error: exception.message}, status: :forbidden }
end
end
layout :set_layout
respond_to :html
helper_method :current_budget