Module | CanCan::ControllerAdditions::ClassMethods |
In: |
lib/cancan/controller_additions.rb
|
Sets up a before filter which authorizes the resource using the instance variable. For example, if you have an ArticlesController it will check the @article instance variable and ensure the user can perform the current action on it. Under the hood it is doing something like the following.
authorize!(params[:action].to_sym, @article || Article)
Call this method directly on the controller class.
class BooksController < ApplicationController authorize_resource end
If you pass in the name of a resource which does not match the controller it will assume it is a parent resource.
class BooksController < ApplicationController authorize_resource :author authorize_resource :book end
Here it will authorize :show, @author on every action before authorizing the book.
That first argument is optional and will default to the singular name of the controller. A hash of options (see below) can also be passed to this method to further customize it.
See load_and_authorize_resource to automatically load the resource too.
Options:
Add this to a controller to ensure it performs authorization through authorized! or authorize_resource call. If neither of these authorization methods are called, a CanCan::AuthorizationNotPerformed exception will be raised. This is normally added to the ApplicationController to ensure all controller actions do authorization.
class ApplicationController < ActionController::Base check_authorization end
See skip_authorization_check to bypass this check on specific controller actions.
Options:
check_authorization :if => :admin_controller?
check_authorization :unless => :devise_controller?
Sets up a before filter which loads and authorizes the current resource. This performs both load_resource and authorize_resource and accepts the same arguments. See those methods for details.
class BooksController < ApplicationController load_and_authorize_resource end
Sets up a before filter which loads the model resource into an instance variable. For example, given an ArticlesController it will load the current article into the @article instance variable. It does this by either calling Article.find(params[:id]) or Article.new(params[:article]) depending upon the action. The index action will automatically set @articles to Article.accessible_by(current_ability).
If a conditions hash is used in the Ability, the new and create actions will set the initial attributes based on these conditions. This way these actions will satisfy the ability restrictions.
Call this method directly on the controller class.
class BooksController < ApplicationController load_resource end
A resource is not loaded if the instance variable is already set. This makes it easy to override the behavior through a before_filter on certain actions.
class BooksController < ApplicationController before_filter :find_book_by_permalink, :only => :show load_resource private def find_book_by_permalink @book = Book.find_by_permalink!(params[:id) end end
If a name is provided which does not match the controller it assumes it is a parent resource. Child resources can then be loaded through it.
class BooksController < ApplicationController load_resource :author load_resource :book, :through => :author end
Here the author resource will be loaded before each action using params[:author_id]. The book resource will then be loaded through the @author instance variable.
That first argument is optional and will default to the singular name of the controller. A hash of options (see below) can also be passed to this method to further customize it.
See load_and_authorize_resource to automatically authorize the resource too.
Options:
load_resource :find_by => :permalink # will use find_by_permalink!(params[:id])
load_resource :id_key => :url # will use find(params[:url])
load_resource :collection => [:sort, :list]
load_resource :new => :build
Call this in the class of a controller to skip the check_authorization behavior on the actions.
class HomeController < ApplicationController skip_authorization_check :only => :index end
Any arguments are passed to the before_filter it triggers.
Skip the authorization behavior of CanCan. This is useful when using load_and_authorize_resource but want to only do loading on certain actions. You can pass :only and :except options to specify which actions to skip the effects on. It will apply to all actions by default.
class ProjectsController < ApplicationController load_and_authorize_resource skip_authorize_resource :only => :index end
You can also pass the resource name as the first argument to skip that resource.
Skip both the loading and authorization behavior of CanCan for this given controller. This is primarily useful to skip the behavior of a superclass. You can pass :only and :except options to specify which actions to skip the effects on. It will apply to all actions by default.
class ProjectsController < SomeOtherController skip_load_and_authorize_resource :only => :index end
You can also pass the resource name as the first argument to skip that resource.
Skip the loading behavior of CanCan. This is useful when using load_and_authorize_resource but want to only do authorization on certain actions. You can pass :only and :except options to specify which actions to skip the effects on. It will apply to all actions by default.
class ProjectsController < ApplicationController load_and_authorize_resource skip_load_resource :only => :index end
You can also pass the resource name as the first argument to skip that resource.