diff options
author | Azul <azul@leap.se> | 2014-07-10 12:13:30 +0200 |
---|---|---|
committer | Azul <azul@leap.se> | 2014-07-14 10:49:39 +0200 |
commit | 60052d15ca02b1c40ed265bed6515880d2851b8f (patch) | |
tree | e6946d2c25a04161c4f3003b1ef66ab9376938f4 /app/controllers/controller_extension/authentication.rb | |
parent | 091793265e23452890c6ca27fc64feb54df2ad0b (diff) |
clean up and simplify error responses and test code
Diffstat (limited to 'app/controllers/controller_extension/authentication.rb')
-rw-r--r-- | app/controllers/controller_extension/authentication.rb | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/app/controllers/controller_extension/authentication.rb b/app/controllers/controller_extension/authentication.rb index fae5145..687bc6e 100644 --- a/app/controllers/controller_extension/authentication.rb +++ b/app/controllers/controller_extension/authentication.rb @@ -27,26 +27,24 @@ module ControllerExtension::Authentication end def access_denied - respond_to do |format| - format.html do - redirect_to home_url, :alert => t(:not_authorized) - end - format.json do - render :json => {'error' => t(:not_authorized)}, status: :forbidden - end - end + respond_to_error :not_authorized, :forbidden, home_url end def login_required + # Warden will intercept the 401 response and call + # SessionController#unauthenticated instead. + respond_to_error :not_authorized_login, :unauthorized, login_url + end + + def respond_to_error(message, status=nil, redirect=nil) + message = t(message) if message.is_a?(Symbol) respond_to do |format| format.html do - redirect_to login_url, alert: t(:not_authorized_login) + redirect_to redirect, alert: message end format.json do - # Warden will intercept the 401 response and call - # SessionController#unauthenticated instead. - render json: {error: t(:not_authorized_login)}, - status: :unauthorized + status ||= :unprocessable_entity + render json: {error: message}, status: status end end end |