Module: CanCan::ControllerAdditions::ClassMethods
- Defined in:
- lib/cancan/controller_additions.rb
Instance Method Summary collapse
-
#authorize_resource(*args) ⇒ Object
Sets up a before filter which authorizes the resource using the instance variable.
- #cancan_resource_class ⇒ Object
- #cancan_skipper ⇒ Object
-
#check_authorization(options = {}) ⇒ Object
Add this to a controller to ensure it performs authorization through
authorize
! orauthorize_resource
call. -
#load_and_authorize_resource(*args) ⇒ Object
Sets up a before filter which loads and authorizes the current resource.
-
#load_resource(*args) ⇒ Object
Sets up a before filter which loads the model resource into an instance variable.
-
#skip_authorization_check(*args) ⇒ Object
Call this in the class of a controller to skip the check_authorization behavior on the actions.
-
#skip_authorize_resource(*args) ⇒ Object
Skip the authorization behavior of CanCan.
-
#skip_load_and_authorize_resource(*args) ⇒ Object
Skip both the loading and authorization behavior of CanCan for this given controller.
-
#skip_load_resource(*args) ⇒ Object
Skip the loading behavior of CanCan.
Instance Method Details
#authorize_resource(*args) ⇒ Object
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.
(params[:action].to_sym, @article || Article)
Call this method directly on the controller class.
class BooksController < ApplicationController
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
:author
: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:
- :
only
-
Only applies before filter to given actions.
- :
except
-
Does not apply before filter to given actions.
- :
singleton
-
Pass
true
if this is a singleton resource through ahas_one
association. - :
parent
-
True or false depending on if the resource is considered a parent resource. This defaults to
true
if a resource name is given which does not match the controller. - :
class
-
The class to use for the model (string or constant). This passed in when the instance variable is not set. Pass
false
if there is no associated class for this resource and it will use a symbol of the resource name. - :
instance_name
-
The name of the instance variable for this resource.
- :
id_param
-
Find using a param key other than :id. For example:
load_resource :id_param => :url # will use find(params[:url])
- :
through
-
Authorize conditions on this parent resource when instance isn’t available.
- :
prepend
-
Passing
true
will use prepend_before_action instead of a normal before_action.
185 186 187 |
# File 'lib/cancan/controller_additions.rb', line 185 def (*args) cancan_resource_class.add_before_action(self, :authorize_resource, *args) end |
#cancan_resource_class ⇒ Object
291 292 293 |
# File 'lib/cancan/controller_additions.rb', line 291 def cancan_resource_class ControllerResource end |
#cancan_skipper ⇒ Object
295 296 297 |
# File 'lib/cancan/controller_additions.rb', line 295 def cancan_skipper self._cancan_skipper ||= { authorize: {}, load: {} } end |
#check_authorization(options = {}) ⇒ Object
Add this to a controller to ensure it performs authorization through authorize
! 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
end
See skip_authorization_check to bypass this check on specific controller actions.
Options:
- :
only
-
Only applies to given actions.
- :
except
-
Does not apply to given actions.
- :
if
-
Supply the name of a controller method to be called. The authorization check only takes place if this returns true.
:if => :admin_controller?
- :
unless
-
Supply the name of a controller method to be called. The authorization check only takes place if this returns false.
:unless => :devise_controller?
265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/cancan/controller_additions.rb', line 265 def ( = {}) block = proc do |controller| next if controller.instance_variable_defined?(:@_authorized) next if [:if] && !controller.send([:if]) next if [:unless] && controller.send([:unless]) raise AuthorizationNotPerformed, 'This action failed the check_authorization because it does not authorize_resource. ' \ 'Add skip_authorization_check to bypass this check.' end send(:after_action, .slice(:only, :except), &block) end |
#load_and_authorize_resource(*args) ⇒ Object
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
end
15 16 17 |
# File 'lib/cancan/controller_additions.rb', line 15 def (*args) cancan_resource_class.add_before_action(self, :load_and_authorize_resource, *args) end |
#load_resource(*args) ⇒ Object
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) or Article.new(params) 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_action on certain actions.
class BooksController < ApplicationController
before_action :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. 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:
- :
only
-
Only applies before filter to given actions.
- :
except
-
Does not apply before filter to given actions.
- :
through
-
Load this resource through another one. This should match the name of the parent instance variable or method.
- :
through_association
-
The name of the association to fetch the child records through the parent resource. This is normally not needed because it defaults to the pluralized resource name.
- :
shallow
-
Pass
true
to allow this resource to be loaded directly when parent isnil
. Defaults tofalse
. - :
singleton
-
Pass
true
if this is a singleton resource through ahas_one
association. - :
parent
-
True or false depending on if the resource is considered a parent resource. This defaults to
true
if a resource name is given which does not match the controller. - :
class
-
The class to use for the model (string or constant).
- :
instance_name
-
The name of the instance variable to load the resource into.
- :
find_by
-
Find using a different attribute other than id. For example.
load_resource :find_by => :permalink # will use find_by_permalink!(params[:id])
- :
id_param
-
Find using a param key other than :id. For example:
load_resource :id_param => :url # will use find(params[:url])
- :
collection
-
Specify which actions are resource collection actions in addition to :
index
. This is usually not necessary because it will try to guess depending on if the id param is present.load_resource :collection => [:sort, :list]
- :
new
-
Specify which actions are new resource actions in addition to :
new
and :create
. Pass an action name into here if you would like to build a new resource instead of fetch one.load_resource :new => :build
- :
prepend
-
Passing
true
will use prepend_before_action instead of a normal before_action.
121 122 123 |
# File 'lib/cancan/controller_additions.rb', line 121 def load_resource(*args) cancan_resource_class.add_before_action(self, :load_resource, *args) end |
#skip_authorization_check(*args) ⇒ Object
Call this in the class of a controller to skip the check_authorization behavior on the actions.
class HomeController < ApplicationController
:only => :index
end
Any arguments are passed to the before_action
it triggers.
286 287 288 289 |
# File 'lib/cancan/controller_additions.rb', line 286 def (*args) block = proc { |controller| controller.instance_variable_set(:@_authorized, true) } send(:before_action, *args, &block) end |
#skip_authorize_resource(*args) ⇒ Object
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
:only => :index
end
You can also pass the resource name as the first argument to skip that resource.
229 230 231 232 233 |
# File 'lib/cancan/controller_additions.rb', line 229 def (*args) = args. name = args.first cancan_skipper[:authorize][name] = end |
#skip_load_and_authorize_resource(*args) ⇒ Object
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
:only => :index
end
You can also pass the resource name as the first argument to skip that resource.
198 199 200 201 |
# File 'lib/cancan/controller_additions.rb', line 198 def (*args) skip_load_resource(*args) (*args) end |
#skip_load_resource(*args) ⇒ Object
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
skip_load_resource :only => :index
end
You can also pass the resource name as the first argument to skip that resource.
213 214 215 216 217 |
# File 'lib/cancan/controller_additions.rb', line 213 def skip_load_resource(*args) = args. name = args.first cancan_skipper[:load][name] = end |