Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 187 lines (177 sloc) 6.875 kB
44b36ce @ryanb adding controller additions with basic behavior.
authored
1 module CanCan
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
2
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.
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
9 #
10 # class BooksController < ApplicationController
11 # load_and_authorize_resource
12 # end
13 #
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
14 def load_and_authorize_resource(options = {})
15 before_filter(options.slice(:only, :except)) { |c| ResourceAuthorization.new(c, c.params, options.except(:only, :except)).load_and_authorize_resource }
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
16 end
17
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.
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.
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
25 #
26 # class BooksController < ApplicationController
27 # load_resource
28 # end
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.
32 #
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
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.
63634b4 @ryanb Adding :collection and :new options to load_resource method so we can…
authored
45 #
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.
49 #
50 # [:+except+]
51 # Does not apply before filter to given actions.
cd217eb @ryanb adding :nested option for load_resource - closes #10
authored
52 #
53 # [:+nested+]
54 # Specify which resource this is nested under.
55 #
56 # load_resource :nested => :author
57 #
a75aee7 @ryanb Allowing :nested option to accept an array for deep nesting
authored
58 # Deep nesting can be defined in an array.
59 #
60 # load_resource :nested => [:publisher, :author]
61 #
021f33c @ryanb Adding :class option to load_resource so one can customize which clas…
authored
62 # [:+class+]
63 # The class to use for the model.
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+.
69 #
70 # load_resource :collection => [:sort, :list]
71 #
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.
76 #
77 # load_resource :new => :build
021f33c @ryanb Adding :class option to load_resource so one can customize which clas…
authored
78 #
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
79 def load_resource(options = {})
80 before_filter(options.slice(:only, :except)) { |c| ResourceAuthorization.new(c, c.params, options.except(:only, :except)).load_resource }
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
81 end
82
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.
87 #
88 # unauthorized! if cannot?(params[:action].to_sym, @article || Article)
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.
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
91 #
92 # class BooksController < ApplicationController
93 # authorize_resource
94 # end
95 #
96 # See load_and_authorize_resource to automatically load the resource too.
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
97 #
98 # Options:
99 # [:+only+]
100 # Only applies before filter to given actions.
101 #
102 # [:+except+]
103 # Does not apply before filter to given actions.
104 #
021f33c @ryanb Adding :class option to load_resource so one can customize which clas…
authored
105 # [:+class+]
106 # The class to use for the model.
107 #
94e031b @ryanb Pass :only and :except options to before filters for load/authorize r…
authored
108 def authorize_resource(options = {})
109 before_filter(options.slice(:only, :except)) { |c| ResourceAuthorization.new(c, c.params, options.except(:only, :except)).authorize_resource }
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
110 end
111 end
112
44b36ce @ryanb adding controller additions with basic behavior.
authored
113 def self.included(base)
a5f9882 @ryanb turning load and authorize resource methods into class methods which …
authored
114 base.extend ClassMethods
0f49b54 @ryanb adding 'cannot?' method which performs opposite check of 'can?' - clo…
authored
115 base.helper_method :can?, :cannot?
44b36ce @ryanb adding controller additions with basic behavior.
authored
116 end
117
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
118 # Raises the CanCan::AccessDenied exception. This is often used in a
119 # controller action to mark a request as unauthorized.
120 #
121 # def show
122 # @article = Article.find(params[:id])
123 # unauthorized! if cannot? :read, @article
124 # end
125 #
ef22de6 @ryanb adding custom message argument to unauthorized! method - closes #18
authored
126 # The unauthorized! method accepts an optional argument which sets the
127 # message of the exception.
128 #
129 # You can rescue from the exception in the controller to define the behavior.
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
130 #
131 # class ApplicationController < ActionController::Base
ef22de6 @ryanb adding custom message argument to unauthorized! method - closes #18
authored
132 # rescue_from CanCan::AccessDenied do |exception|
133 # flash[:error] = exception.message
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
134 # redirect_to root_url
135 # end
136 # end
137 #
138 # See the load_and_authorize_resource method to automatically add
139 # the "unauthorized!" behavior to a RESTful controller's actions.
ef22de6 @ryanb adding custom message argument to unauthorized! method - closes #18
authored
140 def unauthorized!(message = "You are not authorized to access this page.")
141 raise AccessDenied, message
44b36ce @ryanb adding controller additions with basic behavior.
authored
142 end
143
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
144 # Creates and returns the current user's ability. You generally do not invoke
145 # this method directly, instead you can override this method to change its
146 # behavior if the Ability class or current_user method are different.
147 #
148 # def current_ability
149 # UserAbility.new(current_account) # instead of Ability.new(current_user)
150 # end
151 #
44b36ce @ryanb adding controller additions with basic behavior.
authored
152 def current_ability
1edf583 @ryanb BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' t…
authored
153 ::Ability.new(current_user)
44b36ce @ryanb adding controller additions with basic behavior.
authored
154 end
155
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
156 # Use in the controller or view to check the user's permission for a given action
157 # and object.
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
158 #
159 # can? :destroy, @project
160 #
161 # You can also pass the class instead of an instance (if you don't have one handy).
162 #
163 # <% if can? :create, Project %>
164 # <%= link_to "New Project", new_project_path %>
165 # <% end %>
166 #
5bd1a85 @ryanb little fixes to inline documentation (rdocs)
authored
167 # This simply calls "can?" on the current_ability. See Ability#can?.
44b36ce @ryanb adding controller additions with basic behavior.
authored
168 def can?(*args)
169 (@current_ability ||= current_ability).can?(*args)
170 end
1034c81 @ryanb adding a before filter for loading and authorizing a resource
authored
171
b9227eb @ryanb adding a lot of inline documentation to code for rdocs
authored
172 # Convenience method which works the same as "can?" but returns the opposite value.
173 #
174 # cannot? :destroy, @project
175 #
0f49b54 @ryanb adding 'cannot?' method which performs opposite check of 'can?' - clo…
authored
176 def cannot?(*args)
177 (@current_ability ||= current_ability).cannot?(*args)
178 end
44b36ce @ryanb adding controller additions with basic behavior.
authored
179 end
180 end
181
aaed265 @ryanb turning into a funtioning Rails plugin
authored
182 if defined? ActionController
183 ActionController::Base.class_eval do
184 include CanCan::ControllerAdditions
185 end
1edf583 @ryanb BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' t…
authored
186 end
Something went wrong with that request. Please try again.