cancan/README.rdoc

204 lines
6.9 KiB
Plaintext
Raw Normal View History

2009-11-17 01:03:54 +00:00
= CanCan
2009-11-17 19:22:09 +00:00
This is a simple authorization solution for Ruby on Rails to restrict what a given user is allowed to access in the application. This is completely decoupled from any role based implementation allowing you to define user roles the way you want. All permissions are stored in a single location for convenience.
2009-11-17 05:23:06 +00:00
2009-11-17 19:22:09 +00:00
This assumes you already have authentication (such as Authlogic[http://github.com/binarylogic/authlogic]) which provides a current_user model.
2009-11-17 05:23:06 +00:00
See the RDocs[http://rdoc.info/projects/ryanb/cancan], Wiki[http://wiki.github.com/ryanb/cancan] and Railscasts episode[http://railscasts.com/episodes/192-authorization-with-cancan] for additional documentation.
2009-11-17 05:23:06 +00:00
== Installation
2009-11-17 06:15:10 +00:00
You can set it up as a gem in your environment.rb file.
config.gem "cancan"
2009-11-17 06:15:10 +00:00
And then install the gem.
2009-11-17 05:23:06 +00:00
2009-11-17 06:31:27 +00:00
sudo rake gems:install
2009-11-17 05:23:06 +00:00
2009-11-17 06:15:10 +00:00
Alternatively you can install it as a Rails plugin.
script/plugin install git://github.com/ryanb/cancan.git
2009-11-17 05:23:06 +00:00
== Getting Started
2009-11-17 05:23:06 +00:00
2009-11-17 19:22:09 +00:00
First, define a class called Ability in "models/ability.rb".
2009-11-17 05:23:06 +00:00
class Ability
include CanCan::Ability
def initialize(user)
2009-11-17 05:23:06 +00:00
if user.admin?
can :manage, :all
else
can :read, :all
end
end
end
2009-11-17 19:22:09 +00:00
This is where all permissions will go. See the "Defining Abilities" section below for more information.
2009-11-17 05:23:06 +00:00
2009-11-17 19:22:09 +00:00
You can access the current permissions at any point using the "can?" and "cannot?" methods in the view.
2009-11-17 05:23:06 +00:00
<% if can? :update, @article %>
<%= link_to "Edit", edit_article_path(@article) %>
<% end %>
2009-11-17 19:22:09 +00:00
You can also use these methods in a controller along with the "unauthorized!" method to restrict access.
2009-11-17 05:23:06 +00:00
def show
@article = Article.find(params[:id])
unauthorized! if cannot? :read, @article
2009-11-17 05:23:06 +00:00
end
Setting this for every action can be tedious, therefore the load_and_authorize_resource method is also provided to automatically authorize all actions in a RESTful style resource controller. It will set up a before filter which loads the resource into the instance variable and authorizes it.
2009-11-17 05:23:06 +00:00
class ArticlesController < ApplicationController
load_and_authorize_resource
2009-11-17 05:23:06 +00:00
def show
# @article is already loaded
end
end
2009-11-17 19:22:09 +00:00
If the user authorization fails, a CanCan::AccessDenied exception will be raised. You can catch this and modify its behavior in the ApplicationController.
2009-11-17 05:23:06 +00:00
class ApplicationController < ActionController::Base
2009-12-14 16:37:30 +00:00
rescue_from CanCan::AccessDenied do |exception|
flash[:error] = exception.message
2009-11-17 05:23:06 +00:00
redirect_to root_url
end
end
== Defining Abilities
2009-11-17 19:22:09 +00:00
As shown above, the Ability class is where all user permissions are defined. The user model is passed into the initialize method so you are free to modify the permissions based on the user's attributes. This way CanCan is completely decoupled with how you choose to handle roles.
2009-11-17 05:23:06 +00:00
The "can" method accepts two arguments, the first one is the action you're setting the permission for, the second one is the class of object you're setting it on.
can :update, Article
You can pass an array for either of these parameters to match any one.
can [:update, :destroy], [Article, Comment]
In this case the user has the ability to update or destroy both articles and comments.
2009-11-17 19:22:09 +00:00
You can pass a block to provide logic based on the article's attributes.
2009-11-17 05:23:06 +00:00
can :update, Article do |article|
article && article.user == user
end
If the block returns true then the user has that :update ability for that article, otherwise he will be denied access. It's possible for the passed in model to be nil if one isn't specified, so be sure to take that into consideration.
You can pass :all to reference every type of object. In this case the object type will be passed into the block as well (just in case object is nil).
can :read, :all do |object_class, object|
object_class != Order
end
Here the user has permission to read all objects except orders.
You can also pass :manage as the action which will match any action. In this case the action is passed to the block.
can :manage, Comment do |action, comment|
action != :destroy
end
Finally, the "cannot" method works similar to "can" but defines which abilities cannot be done.
2009-11-17 05:23:06 +00:00
can :read, :all
cannot :read, Product
2009-11-17 05:23:06 +00:00
== Checking Abilities
Use the "can?" method in the controller or view to check the user's permission for a given action and object.
can? :destroy, @project
2009-11-17 19:22:09 +00:00
You can also pass the class instead of an instance (if you don't have one handy).
2009-11-17 05:23:06 +00:00
<% if can? :create, Project %>
<%= link_to "New Project", new_project_path %>
<% end %>
The "cannot?" method is for convenience and performs the opposite check of "can?"
cannot? :destroy, @project
2009-11-17 05:23:06 +00:00
== Aliasing Actions
You can use the "alias_action" method to alias one or more actions into one.
alias_action :update, :destroy, :to => :modify
can :modify, Comment
can? :update, Comment # => true
The following aliases are added by default for conveniently mapping common controller actions.
alias_action :index, :show, :to => :read
alias_action :new, :to => :create
alias_action :edit, :to => :update
== Authorizing Controller Actions
As mentioned in the Getting Started section, you can use the +load_and_authorize_resource+ method in your controller to load the resource into an instance variable and authorize it. If you have a nested resource you can specify that as well.
load_and_authorize_resource :nested => :author
You can also pass an array to the :+nested+ attribute for deep nesting.
If you want to customize the loading behavior on certain actions, you can do so in a before filter.
class BooksController < ApplicationController
before_filter :find_book_by_permalink, :only => :show
load_and_authorize_resource
private
def find_book_by_permalink
@book = Book.find_by_permalink!(params[:id)
end
end
Here the @book instance variable is already set so it will not be loaded again for that action. This works for nested resources as well.
== Assumptions & Configuring
2009-11-17 05:23:06 +00:00
CanCan makes two assumptions about your application.
2009-11-17 19:22:09 +00:00
* You have an Ability class which defines the permissions.
* You have a current_user method in the controller which returns the current user model.
2009-11-17 05:23:06 +00:00
2009-11-17 19:22:09 +00:00
You can override these by overriding the "current_ability" method in your ApplicationController.
2009-11-17 05:23:06 +00:00
def current_ability
UserAbility.new(current_account) # instead of Ability.new(current_user)
2009-11-17 05:23:06 +00:00
end
That's it!
== Testing Abilities
It is very easy to test the Ability model since you can call "can?" directly on it as you would in the view or controller.
2009-11-17 20:59:44 +00:00
def test "user can only destroy projects which he owns"
user = User.new
ability = Ability.new(user)
assert ability.can?(:destroy, Project.new(:user => user))
assert ability.cannot?(:destroy, Project.new)
end
2009-11-17 06:31:27 +00:00
== Special Thanks
CanCan was inspired by declarative_authorization[http://github.com/stffn/declarative_authorization/] and aegis[http://github.com/makandra/aegis]. Many thanks to the authors and contributors.