adding custom message argument to unauthorized! method - closes #18
This commit is contained in:
		
							parent
							
								
									67416532f4
								
							
						
					
					
						commit
						ef22de689b
					
				@ -1,3 +1,6 @@
 | 
				
			|||||||
 | 
					* Adding custom message argument to unauthorized! method (thanks tjwallace) - see issue #18
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
1.0.1 (Dec 14, 2009)
 | 
					1.0.1 (Dec 14, 2009)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* Adding :class option to load_resource so one can customize which class to use for the model - see issue #17
 | 
					* Adding :class option to load_resource so one can customize which class to use for the model - see issue #17
 | 
				
			||||||
 | 
				
			|||||||
@ -66,7 +66,7 @@ If the user authorization fails, a CanCan::AccessDenied exception will be raised
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  class ApplicationController < ActionController::Base
 | 
					  class ApplicationController < ActionController::Base
 | 
				
			||||||
    rescue_from CanCan::AccessDenied do |exception|
 | 
					    rescue_from CanCan::AccessDenied do |exception|
 | 
				
			||||||
      flash[:error] = "Sorry, you are not allowed to access that page."
 | 
					      flash[:error] = exception.message
 | 
				
			||||||
      redirect_to root_url
 | 
					      redirect_to root_url
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
				
			|||||||
@ -123,24 +123,22 @@ module CanCan
 | 
				
			|||||||
    #     unauthorized! if cannot? :read, @article
 | 
					    #     unauthorized! if cannot? :read, @article
 | 
				
			||||||
    #   end
 | 
					    #   end
 | 
				
			||||||
    # 
 | 
					    # 
 | 
				
			||||||
    # You can rescue from the exception in the controller to specify
 | 
					    # The unauthorized! method accepts an optional argument which sets the
 | 
				
			||||||
    # the user experience.
 | 
					    # message of the exception.
 | 
				
			||||||
 | 
					    # 
 | 
				
			||||||
 | 
					    # You can rescue from the exception in the controller to define the behavior.
 | 
				
			||||||
    # 
 | 
					    # 
 | 
				
			||||||
    #   class ApplicationController < ActionController::Base
 | 
					    #   class ApplicationController < ActionController::Base
 | 
				
			||||||
    #     rescue_from CanCan::AccessDenied, :with => :access_denied
 | 
					    #     rescue_from CanCan::AccessDenied do |exception|
 | 
				
			||||||
    #   
 | 
					    #       flash[:error] = exception.message
 | 
				
			||||||
    #     protected
 | 
					 | 
				
			||||||
    #   
 | 
					 | 
				
			||||||
    #     def access_denied
 | 
					 | 
				
			||||||
    #       flash[:error] = "Sorry, you are not allowed to access that page."
 | 
					 | 
				
			||||||
    #       redirect_to root_url
 | 
					    #       redirect_to root_url
 | 
				
			||||||
    #     end
 | 
					    #     end
 | 
				
			||||||
    #   end
 | 
					    #   end
 | 
				
			||||||
    # 
 | 
					    # 
 | 
				
			||||||
    # See the load_and_authorize_resource method to automatically add
 | 
					    # See the load_and_authorize_resource method to automatically add
 | 
				
			||||||
    # the "unauthorized!" behavior to a RESTful controller's actions.
 | 
					    # the "unauthorized!" behavior to a RESTful controller's actions.
 | 
				
			||||||
    def unauthorized!
 | 
					    def unauthorized!(message = "You are not authorized to access this page.")
 | 
				
			||||||
      raise AccessDenied, "You are unable to access this page."
 | 
					      raise AccessDenied, message
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    # Creates and returns the current user's ability. You generally do not invoke
 | 
					    # Creates and returns the current user's ability. You generally do not invoke
 | 
				
			||||||
 | 
				
			|||||||
@ -9,10 +9,16 @@ describe CanCan::ControllerAdditions do
 | 
				
			|||||||
    @controller_class.send(:include, CanCan::ControllerAdditions)
 | 
					    @controller_class.send(:include, CanCan::ControllerAdditions)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  it "should read from the cache with request uri as key and render that text" do
 | 
					  it "should raise access denied with default message when calling unauthorized!" do
 | 
				
			||||||
    lambda {
 | 
					    lambda {
 | 
				
			||||||
      @controller.unauthorized!
 | 
					      @controller.unauthorized!
 | 
				
			||||||
    }.should raise_error(CanCan::AccessDenied)
 | 
					    }.should raise_error(CanCan::AccessDenied, "You are not authorized to access this page.")
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  it "should raise access denied with custom message when calling unauthorized!" do
 | 
				
			||||||
 | 
					    lambda {
 | 
				
			||||||
 | 
					      @controller.unauthorized! "Access denied!"
 | 
				
			||||||
 | 
					    }.should raise_error(CanCan::AccessDenied, "Access denied!")
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  it "should have a current_ability method which generates an ability for the current user" do
 | 
					  it "should have a current_ability method which generates an ability for the current user" do
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user