Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 300 lines (277 sloc) 11.089 kb
0cfb8c7 @ryanb adding basic ability module
authored
1 module CanCan
dfd84a1 @ryanb improving inline documentation
authored
2
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
3 # This module is designed to be included into an Ability class. This will
4 # provide the "can" methods for defining and checking abilities.
dfd84a1 @ryanb improving inline documentation
authored
5 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
6 # class Ability
7 # include CanCan::Ability
8 #
9 # def initialize(user)
10 # if user.admin?
11 # can :manage, :all
12 # else
13 # can :read, :all
14 # end
15 # end
16 # end
dfd84a1 @ryanb improving inline documentation
authored
17 #
0cfb8c7 @ryanb adding basic ability module
authored
18 module Ability
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
19 # Check if the user has permission to perform a given action on an object.
dfd84a1 @ryanb improving inline documentation
authored
20 #
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
21 # can? :destroy, @project
dfd84a1 @ryanb improving inline documentation
authored
22 #
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
23 # You can also pass the class instead of an instance (if you don't have one handy).
dfd84a1 @ryanb improving inline documentation
authored
24 #
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
25 # can? :create, Project
dfd84a1 @ryanb improving inline documentation
authored
26 #
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
27 # Nested resources can be passed through a hash, this way conditions which are
28 # dependent upon the association will work when using a class.
29 #
30 # can? :create, @category => Project
31 #
510cf50 @ryanb adding documentation for passing additional arguments to can?
authored
32 # Any additional arguments will be passed into the "can" block definition. This
33 # can be used to pass more information about the user's request for example.
dfd84a1 @ryanb improving inline documentation
authored
34 #
510cf50 @ryanb adding documentation for passing additional arguments to can?
authored
35 # can? :create, Project, request.remote_ip
dfd84a1 @ryanb improving inline documentation
authored
36 #
510cf50 @ryanb adding documentation for passing additional arguments to can?
authored
37 # can :create Project do |project, remote_ip|
38 # # ...
39 # end
dfd84a1 @ryanb improving inline documentation
authored
40 #
41 # Not only can you use the can? method in the controller and view (see ControllerAdditions),
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
42 # but you can also call it directly on an ability instance.
dfd84a1 @ryanb improving inline documentation
authored
43 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
44 # ability.can? :destroy, @project
dfd84a1 @ryanb improving inline documentation
authored
45 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
46 # This makes testing a user's abilities very easy.
dfd84a1 @ryanb improving inline documentation
authored
47 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
48 # def test "user can only destroy projects which he owns"
49 # user = User.new
50 # ability = Ability.new(user)
51 # assert ability.can?(:destroy, Project.new(:user => user))
52 # assert ability.cannot?(:destroy, Project.new)
53 # end
dfd84a1 @ryanb improving inline documentation
authored
54 #
55 # Also see the RSpec Matchers to aid in testing.
d9f3c8b @ryanb renaming noun to subject internally
authored
56 def can?(action, subject, *extra_args)
12037d7 @funny-falcon should not allow to can? when raw sql without block is present
funny-falcon authored
57 match = relevant_can_definitions_for_match(action, subject).detect do |can_definition|
6084814 @ryanb refactoring can definition matching behavior
authored
58 can_definition.matches_conditions?(action, subject, extra_args)
7d7d249 @funny-falcon passing throw matching rules with not matching conditions
funny-falcon authored
59 end
6084814 @ryanb refactoring can definition matching behavior
authored
60 match ? match.base_behavior : false
0cfb8c7 @ryanb adding basic ability module
authored
61 end
dfd84a1 @ryanb improving inline documentation
authored
62
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
63 # Convenience method which works the same as "can?" but returns the opposite value.
dfd84a1 @ryanb improving inline documentation
authored
64 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
65 # cannot? :destroy, @project
dfd84a1 @ryanb improving inline documentation
authored
66 #
0f49b54 @ryanb adding 'cannot?' method which performs opposite check of 'can?' - closes...
authored
67 def cannot?(*args)
68 !can?(*args)
69 end
dfd84a1 @ryanb improving inline documentation
authored
70
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
71 # Defines which abilities are allowed using two arguments. The first one is the action
72 # you're setting the permission for, the second one is the class of object you're setting it on.
dfd84a1 @ryanb improving inline documentation
authored
73 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
74 # can :update, Article
dfd84a1 @ryanb improving inline documentation
authored
75 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
76 # You can pass an array for either of these parameters to match any one.
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
77 # Here the user has the ability to update or destroy both articles and comments.
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
78 #
79 # can [:update, :destroy], [Article, Comment]
80 #
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
81 # You can pass :all to match any object and :manage to match any action. Here are some examples.
82 #
83 # can :manage, :all
84 # can :update, :all
85 # can :manage, Project
dfd84a1 @ryanb improving inline documentation
authored
86 #
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
87 # You can pass a hash of conditions as the third argument. Here the user can only see active projects which he owns.
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
88 #
baeef0b @ryanb adding conditions behavior to Ability#can and fetch with Ability#conditi...
authored
89 # can :read, Project, :active => true, :user_id => user.id
dfd84a1 @ryanb improving inline documentation
authored
90 #
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
91 # See ActiveRecordAdditions#accessible_by for how to use this in database queries. These conditions
92 # are also used for initial attributes when building a record in ControllerAdditions#load_resource.
dfd84a1 @ryanb improving inline documentation
authored
93 #
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
94 # If the conditions hash does not give you enough control over defining abilities, you can use a block
95 # along with any Ruby code you want.
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
96 #
baeef0b @ryanb adding conditions behavior to Ability#can and fetch with Ability#conditi...
authored
97 # can :update, Project do |project|
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
98 # project.groups.include?(user.group)
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
99 # end
dfd84a1 @ryanb improving inline documentation
authored
100 #
baeef0b @ryanb adding conditions behavior to Ability#can and fetch with Ability#conditi...
authored
101 # If the block returns true then the user has that :update ability for that project, otherwise he
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
102 # will be denied access. The downside to using a block is that it cannot be used to generate
103 # conditions for database queries.
dfd84a1 @ryanb improving inline documentation
authored
104 #
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
105 # You can pass custom objects into this "can" method, this is usually done with a symbol
106 # and is useful if a class isn't available to define permissions on.
dfd84a1 @ryanb improving inline documentation
authored
107 #
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
108 # can :read, :stats
109 # can? :read, :stats # => true
dfd84a1 @ryanb improving inline documentation
authored
110 #
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
111 # IMPORTANT: Neither a hash of conditions or a block will be used when checking permission on a class.
dfd84a1 @ryanb improving inline documentation
authored
112 #
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
113 # can :update, Project, :priority => 3
114 # can? :update, Project # => true
dfd84a1 @ryanb improving inline documentation
authored
115 #
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
116 # If you pass no arguments to +can+, the action, class, and object will be passed to the block and the
117 # block will always be executed. This allows you to override the full behavior if the permissions are
118 # defined in an external source such as the database.
dfd84a1 @ryanb improving inline documentation
authored
119 #
bf9b8ad @ryanb filling in some inline documentation for 1.4
authored
120 # can do |action, object_class, object|
121 # # check the database and return true/false
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
122 # end
dfd84a1 @ryanb improving inline documentation
authored
123 #
66314a8 @ryanb support no arguments to 'can' definition which always calls block
authored
124 def can(action = nil, subject = nil, conditions = nil, &block)
bbbc8a6 @ryanb refactoring much of Ability class into separate CanDefinition class
authored
125 can_definitions << CanDefinition.new(true, action, subject, conditions, block)
d4405e6 @ryanb adding cannot method to define which abilities cannot be done - closes #...
authored
126 end
dfd84a1 @ryanb improving inline documentation
authored
127
128 # Defines an ability which cannot be done. Accepts the same arguments as "can".
129 #
d4405e6 @ryanb adding cannot method to define which abilities cannot be done - closes #...
authored
130 # can :read, :all
131 # cannot :read, Comment
dfd84a1 @ryanb improving inline documentation
authored
132 #
d4405e6 @ryanb adding cannot method to define which abilities cannot be done - closes #...
authored
133 # A block can be passed just like "can", however if the logic is complex it is recommended
134 # to use the "can" method.
dfd84a1 @ryanb improving inline documentation
authored
135 #
d4405e6 @ryanb adding cannot method to define which abilities cannot be done - closes #...
authored
136 # cannot :read, Product do |product|
137 # product.invisible?
138 # end
dfd84a1 @ryanb improving inline documentation
authored
139 #
66314a8 @ryanb support no arguments to 'can' definition which always calls block
authored
140 def cannot(action = nil, subject = nil, conditions = nil, &block)
bbbc8a6 @ryanb refactoring much of Ability class into separate CanDefinition class
authored
141 can_definitions << CanDefinition.new(false, action, subject, conditions, block)
4b6f538 @ryanb moving can definition into ability instance instead of class, this remov...
authored
142 end
dfd84a1 @ryanb improving inline documentation
authored
143
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
144 # Alias one or more actions into another one.
dfd84a1 @ryanb improving inline documentation
authored
145 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
146 # alias_action :update, :destroy, :to => :modify
147 # can :modify, Comment
dfd84a1 @ryanb improving inline documentation
authored
148 #
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
149 # Then :modify permission will apply to both :update and :destroy requests.
dfd84a1 @ryanb improving inline documentation
authored
150 #
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
151 # can? :update, Comment # => true
152 # can? :destroy, Comment # => true
dfd84a1 @ryanb improving inline documentation
authored
153 #
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
154 # This only works in one direction. Passing the aliased action into the "can?" call
155 # will not work because aliases are meant to generate more generic actions.
dfd84a1 @ryanb improving inline documentation
authored
156 #
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
157 # alias_action :update, :destroy, :to => :modify
158 # can :update, Comment
159 # can? :modify, Comment # => false
dfd84a1 @ryanb improving inline documentation
authored
160 #
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
161 # Unless that exact alias is used.
dfd84a1 @ryanb improving inline documentation
authored
162 #
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
163 # can :modify, Comment
164 # can? :modify, Comment # => true
dfd84a1 @ryanb improving inline documentation
authored
165 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
166 # The following aliases are added by default for conveniently mapping common controller actions.
dfd84a1 @ryanb improving inline documentation
authored
167 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
168 # alias_action :index, :show, :to => :read
169 # alias_action :new, :to => :create
170 # alias_action :edit, :to => :update
dfd84a1 @ryanb improving inline documentation
authored
171 #
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
172 # This way one can use params[:action] in the controller to determine the permission.
4b6f538 @ryanb moving can definition into ability instance instead of class, this remov...
authored
173 def alias_action(*args)
174 target = args.pop[:to]
f99d506 @ryanb Append aliased actions (don't overwrite them) - closes #20
authored
175 aliased_actions[target] ||= []
176 aliased_actions[target] += args
4b6f538 @ryanb moving can definition into ability instance instead of class, this remov...
authored
177 end
dfd84a1 @ryanb improving inline documentation
authored
178
7d3b4cd @ryanb Adding clear_aliased_actions to Ability which removes previously defined...
authored
179 # Returns a hash of aliased actions. The key is the target and the value is an array of actions aliasing the key.
c40490d @ryanb refactoring ability can? method - closes #12
authored
180 def aliased_actions
181 @aliased_actions ||= default_alias_actions
182 end
dfd84a1 @ryanb improving inline documentation
authored
183
7d3b4cd @ryanb Adding clear_aliased_actions to Ability which removes previously defined...
authored
184 # Removes previously aliased actions including the defaults.
185 def clear_aliased_actions
186 @aliased_actions = {}
187 end
dfd84a1 @ryanb improving inline documentation
authored
188
a42e067 @ryanb extracting out Query class for generating sql conditions and association...
authored
189 # Returns a CanCan::Query instance to help generate database queries based on the ability.
e098dda @ryanb refactoring query.conditions
authored
190 # If any relevant can definitions use a block then an exception will be raised because an
191 # SQL query cannot be generated from blocks of code.
964a476 @ryanb removing need to pass tableize option around for query conditions
authored
192 def query(action, subject)
e098dda @ryanb refactoring query.conditions
authored
193 Query.new(subject, relevant_can_definitions_for_query(action, subject))
e200814 @ryanb adding joins clause to accessible_by when conditions are across associat...
authored
194 end
25637bb @ryanb removing extra white space at end of lines
authored
195
a5f838a @ryanb use I18n for unauthorization messages - closes #103
authored
196 # See ControllerAdditions#authorize! for documentation.
197 def authorize!(action, subject, *args)
198 message = nil
199 if args.last.kind_of?(Hash) && args.last.has_key?(:message)
200 message = args.pop[:message]
201 end
202 if cannot?(action, subject, *args)
203 message ||= unauthorized_message(action, subject)
204 raise AccessDenied.new(message, action, subject)
205 end
206 end
207
208 def unauthorized_message(action, subject)
209 keys = unauthorized_message_keys(action, subject)
158c908 @ryanb adding action and subject variables to I18n unauthorized message - close...
authored
210 variables = {:action => action.to_s}
211 variables[:subject] = (subject.class == Class ? subject : subject.class).to_s.downcase
212 message = I18n.translate(nil, variables.merge(:scope => :unauthorized, :default => keys + [""]))
a5f838a @ryanb use I18n for unauthorization messages - closes #103
authored
213 message.blank? ? nil : message
214 end
215
a744377 @ryanb the new and create actions will now build the resource with attributes b...
authored
216 def attributes_for(action, subject)
217 attributes = {}
218 relevant_can_definitions(action, subject).map do |can_definition|
721939b @ryanb cleaning up some internal specs and names
authored
219 attributes.merge!(can_definition.attributes_from_conditions) if can_definition.base_behavior
a744377 @ryanb the new and create actions will now build the resource with attributes b...
authored
220 end
221 attributes
222 end
223
9d91545 @ryanb load the collection instance variable on index action - closes #137
authored
224 def has_block?(action, subject)
225 relevant_can_definitions(action, subject).any?(&:only_block?)
226 end
12037d7 @funny-falcon should not allow to can? when raw sql without block is present
funny-falcon authored
227
228 def has_raw_sql?(action, subject)
229 relevant_can_definitions(action, subject).any?(&:only_raw_sql?)
230 end
9d91545 @ryanb load the collection instance variable on index action - closes #137
authored
231
7d3b4cd @ryanb Adding clear_aliased_actions to Ability which removes previously defined...
authored
232 private
25637bb @ryanb removing extra white space at end of lines
authored
233
a5f838a @ryanb use I18n for unauthorization messages - closes #103
authored
234 def unauthorized_message_keys(action, subject)
235 subject = (subject.class == Class ? subject : subject.class).name.underscore unless subject.kind_of? Symbol
236 [subject, :all].map do |try_subject|
237 [aliases_for_action(action), :manage].flatten.map do |try_action|
238 :"#{try_action}.#{try_subject}"
239 end
240 end.flatten
241 end
242
243 # Accepts an array of actions and returns an array of actions which match.
cad4259 @ryanb supporting deeply nested aliases - closes #98
authored
244 # This should be called before "matches?" and other checking methods since they
245 # rely on the actions to be expanded.
246 def expand_actions(actions)
247 actions.map do |action|
248 aliased_actions[action] ? [action, *expand_actions(aliased_actions[action])] : action
249 end.flatten
250 end
dfd84a1 @ryanb improving inline documentation
authored
251
a5f838a @ryanb use I18n for unauthorization messages - closes #103
authored
252 # Given an action, it will try to find all of the actions which are aliased to it.
253 # This does the opposite kind of lookup as expand_actions.
254 def aliases_for_action(action)
255 results = [action]
256 aliased_actions.each do |aliased_action, actions|
257 results += aliases_for_action(aliased_action) if actions.include? action
258 end
259 results
260 end
261
bbbc8a6 @ryanb refactoring much of Ability class into separate CanDefinition class
authored
262 def can_definitions
263 @can_definitions ||= []
264 end
dfd84a1 @ryanb improving inline documentation
authored
265
6084814 @ryanb refactoring can definition matching behavior
authored
266 # Returns an array of CanDefinition instances which match the action and subject
267 # This does not take into consideration any hash conditions or block statements
268 def relevant_can_definitions(action, subject)
269 can_definitions.reverse.select do |can_definition|
270 can_definition.expanded_actions = expand_actions(can_definition.actions)
271 can_definition.relevant? action, subject
baeef0b @ryanb adding conditions behavior to Ability#can and fetch with Ability#conditi...
authored
272 end
273 end
12037d7 @funny-falcon should not allow to can? when raw sql without block is present
funny-falcon authored
274
275 def relevant_can_definitions_for_match(action, subject)
276 relevant_can_definitions(action, subject).each do |can_definition|
277 if can_definition.only_raw_sql?
278 raise Error, "The can? and cannot? call cannot be used with a raw sql 'can' definition. The checking code cannot be determined for #{action.inspect} #{subject.inspect}"
279 end
280 end
281 end
dfd84a1 @ryanb improving inline documentation
authored
282
e098dda @ryanb refactoring query.conditions
authored
283 def relevant_can_definitions_for_query(action, subject)
a42e067 @ryanb extracting out Query class for generating sql conditions and association...
authored
284 relevant_can_definitions(action, subject).each do |can_definition|
285 if can_definition.only_block?
4fe44af @ryanb be more clear about blocks not working with accessible_by - closes #130
authored
286 raise Error, "The accessible_by call cannot be used with a block 'can' definition. The SQL cannot be determined for #{action.inspect} #{subject.inspect}"
a42e067 @ryanb extracting out Query class for generating sql conditions and association...
authored
287 end
288 end
289 end
290
4b6f538 @ryanb moving can definition into ability instance instead of class, this remov...
authored
291 def default_alias_actions
292 {
293 :read => [:index, :show],
294 :create => [:new],
295 :update => [:edit],
296 }
297 end
0cfb8c7 @ryanb adding basic ability module
authored
298 end
299 end
Something went wrong with that request. Please try again.