use a controller method to handle cancan denials
authorChris Flipse <cflipse@gmail.com>
Sat, 9 Jun 2018 20:20:21 +0000 (16:20 -0400)
committerChris Flipse <cflipse@gmail.com>
Sun, 17 Jun 2018 17:57:06 +0000 (13:57 -0400)
This will let controllers override for specific circumstances

app/controllers/application_controller.rb

index 84adc1a32d287d4e34b4b0544f2f52efd72f8d0d..54d5835bbc5868c87d5f1ffb10a2c4157a0dd651 100644 (file)
@@ -4,6 +4,8 @@ class ApplicationController < ActionController::Base
 
   protect_from_forgery :with => :exception
 
 
   protect_from_forgery :with => :exception
 
+  rescue_from CanCan::AccessDenied, :with => :deny_access
+
   before_action :fetch_body
   around_action :better_errors_allow_inline, :if => proc { Rails.env.development? }
 
   before_action :fetch_body
   around_action :better_errors_allow_inline, :if => proc { Rails.env.development? }
 
@@ -468,13 +470,17 @@ class ApplicationController < ActionController::Base
     raise
   end
 
     raise
   end
 
-  rescue_from CanCan::AccessDenied do |exception|
-    raise "Access denied on #{exception.action} #{exception.subject.inspect}"
-    # ...
+  def current_ability
+    Ability.new(current_user, current_token)
   end
 
   end
 
-  def current_ability
-    @current_ability ||= Ability.new(current_user, current_token)
+  def deny_access(exception)
+    if current_user
+      raise "Access denied on #{exception.action} #{exception.subject.inspect}"
+      # ...
+    else
+      require_user
+    end
   end
 
   private
   end
 
   private