Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 199 lines (122 sloc) 6.83 kb
6c6a57b @ryanb adding documentation placeholder
authored
1 = CanCan
2
b1d3d66 @ryanb filling readme
authored
3 This is a simple authorization solution for Rails which is completely decoupled from how you set up the user's roles. All permissions are stored in a single location for convenience.
4
5 This assumes you already have an authentication solution (such as Authlogic) which proves a current_user model.
6
7
8 == Installation
9
28eaf1b @ryanb releasing gem v0.1.0
authored
10 You can set it up as a gem in your environment.rb file.
11
12 config.gem "cancan", :source => "http://gemcutter.org"
13
14 And then install the gem.
b1d3d66 @ryanb filling readme
authored
15
9d58226 @ryanb couple fixes in readme
authored
16 sudo rake gems:install
b1d3d66 @ryanb filling readme
authored
17
28eaf1b @ryanb releasing gem v0.1.0
authored
18 Alternatively you can install it as a Rails plugin.
19
20 script/plugin install git://github.com/ryanb/cancan.git
b1d3d66 @ryanb filling readme
authored
21
22
23 == Setup
24
25 First define a class called Ability, place it in "models/ability.rb".
26
27 class Ability
28 include CanCan::Ability
29
30 def prepare(user)
31 if user.admin?
32 can :manage, :all
33 else
34 can :read, :all
35 end
36 end
37 end
38
39 This class is where all permissions will go. See the "Defining Abilities" section below for more information.
40
41 In the view layer you can access the current permissions at any point using the "can?" method. See "Checking Abilities" section below.
42
43 <% if can? :update, @article %>
44 <%= link_to "Edit", edit_article_path(@article) %>
45 <% end %>
46
47 You can also use this method in the controller layer along with the "unauthorized!" method to restrict access.
48
49 def show
50 @article = Article.find(params[:id])
51 unauthorized! unless can? :read, @article
52 end
53
54 Setting this for every action can be tedious, therefore a before filter is also provided for automatically applying this setting to a RESTful style resource controller.
55
56 class ArticlesController < ApplicationController
57 before_filter :load_and_authorize_resource
58
59 def show
60 # @article is already loaded
61 end
62 end
63
64 If the user authorization fails, a CanCan::AccessDenied exception will be raised. You can catch this and modify its behavior.
65
66 class ApplicationController < ActionController::Base
67 rescue_from CanCan::AccessDenied, :with => :access_denied
68
69 protected
70
71 def access_denied
72 flash[:error] = "Sorry, you are not allowed to access that page."
73 redirect_to root_url
74 end
75 end
76
77
78 == Defining Abilities
79
80 As shown above, the Ability#prepare method is where all user permissions are defined. The user model is passed into this 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.
81
82 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.
83
84 can :update, Article
85
86 You can pass an array for either of these parameters to match any one.
87
88 can [:update, :destroy], [Article, Comment]
89
90 In this case the user has the ability to update or destroy both articles and comments.
91
92 You can pass a block to provide logic based on the article's attributes. For example:
93
94 can :update, Article do |article|
95 article && article.user == user
96 end
97
98 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.
99
100 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).
101
102 can :read, :all do |object_class, object|
103 object_class != Order
104 end
105
106 Here the user has permission to read all objects except orders.
107
108 You can also pass :manage as the action which will match any action. In this case the action is passed to the block.
109
110 can :manage, Comment do |action, comment|
111 action != :destroy
112 end
113
114 Finally, you can use the "alias_action" method to alias one or more actions into one.
115
116 alias_action :update, :destroy, :to => :modify
117 can :modify, Comment
118
119 The following aliases are added by default for conveniently mapping common controller actions.
120
121 alias_action :index, :show, :to => :read
122 alias_action :new, :to => :create
123 alias_action :edit, :to => :update
124
125
126 == Checking Abilities
127
128 Use the "can?" method in the controller or view to check the user's permission for a given action and object.
129
130 can? :destroy, @project
131
132 You can also pass the class instead of an instance (if you don't have one handy). For example:
133
134 <% if can? :create, Project %>
135 <%= link_to "New Project", new_project_path %>
136 <% end %>
137
138
139 == Custom Actions
140
141 There is no limit to what actions you can use to determine abilities. For example, if only pro users are allowed to upload a picture for their product, you might add restrictions like this.
142
143 # ability.rb
144 can :upload_picture, Project if user.pro?
145
146 # projects/_form.html.erb
147 <%= f.file_field :picture if can? :upload_picture, @project %>
148
149 # projects_controller.rb
150 def update
151 unauthorized! if params[:project][:upload_picture] && !can?(:upload_picture, @project)
152 # ...
153 end
154
155
156 == Customizing Assumptions
157
158 CanCan makes two assumptions about your application.
159
9d58226 @ryanb couple fixes in readme
authored
160 * The permissions are defined in Ability#prepare.
161 * The user is fetched with current_user method in the controller.
b1d3d66 @ryanb filling readme
authored
162
163 You can override these by defining the "current_ability" method in your ApplicationController.
164
165 def current_ability
166 ability = UserAbility.new # instead of Ability
167 ability.prepare(current_account) # instead of current_user
168 ability # be sure to return the ability
169 end
170
171 That's it!
172
173
174 == Permissions in Database
175
176 Perhaps a non-coder needs the ability to modify the user abilities, or you want to change them without having to re-deploy the application. In that case it may be best to store the permission logic in a separate model, let's call it Permission. It is easy to use the database records when defining abilities.
177
178 For example, let's assume that each user has_many :permissions, and each permission has "action", "object_type" and "object_id" columns. The last of which is optional.
179
180 class Ability
181 include CanCan::Ability
182
183 def prepare(user)
184 can :manage, :all do |action, object_class, object|
185 user.permissions.find_all_by_action(action).any? do |permission|
186 permission.object_type.constantize == object_class &&
187 (object.nil? || permission.object_id.nil? || permission.object_id == object.id)
188 end
189 end
190 end
191 end
192
193 The actual details will depend largely on your application requirements, but hopefully you can see how it's possible to define permissions in the database and use them with CanCan.
9d58226 @ryanb couple fixes in readme
authored
194
195
196 == Special Thanks
197
198 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.
Something went wrong with that request. Please try again.