Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 250 lines (239 sloc) 9.683 kB
44b36ce @ryanb adding controller additions with basic behavior.
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 automatically included into all controllers.
4 # It also makes the "can?" and "cannot?" methods available to all views.
44b36ce @ryanb adding controller additions with basic behavior.
authored
5 module ControllerAdditions
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
6 module ClassMethods
ffa677b @ryanb Don't set resource instance variable if it has been set already - clo…
authored
7 # Sets up a before filter which loads and authorizes the current resource. This performs both
8 # load_resource and authorize_resource and accepts the same arguments. See those methods for details.
dfd84a1 @ryanb improving inline documentation
authored
9 #
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
10 # class BooksController < ApplicationController
11 # load_and_authorize_resource
12 # end
dfd84a1 @ryanb improving inline documentation
authored
13 #
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
14 def load_and_authorize_resource(*args)
15 ControllerResource.add_before_filter(self, :load_and_authorize_resource, *args)
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
16 end
dfd84a1 @ryanb improving inline documentation
authored
17
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
18 # Sets up a before filter which loads the model resource into an instance variable.
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
19 # For example, given an ArticlesController it will load the current article into the @article
20 # instance variable. It does this by either calling Article.find(params[:id]) or
21 # Article.new(params[:article]) depending upon the action. It does nothing for the "index"
22 # action.
dfd84a1 @ryanb improving inline documentation
authored
23 #
ffa677b @ryanb Don't set resource instance variable if it has been set already - clo…
authored
24 # Call this method directly on the controller class.
dfd84a1 @ryanb improving inline documentation
authored
25 #
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
26 # class BooksController < ApplicationController
27 # load_resource
28 # end
dfd84a1 @ryanb improving inline documentation
authored
29 #
ffa677b @ryanb Don't set resource instance variable if it has been set already - clo…
authored
30 # A resource is not loaded if the instance variable is already set. This makes it easy to override
31 # the behavior through a before_filter on certain actions.
dfd84a1 @ryanb improving inline documentation
authored
32 #
ffa677b @ryanb Don't set resource instance variable if it has been set already - clo…
authored
33 # class BooksController < ApplicationController
34 # before_filter :find_book_by_permalink, :only => :show
35 # load_resource
36 #
37 # private
38 #
39 # def find_book_by_permalink
40 # @book = Book.find_by_permalink!(params[:id)
41 # end
42 # end
dfd84a1 @ryanb improving inline documentation
authored
43 #
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
44 # If a name is provided which does not match the controller it assumes it is a parent resource. Child
45 # resources can then be loaded through it.
46 #
47 # class BooksController < ApplicationController
48 # load_resource :author
49 # load_resource :book, :through => :author
50 # end
51 #
52 # Here the author resource will be loaded before each action using params[:author_id]. The book resource
53 # will then be loaded through the @author instance variable.
54 #
55 # That first argument is optional and will default to the singular name of the controller.
56 # A hash of options (see below) can also be passed to this method to further customize it.
57 #
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
58 # See load_and_authorize_resource to automatically authorize the resource too.
dfd84a1 @ryanb improving inline documentation
authored
59 #
63634b4 @ryanb Adding :collection and :new options to load_resource method so we can…
authored
60 # Options:
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
61 # [:+only+]
62 # Only applies before filter to given actions.
dfd84a1 @ryanb improving inline documentation
authored
63 #
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
64 # [:+except+]
65 # Does not apply before filter to given actions.
dfd84a1 @ryanb improving inline documentation
authored
66 #
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
67 # [:+through+]
68 # Load this resource through another one. This should match the name of the parent instance variable.
2a3dd85 @ryanb adding :name option to load_and_authorize_resource if it does not mat…
authored
69 #
84f4c90 @ryanb adding :singular option to support has_one associations in load/autho…
authored
70 # [:+singular+]
71 # Pass +true+ if this is a singular resource through a +has_one+ association.
72 #
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
73 # [:+parent+]
74 # True or false depending on if the resource is considered a parent resource. This defaults to +true+ if a resource
75 # name is given which does not match the controller.
2a3dd85 @ryanb adding :name option to load_and_authorize_resource if it does not mat…
authored
76 #
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
77 # [:+class+]
23a5888 @ryanb renaming :class option to :resource for load_and_authorize_resource w…
authored
78 # The class to use for the model (string or constant).
dfd84a1 @ryanb improving inline documentation
authored
79 #
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
80 # [:+instance_name+]
81 # The name of the instance variable to load the resource into.
82 #
236cece @ryanb adding :find_by option to load_resource - closes #19
authored
83 # [:+find_by+]
84 # Find using a different attribute other than id. For example.
85 #
86 # load_resource :find_by => :permalink # will use find_by_permlink!(params[:id])
87 #
63634b4 @ryanb Adding :collection and :new options to load_resource method so we can…
authored
88 # [:+collection+]
89 # Specify which actions are resource collection actions in addition to :+index+. This
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
90 # is usually not necessary because it will try to guess depending on if the id param is present.
dfd84a1 @ryanb improving inline documentation
authored
91 #
63634b4 @ryanb Adding :collection and :new options to load_resource method so we can…
authored
92 # load_resource :collection => [:sort, :list]
dfd84a1 @ryanb improving inline documentation
authored
93 #
63634b4 @ryanb Adding :collection and :new options to load_resource method so we can…
authored
94 # [:+new+]
95 # Specify which actions are new resource actions in addition to :+new+ and :+create+.
96 # Pass an action name into here if you would like to build a new resource instead of
97 # fetch one.
dfd84a1 @ryanb improving inline documentation
authored
98 #
63634b4 @ryanb Adding :collection and :new options to load_resource method so we can…
authored
99 # load_resource :new => :build
dfd84a1 @ryanb improving inline documentation
authored
100 #
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
101 def load_resource(*args)
102 ControllerResource.add_before_filter(self, :load_resource, *args)
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
103 end
dfd84a1 @ryanb improving inline documentation
authored
104
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
105 # Sets up a before filter which authorizes the resource using the instance variable.
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
106 # For example, if you have an ArticlesController it will check the @article instance variable
107 # and ensure the user can perform the current action on it. Under the hood it is doing
108 # something like the following.
dfd84a1 @ryanb improving inline documentation
authored
109 #
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
110 # authorize!(params[:action].to_sym, @article || Article)
dfd84a1 @ryanb improving inline documentation
authored
111 #
ffa677b @ryanb Don't set resource instance variable if it has been set already - clo…
authored
112 # Call this method directly on the controller class.
dfd84a1 @ryanb improving inline documentation
authored
113 #
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
114 # class BooksController < ApplicationController
115 # authorize_resource
116 # end
dfd84a1 @ryanb improving inline documentation
authored
117 #
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
118 # If you pass in the name of a resource which does not match the controller it will assume
119 # it is a parent resource.
120 #
121 # class BooksController < ApplicationController
122 # authorize_resource :author
123 # authorize_resource :book
124 # end
125 #
126 # Here it will authorize :+show+, @+author+ on every action before authorizing the book.
127 #
128 # That first argument is optional and will default to the singular name of the controller.
129 # A hash of options (see below) can also be passed to this method to further customize it.
130 #
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
131 # See load_and_authorize_resource to automatically load the resource too.
dfd84a1 @ryanb improving inline documentation
authored
132 #
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
133 # Options:
134 # [:+only+]
135 # Only applies before filter to given actions.
dfd84a1 @ryanb improving inline documentation
authored
136 #
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
137 # [:+except+]
138 # Does not apply before filter to given actions.
dfd84a1 @ryanb improving inline documentation
authored
139 #
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
140 # [:+parent+]
141 # True or false depending on if the resource is considered a parent resource. This defaults to +true+ if a resource
142 # name is given which does not match the controller.
143 #
144 # [:+class+]
145 # The class to use for the model (string or constant). This passed in when the instance variable is not set.
146 # Pass +false+ if there is no associated class for this resource and it will use a symbol of the resource name.
2a3dd85 @ryanb adding :name option to load_and_authorize_resource if it does not mat…
authored
147 #
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
148 # [:+instance_name+]
149 # The name of the instance variable for this resource.
2a3dd85 @ryanb adding :name option to load_and_authorize_resource if it does not mat…
authored
150 #
23a5888 @ryanb renaming :class option to :resource for load_and_authorize_resource w…
authored
151 # [:+resource+]
152 # The class to use for the model (string or constant). Alternatively pass a symbol
153 # to represent a resource which does not have a class.
dfd84a1 @ryanb improving inline documentation
authored
154 #
25a1c55 @ryanb adding :through option to replace :nesting option and moving Resource…
authored
155 def authorize_resource(*args)
156 ControllerResource.add_before_filter(self, :authorize_resource, *args)
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
157 end
158 end
dfd84a1 @ryanb improving inline documentation
authored
159
44b36ce @ryanb adding controller additions with basic behavior.
authored
160 def self.included(base)
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
161 base.extend ClassMethods
0f49b54 @ryanb adding 'cannot?' method which performs opposite check of 'can?' - clo…
authored
162 base.helper_method :can?, :cannot?
44b36ce @ryanb adding controller additions with basic behavior.
authored
163 end
dfd84a1 @ryanb improving inline documentation
authored
164
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
165 # Raises a CanCan::AccessDenied exception if the current_ability cannot
166 # perform the given action. This is usually called in a controller action or
167 # before filter to perform the authorization.
dfd84a1 @ryanb improving inline documentation
authored
168 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
169 # def show
170 # @article = Article.find(params[:id])
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
171 # authorize! :read, @article
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
172 # end
dfd84a1 @ryanb improving inline documentation
authored
173 #
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
174 # A :message option can be passed to specify a different message.
dfd84a1 @ryanb improving inline documentation
authored
175 #
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
176 # authorize! :read, @article, :message => "Not authorized to read #{@article.name}"
dfd84a1 @ryanb improving inline documentation
authored
177 #
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
178 # You can rescue from the exception in the controller to customize how unauthorized
179 # access is displayed to the user.
dfd84a1 @ryanb improving inline documentation
authored
180 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
181 # class ApplicationController < ActionController::Base
ef22de6 @ryanb adding custom message argument to unauthorized! method - closes #18
authored
182 # rescue_from CanCan::AccessDenied do |exception|
183 # flash[:error] = exception.message
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
184 # redirect_to root_url
185 # end
186 # end
dfd84a1 @ryanb improving inline documentation
authored
187 #
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
188 # See the CanCan::AccessDenied exception for more details on working with the exception.
dfd84a1 @ryanb improving inline documentation
authored
189 #
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
190 # See the load_and_authorize_resource method to automatically add the authorize! behavior
191 # to the default RESTful actions.
192 def authorize!(action, subject, *args)
193 message = nil
194 if args.last.kind_of?(Hash) && args.last.has_key?(:message)
195 message = args.pop[:message]
196 end
197 raise AccessDenied.new(message, action, subject) if cannot?(action, subject, *args)
198 end
dfd84a1 @ryanb improving inline documentation
authored
199
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
200 def unauthorized!(message = nil)
201 raise ImplementationRemoved, "The unauthorized! method has been removed from CanCan, use authorize! instead."
44b36ce @ryanb adding controller additions with basic behavior.
authored
202 end
dfd84a1 @ryanb improving inline documentation
authored
203
ef5900c @ryanb adding caching to current_ability class method, if you're overriding …
authored
204 # Creates and returns the current user's ability and caches it. If you
205 # want to override how the Ability is defined then this is the place.
206 # Just define the method in the controller to change behavior.
dfd84a1 @ryanb improving inline documentation
authored
207 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
208 # def current_ability
ef5900c @ryanb adding caching to current_ability class method, if you're overriding …
authored
209 # # instead of Ability.new(current_user)
210 # @current_ability ||= UserAbility.new(current_account)
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
211 # end
dfd84a1 @ryanb improving inline documentation
authored
212 #
ef5900c @ryanb adding caching to current_ability class method, if you're overriding …
authored
213 # Notice it is important to cache the ability object so it is not
214 # recreated every time.
44b36ce @ryanb adding controller additions with basic behavior.
authored
215 def current_ability
ef5900c @ryanb adding caching to current_ability class method, if you're overriding …
authored
216 @current_ability ||= ::Ability.new(current_user)
baeef0b @ryanb adding conditions behavior to Ability#can and fetch with Ability#cond…
authored
217 end
dfd84a1 @ryanb improving inline documentation
authored
218
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
219 # Use in the controller or view to check the user's permission for a given action
220 # and object.
dfd84a1 @ryanb improving inline documentation
authored
221 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
222 # can? :destroy, @project
dfd84a1 @ryanb improving inline documentation
authored
223 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
224 # You can also pass the class instead of an instance (if you don't have one handy).
dfd84a1 @ryanb improving inline documentation
authored
225 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
226 # <% if can? :create, Project %>
227 # <%= link_to "New Project", new_project_path %>
228 # <% end %>
dfd84a1 @ryanb improving inline documentation
authored
229 #
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
230 # This simply calls "can?" on the current_ability. See Ability#can?.
44b36ce @ryanb adding controller additions with basic behavior.
authored
231 def can?(*args)
ef5900c @ryanb adding caching to current_ability class method, if you're overriding …
authored
232 current_ability.can?(*args)
44b36ce @ryanb adding controller additions with basic behavior.
authored
233 end
dfd84a1 @ryanb improving inline documentation
authored
234
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
235 # Convenience method which works the same as "can?" but returns the opposite value.
dfd84a1 @ryanb improving inline documentation
authored
236 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
237 # cannot? :destroy, @project
dfd84a1 @ryanb improving inline documentation
authored
238 #
0f49b54 @ryanb adding 'cannot?' method which performs opposite check of 'can?' - clo…
authored
239 def cannot?(*args)
ef5900c @ryanb adding caching to current_ability class method, if you're overriding …
authored
240 current_ability.cannot?(*args)
0f49b54 @ryanb adding 'cannot?' method which performs opposite check of 'can?' - clo…
authored
241 end
44b36ce @ryanb adding controller additions with basic behavior.
authored
242 end
243 end
244
aaed265 @ryanb turning into a funtioning Rails plugin
authored
245 if defined? ActionController
246 ActionController::Base.class_eval do
247 include CanCan::ControllerAdditions
248 end
1edf583 @ryanb BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' t…
authored
249 end
Something went wrong with that request. Please try again.