Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 204 lines (193 sloc) 7.359 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 #
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
14 def load_and_authorize_resource(options = {})
23a5888 @ryanb renaming :class option to :resource for load_and_authorize_resource w…
authored
15 ResourceAuthorization.add_before_filter(self, :load_and_authorize_resource, options)
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
16 end
dfd84a1 @ryanb improving inline documentation
authored
17
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
18 # Sets up a before filter which loads the appropriate model resource into an instance variable.
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 #
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
44 # See load_and_authorize_resource to automatically authorize the resource too.
dfd84a1 @ryanb improving inline documentation
authored
45 #
63634b4 @ryanb Adding :collection and :new options to load_resource method so we can…
authored
46 # Options:
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
47 # [:+only+]
48 # Only applies before filter to given actions.
dfd84a1 @ryanb improving inline documentation
authored
49 #
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
50 # [:+except+]
51 # Does not apply before filter to given actions.
dfd84a1 @ryanb improving inline documentation
authored
52 #
cd217eb @ryanb adding :nested option for load_resource - closes #10
authored
53 # [:+nested+]
54 # Specify which resource this is nested under.
dfd84a1 @ryanb improving inline documentation
authored
55 #
cd217eb @ryanb adding :nested option for load_resource - closes #10
authored
56 # load_resource :nested => :author
dfd84a1 @ryanb improving inline documentation
authored
57 #
a75aee7 @ryanb Allowing :nested option to accept an array for deep nesting
authored
58 # Deep nesting can be defined in an array.
dfd84a1 @ryanb improving inline documentation
authored
59 #
a75aee7 @ryanb Allowing :nested option to accept an array for deep nesting
authored
60 # load_resource :nested => [:publisher, :author]
dfd84a1 @ryanb improving inline documentation
authored
61 #
23a5888 @ryanb renaming :class option to :resource for load_and_authorize_resource w…
authored
62 # [:+resource+]
63 # The class to use for the model (string or constant).
dfd84a1 @ryanb improving inline documentation
authored
64 #
63634b4 @ryanb Adding :collection and :new options to load_resource method so we can…
authored
65 # [:+collection+]
66 # Specify which actions are resource collection actions in addition to :+index+. This
67 # is usually not necessary because it will try to guess depending on if an :+id+
68 # is present in +params+.
dfd84a1 @ryanb improving inline documentation
authored
69 #
63634b4 @ryanb Adding :collection and :new options to load_resource method so we can…
authored
70 # load_resource :collection => [:sort, :list]
dfd84a1 @ryanb improving inline documentation
authored
71 #
63634b4 @ryanb Adding :collection and :new options to load_resource method so we can…
authored
72 # [:+new+]
73 # Specify which actions are new resource actions in addition to :+new+ and :+create+.
74 # Pass an action name into here if you would like to build a new resource instead of
75 # fetch one.
dfd84a1 @ryanb improving inline documentation
authored
76 #
63634b4 @ryanb Adding :collection and :new options to load_resource method so we can…
authored
77 # load_resource :new => :build
dfd84a1 @ryanb improving inline documentation
authored
78 #
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
79 def load_resource(options = {})
23a5888 @ryanb renaming :class option to :resource for load_and_authorize_resource w…
authored
80 ResourceAuthorization.add_before_filter(self, :load_resource, options)
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
81 end
dfd84a1 @ryanb improving inline documentation
authored
82
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
83 # Sets up a before filter which authorizes the current resource using the instance variable.
84 # For example, if you have an ArticlesController it will check the @article instance variable
85 # and ensure the user can perform the current action on it. Under the hood it is doing
86 # something like the following.
dfd84a1 @ryanb improving inline documentation
authored
87 #
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
88 # authorize!(params[:action].to_sym, @article || Article)
dfd84a1 @ryanb improving inline documentation
authored
89 #
ffa677b @ryanb Don't set resource instance variable if it has been set already - clo…
authored
90 # Call this method directly on the controller class.
dfd84a1 @ryanb improving inline documentation
authored
91 #
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
92 # class BooksController < ApplicationController
93 # authorize_resource
94 # end
dfd84a1 @ryanb improving inline documentation
authored
95 #
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
96 # See load_and_authorize_resource to automatically load the resource too.
dfd84a1 @ryanb improving inline documentation
authored
97 #
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
98 # Options:
99 # [:+only+]
100 # Only applies before filter to given actions.
dfd84a1 @ryanb improving inline documentation
authored
101 #
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
102 # [:+except+]
103 # Does not apply before filter to given actions.
dfd84a1 @ryanb improving inline documentation
authored
104 #
23a5888 @ryanb renaming :class option to :resource for load_and_authorize_resource w…
authored
105 # [:+resource+]
106 # The class to use for the model (string or constant). Alternatively pass a symbol
107 # to represent a resource which does not have a class.
dfd84a1 @ryanb improving inline documentation
authored
108 #
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
109 def authorize_resource(options = {})
23a5888 @ryanb renaming :class option to :resource for load_and_authorize_resource w…
authored
110 ResourceAuthorization.add_before_filter(self, :authorize_resource, options)
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
111 end
112 end
dfd84a1 @ryanb improving inline documentation
authored
113
44b36ce @ryanb adding controller additions with basic behavior.
authored
114 def self.included(base)
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
115 base.extend ClassMethods
0f49b54 @ryanb adding 'cannot?' method which performs opposite check of 'can?' - clo…
authored
116 base.helper_method :can?, :cannot?
44b36ce @ryanb adding controller additions with basic behavior.
authored
117 end
dfd84a1 @ryanb improving inline documentation
authored
118
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
119 # Raises a CanCan::AccessDenied exception if the current_ability cannot
120 # perform the given action. This is usually called in a controller action or
121 # before filter to perform the authorization.
dfd84a1 @ryanb improving inline documentation
authored
122 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
123 # def show
124 # @article = Article.find(params[:id])
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
125 # authorize! :read, @article
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
126 # end
dfd84a1 @ryanb improving inline documentation
authored
127 #
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
128 # A :message option can be passed to specify a different message.
dfd84a1 @ryanb improving inline documentation
authored
129 #
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
130 # authorize! :read, @article, :message => "Not authorized to read #{@article.name}"
dfd84a1 @ryanb improving inline documentation
authored
131 #
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
132 # You can rescue from the exception in the controller to customize how unauthorized
133 # access is displayed to the user.
dfd84a1 @ryanb improving inline documentation
authored
134 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
135 # class ApplicationController < ActionController::Base
ef22de6 @ryanb adding custom message argument to unauthorized! method - closes #18
authored
136 # rescue_from CanCan::AccessDenied do |exception|
137 # flash[:error] = exception.message
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
138 # redirect_to root_url
139 # end
140 # end
dfd84a1 @ryanb improving inline documentation
authored
141 #
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
142 # See the CanCan::AccessDenied exception for more details on working with the exception.
dfd84a1 @ryanb improving inline documentation
authored
143 #
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
144 # See the load_and_authorize_resource method to automatically add the authorize! behavior
145 # to the default RESTful actions.
146 def authorize!(action, subject, *args)
147 message = nil
148 if args.last.kind_of?(Hash) && args.last.has_key?(:message)
149 message = args.pop[:message]
150 end
151 raise AccessDenied.new(message, action, subject) if cannot?(action, subject, *args)
152 end
dfd84a1 @ryanb improving inline documentation
authored
153
8903fee @ryanb removing unauthorized! in favor of authorize! and including more info…
authored
154 def unauthorized!(message = nil)
155 raise ImplementationRemoved, "The unauthorized! method has been removed from CanCan, use authorize! instead."
44b36ce @ryanb adding controller additions with basic behavior.
authored
156 end
dfd84a1 @ryanb improving inline documentation
authored
157
ef5900c @ryanb adding caching to current_ability class method, if you're overriding …
authored
158 # Creates and returns the current user's ability and caches it. If you
159 # want to override how the Ability is defined then this is the place.
160 # Just define the method in the controller to change behavior.
dfd84a1 @ryanb improving inline documentation
authored
161 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
162 # def current_ability
ef5900c @ryanb adding caching to current_ability class method, if you're overriding …
authored
163 # # instead of Ability.new(current_user)
164 # @current_ability ||= UserAbility.new(current_account)
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
165 # end
dfd84a1 @ryanb improving inline documentation
authored
166 #
ef5900c @ryanb adding caching to current_ability class method, if you're overriding …
authored
167 # Notice it is important to cache the ability object so it is not
168 # recreated every time.
44b36ce @ryanb adding controller additions with basic behavior.
authored
169 def current_ability
ef5900c @ryanb adding caching to current_ability class method, if you're overriding …
authored
170 @current_ability ||= ::Ability.new(current_user)
baeef0b @ryanb adding conditions behavior to Ability#can and fetch with Ability#cond…
authored
171 end
dfd84a1 @ryanb improving inline documentation
authored
172
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
173 # Use in the controller or view to check the user's permission for a given action
174 # and object.
dfd84a1 @ryanb improving inline documentation
authored
175 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
176 # can? :destroy, @project
dfd84a1 @ryanb improving inline documentation
authored
177 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
178 # You can also pass the class instead of an instance (if you don't have one handy).
dfd84a1 @ryanb improving inline documentation
authored
179 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
180 # <% if can? :create, Project %>
181 # <%= link_to "New Project", new_project_path %>
182 # <% end %>
dfd84a1 @ryanb improving inline documentation
authored
183 #
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
184 # This simply calls "can?" on the current_ability. See Ability#can?.
44b36ce @ryanb adding controller additions with basic behavior.
authored
185 def can?(*args)
ef5900c @ryanb adding caching to current_ability class method, if you're overriding …
authored
186 current_ability.can?(*args)
44b36ce @ryanb adding controller additions with basic behavior.
authored
187 end
dfd84a1 @ryanb improving inline documentation
authored
188
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
189 # Convenience method which works the same as "can?" but returns the opposite value.
dfd84a1 @ryanb improving inline documentation
authored
190 #
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
191 # cannot? :destroy, @project
dfd84a1 @ryanb improving inline documentation
authored
192 #
0f49b54 @ryanb adding 'cannot?' method which performs opposite check of 'can?' - clo…
authored
193 def cannot?(*args)
ef5900c @ryanb adding caching to current_ability class method, if you're overriding …
authored
194 current_ability.cannot?(*args)
0f49b54 @ryanb adding 'cannot?' method which performs opposite check of 'can?' - clo…
authored
195 end
44b36ce @ryanb adding controller additions with basic behavior.
authored
196 end
197 end
198
aaed265 @ryanb turning into a funtioning Rails plugin
authored
199 if defined? ActionController
200 ActionController::Base.class_eval do
201 include CanCan::ControllerAdditions
202 end
1edf583 @ryanb BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' t…
authored
203 end
Something went wrong with that request. Please try again.