tma-declarative_authorization 0.3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. data/CHANGELOG +79 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +504 -0
  4. data/Rakefile +43 -0
  5. data/app/controllers/authorization_rules_controller.rb +235 -0
  6. data/app/controllers/authorization_usages_controller.rb +23 -0
  7. data/app/helpers/authorization_rules_helper.rb +183 -0
  8. data/app/views/authorization_rules/_change.erb +49 -0
  9. data/app/views/authorization_rules/_show_graph.erb +37 -0
  10. data/app/views/authorization_rules/_suggestion.erb +9 -0
  11. data/app/views/authorization_rules/_suggestions.erb +24 -0
  12. data/app/views/authorization_rules/change.html.erb +124 -0
  13. data/app/views/authorization_rules/graph.dot.erb +68 -0
  14. data/app/views/authorization_rules/graph.html.erb +40 -0
  15. data/app/views/authorization_rules/index.html.erb +17 -0
  16. data/app/views/authorization_usages/index.html.erb +36 -0
  17. data/authorization_rules.dist.rb +20 -0
  18. data/config/routes.rb +7 -0
  19. data/garlic_example.rb +20 -0
  20. data/init.rb +5 -0
  21. data/lib/declarative_authorization.rb +15 -0
  22. data/lib/declarative_authorization/authorization.rb +630 -0
  23. data/lib/declarative_authorization/development_support/analyzer.rb +252 -0
  24. data/lib/declarative_authorization/development_support/change_analyzer.rb +253 -0
  25. data/lib/declarative_authorization/development_support/change_supporter.rb +578 -0
  26. data/lib/declarative_authorization/development_support/development_support.rb +243 -0
  27. data/lib/declarative_authorization/helper.rb +60 -0
  28. data/lib/declarative_authorization/in_controller.rb +571 -0
  29. data/lib/declarative_authorization/in_model.rb +150 -0
  30. data/lib/declarative_authorization/maintenance.rb +188 -0
  31. data/lib/declarative_authorization/obligation_scope.rb +297 -0
  32. data/lib/declarative_authorization/rails_legacy.rb +14 -0
  33. data/lib/declarative_authorization/reader.rb +438 -0
  34. data/test/authorization_test.rb +823 -0
  35. data/test/controller_filter_resource_access_test.rb +385 -0
  36. data/test/controller_test.rb +369 -0
  37. data/test/dsl_reader_test.rb +157 -0
  38. data/test/helper_test.rb +154 -0
  39. data/test/maintenance_test.rb +41 -0
  40. data/test/model_test.rb +1215 -0
  41. data/test/schema.sql +53 -0
  42. data/test/test_helper.rb +112 -0
  43. metadata +106 -0
@@ -0,0 +1,243 @@
1
+
2
+ module Authorization
3
+ module DevelopmentSupport
4
+ class AbstractAnalyzer
5
+ attr_reader :engine
6
+
7
+ def initialize (engine)
8
+ @engine = engine
9
+ end
10
+
11
+ def roles
12
+ AnalyzerEngine.roles(engine)
13
+ end
14
+
15
+ def rules
16
+ roles.collect {|role| role.rules }.flatten
17
+ end
18
+ end
19
+
20
+ # Groups utility methods and classes to better work with authorization object
21
+ # model.
22
+ module AnalyzerEngine
23
+
24
+ def self.roles (engine)
25
+ Role.all(engine)
26
+ end
27
+
28
+ def self.relevant_roles (engine, users)
29
+ users.collect {|user| user.role_symbols.map {|role_sym| Role.for_sym(role_sym, engine)}}.
30
+ flatten.uniq.collect {|role| [role] + role.ancestors}.flatten.uniq
31
+ end
32
+
33
+ def self.rule_for_permission (engine, privilege, context, role)
34
+ AnalyzerEngine.roles(engine).
35
+ find {|cloned_role| cloned_role.to_sym == role.to_sym}.rules.find do |rule|
36
+ rule.contexts.include?(context) and rule.privileges.include?(privilege)
37
+ end
38
+ end
39
+
40
+ def self.apply_change (engine, change)
41
+ case change[0]
42
+ when :add_role
43
+ role_symbol = change[1]
44
+ if engine.roles.include?(role_symbol)
45
+ false
46
+ else
47
+ engine.roles << role_symbol
48
+ true
49
+ end
50
+ when :add_privilege
51
+ privilege, context, role = change[1,3]
52
+ role = Role.for_sym(role.to_sym, engine)
53
+ privilege = Privilege.for_sym(privilege.to_sym, engine)
54
+ if ([privilege] + privilege.ancestors).any? {|ancestor_privilege| ([role] + role.ancestors).any? {|ancestor_role| !ancestor_role.rules_for_permission(ancestor_privilege, context).empty?}}
55
+ false
56
+ else
57
+ engine.auth_rules << AuthorizationRule.new(role.to_sym,
58
+ [privilege.to_sym], [context])
59
+ true
60
+ end
61
+ when :remove_privilege
62
+ privilege, context, role = change[1,3]
63
+ role = Role.for_sym(role.to_sym, engine)
64
+ privilege = Privilege.for_sym(privilege.to_sym, engine)
65
+ rules_with_priv = role.rules_for_permission(privilege, context)
66
+ if rules_with_priv.empty?
67
+ false
68
+ else
69
+ rules_with_priv.each do |rule|
70
+ rule.rule.privileges.delete(privilege.to_sym)
71
+ engine.auth_rules.delete(rule.rule) if rule.rule.privileges.empty?
72
+ end
73
+ true
74
+ end
75
+ end
76
+ end
77
+
78
+ class Role
79
+ @@role_objects = {}
80
+ attr_reader :role
81
+ def initialize (role, rules, engine)
82
+ @role = role
83
+ @rules = rules
84
+ @engine = engine
85
+ end
86
+
87
+ def source_line
88
+ @rules.empty? ? nil : @rules.first.source_line
89
+ end
90
+ def source_file
91
+ @rules.empty? ? nil : @rules.first.source_file
92
+ end
93
+
94
+ # ancestors' privileges are included in in the current role
95
+ def ancestors (role_symbol = nil)
96
+ role_symbol ||= @role
97
+ (@engine.role_hierarchy[role_symbol] || []).
98
+ collect {|lower_role| ancestors(lower_role) }.flatten +
99
+ (role_symbol == @role ? [] : [Role.for_sym(role_symbol, @engine)])
100
+ end
101
+ def descendants (role_symbol = nil)
102
+ role_symbol ||= @role
103
+ (@engine.rev_role_hierarchy[role_symbol] || []).
104
+ collect {|higher_role| descendants(higher_role) }.flatten +
105
+ (role_symbol == @role ? [] : [Role.for_sym(role_symbol, @engine)])
106
+ end
107
+
108
+ def rules
109
+ @rules ||= @engine.auth_rules.select {|rule| rule.role == @role}.
110
+ collect {|rule| Rule.new(rule, @engine)}
111
+ end
112
+ def rules_for_permission (privilege, context)
113
+ rules.select do |rule|
114
+ rule.matches?([@role], [privilege.to_sym], context)
115
+ end
116
+ end
117
+
118
+ def to_sym
119
+ @role
120
+ end
121
+ def self.for_sym (role_sym, engine)
122
+ @@role_objects[[role_sym, engine]] ||= new(role_sym, nil, engine)
123
+ end
124
+
125
+ def self.all (engine)
126
+ rules_by_role = engine.auth_rules.inject({}) do |memo, rule|
127
+ memo[rule.role] ||= []
128
+ memo[rule.role] << rule
129
+ memo
130
+ end
131
+ engine.roles.collect do |role|
132
+ new(role, (rules_by_role[role] || []).
133
+ collect {|rule| Rule.new(rule, engine)}, engine)
134
+ end
135
+ end
136
+ def self.all_for_privilege (privilege, context, engine)
137
+ privilege = privilege.is_a?(Symbol) ? Privilege.for_sym(privilege, engine) : privilege
138
+ privilege_symbols = ([privilege] + privilege.ancestors).map(&:to_sym)
139
+ all(engine).select {|role| role.rules.any? {|rule| rule.matches?([role.to_sym], privilege_symbols, context)}}.
140
+ collect {|role| [role] + role.descendants}.flatten.uniq
141
+ end
142
+ end
143
+
144
+ class Rule
145
+ @@rule_objects = {}
146
+ delegate :source_line, :source_file, :contexts, :matches?, :to => :@rule
147
+ attr_reader :rule
148
+ def initialize (rule, engine)
149
+ @rule = rule
150
+ @engine = engine
151
+ end
152
+ def privileges
153
+ PrivilegesSet.new(self, @engine, @rule.privileges.collect {|privilege| Privilege.for_sym(privilege, @engine) })
154
+ end
155
+ def self.for_rule (rule, engine)
156
+ @@rule_objects[[rule, engine]] ||= new(rule, engine)
157
+ end
158
+ end
159
+
160
+ class Privilege
161
+ @@privilege_objects = {}
162
+ def initialize (privilege, engine)
163
+ @privilege = privilege
164
+ @engine = engine
165
+ end
166
+
167
+ # Ancestor privileges are higher in the hierarchy.
168
+ # Doesn't take context into account.
169
+ def ancestors (priv_symbol = nil)
170
+ priv_symbol ||= @privilege
171
+ # context-specific?
172
+ (@engine.rev_priv_hierarchy[[priv_symbol, nil]] || []).
173
+ collect {|higher_priv| ancestors(higher_priv) }.flatten +
174
+ (priv_symbol == @privilege ? [] : [Privilege.for_sym(priv_symbol, @engine)])
175
+ end
176
+ def descendants (priv_symbol = nil)
177
+ priv_symbol ||= @privilege
178
+ # context-specific?
179
+ (@engine.privilege_hierarchy[priv_symbol] || []).
180
+ collect {|lower_priv, context| descendants(lower_priv) }.flatten +
181
+ (priv_symbol == @privilege ? [] : [Privilege.for_sym(priv_symbol, @engine)])
182
+ end
183
+
184
+ def rules
185
+ @rules ||= find_rules_for_privilege
186
+ end
187
+ def source_line
188
+ rules.empty? ? nil : rules.first.source_line
189
+ end
190
+ def source_file
191
+ rules.empty? ? nil : rules.first.source_file
192
+ end
193
+
194
+ def to_sym
195
+ @privilege
196
+ end
197
+ def self.for_sym (privilege_sym, engine)
198
+ @@privilege_objects[[privilege_sym, engine]] ||= new(privilege_sym, engine)
199
+ end
200
+
201
+ private
202
+ def find_rules_for_privilege
203
+ @engine.auth_rules.select {|rule| rule.privileges.include?(@privilege)}.
204
+ collect {|rule| Rule.for_rule(rule, @engine)}
205
+ end
206
+ end
207
+
208
+ class PrivilegesSet < Set
209
+ def initialize (*args)
210
+ if args.length > 2
211
+ @rule = args.shift
212
+ @engine = args.shift
213
+ end
214
+ super(*args)
215
+ end
216
+ def include? (privilege)
217
+ if privilege.is_a?(Symbol)
218
+ super(privilege_from_symbol(privilege))
219
+ else
220
+ super
221
+ end
222
+ end
223
+ def delete (privilege)
224
+ @rule.rule.privileges.delete(privilege.to_sym)
225
+ if privilege.is_a?(Symbol)
226
+ super(privilege_from_symbol(privilege))
227
+ else
228
+ super
229
+ end
230
+ end
231
+
232
+ def intersects? (privileges)
233
+ intersection(privileges).length > 0
234
+ end
235
+
236
+ private
237
+ def privilege_from_symbol (privilege_sym)
238
+ Privilege.for_sym(privilege_sym, @engine)
239
+ end
240
+ end
241
+ end
242
+ end
243
+ end
@@ -0,0 +1,60 @@
1
+ # Authorization::AuthorizationHelper
2
+ require File.dirname(__FILE__) + '/authorization.rb'
3
+
4
+ module Authorization
5
+ module AuthorizationHelper
6
+
7
+ # If the current user meets the given privilege, permitted_to? returns true
8
+ # and yields to the optional block. The attribute checks that are defined
9
+ # in the authorization rules are only evaluated if an object is given
10
+ # for context.
11
+ #
12
+ # Examples:
13
+ # <% permitted_to? :create, :users do %>
14
+ # <%= link_to 'New', new_user_path %>
15
+ # <% end %>
16
+ # ...
17
+ # <% if permitted_to? :create, :users %>
18
+ # <%= link_to 'New', new_user_path %>
19
+ # <% else %>
20
+ # You are not allowed to create new users!
21
+ # <% end %>
22
+ # ...
23
+ # <% for user in @users %>
24
+ # <%= link_to 'Edit', edit_user_path(user) if permitted_to? :update, user %>
25
+ # <% end %>
26
+ #
27
+ # To pass in an object and override the context, you can use the optional
28
+ # options:
29
+ # permitted_to? :update, user, :context => :account
30
+ #
31
+ def permitted_to? (privilege, object_or_sym = nil, options = {}, &block)
32
+ controller.permitted_to?(privilege, object_or_sym, options, &block)
33
+ end
34
+
35
+ # While permitted_to? is used for authorization in views, in some cases
36
+ # content should only be shown to some users without being concerned
37
+ # with authorization. E.g. to only show the most relevant menu options
38
+ # to a certain group of users. That is what has_role? should be used for.
39
+ #
40
+ # Examples:
41
+ # <% has_role?(:sales) do %>
42
+ # <%= link_to 'All contacts', contacts_path %>
43
+ # <% end %>
44
+ # ...
45
+ # <% if has_role?(:sales) %>
46
+ # <%= link_to 'Customer contacts', contacts_path %>
47
+ # <% else %>
48
+ # ...
49
+ # <% end %>
50
+ #
51
+ def has_role? (*roles, &block)
52
+ controller.has_role?(*roles, &block)
53
+ end
54
+
55
+ # As has_role? except checks all roles included in the role hierarchy
56
+ def has_role_with_hierarchy?(*roles, &block)
57
+ controller.has_role_with_hierarchy?(*roles, &block)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,571 @@
1
+ # Authorization::AuthorizationInController
2
+ require File.dirname(__FILE__) + '/authorization.rb'
3
+
4
+ module Authorization
5
+ module AuthorizationInController
6
+
7
+ def self.included(base) # :nodoc:
8
+ base.extend(ClassMethods)
9
+ base.hide_action :authorization_engine, :permitted_to?,
10
+ :permitted_to!
11
+ end
12
+
13
+ DEFAULT_DENY = false
14
+
15
+ # Returns the Authorization::Engine for the current controller.
16
+ def authorization_engine
17
+ @authorization_engine ||= Authorization::Engine.instance
18
+ end
19
+
20
+ # If the current user meets the given privilege, permitted_to? returns true
21
+ # and yields to the optional block. The attribute checks that are defined
22
+ # in the authorization rules are only evaluated if an object is given
23
+ # for context.
24
+ #
25
+ # See examples for Authorization::AuthorizationHelper #permitted_to?
26
+ #
27
+ # If no object or context is specified, the controller_name is used as
28
+ # context.
29
+ #
30
+ def permitted_to? (privilege, object_or_sym = nil, options = {}, &block)
31
+ permitted_to!(privilege, object_or_sym, options.merge(:non_bang => true), &block)
32
+ end
33
+
34
+ # Works similar to the permitted_to? method, but
35
+ # throws the authorization exceptions, just like Engine#permit!
36
+ def permitted_to! (privilege, object_or_sym = nil, options = {}, &block)
37
+ context = object = nil
38
+ if object_or_sym.nil?
39
+ context = self.class.controller_name.to_sym
40
+ elsif object_or_sym.is_a?(Symbol)
41
+ context = object_or_sym
42
+ else
43
+ object = object_or_sym
44
+ end
45
+
46
+ non_bang = options.delete(:non_bang)
47
+ args = [
48
+ privilege,
49
+ {:user => current_user,
50
+ :object => object,
51
+ :context => context,
52
+ :skip_attribute_test => object.nil?}.merge(options)
53
+ ]
54
+ if non_bang
55
+ authorization_engine.permit?(*args, &block)
56
+ else
57
+ authorization_engine.permit!(*args, &block)
58
+ end
59
+ end
60
+
61
+ # While permitted_to? is used for authorization, in some cases
62
+ # content should only be shown to some users without being concerned
63
+ # with authorization. E.g. to only show the most relevant menu options
64
+ # to a certain group of users. That is what has_role? should be used for.
65
+ def has_role? (*roles, &block)
66
+ user_roles = authorization_engine.roles_for(current_user)
67
+ result = roles.all? do |role|
68
+ user_roles.include?(role)
69
+ end
70
+ yield if result and block_given?
71
+ result
72
+ end
73
+
74
+ # As has_role? except checks all roles included in the role hierarchy
75
+ def has_role_with_hierarchy?(*roles, &block)
76
+ user_roles = authorization_engine.roles_with_hierarchy_for(current_user)
77
+ result = roles.all? do |role|
78
+ user_roles.include?(role)
79
+ end
80
+ yield if result and block_given?
81
+ result
82
+ end
83
+
84
+
85
+ protected
86
+ def filter_access_filter # :nodoc:
87
+ permissions = self.class.all_filter_access_permissions
88
+ all_permissions = permissions.select {|p| p.actions.include?(:all)}
89
+ matching_permissions = permissions.select {|p| p.matches?(action_name)}
90
+ allowed = false
91
+ auth_exception = nil
92
+ begin
93
+ allowed = if !matching_permissions.empty?
94
+ matching_permissions.all? {|perm| perm.permit!(self)}
95
+ elsif !all_permissions.empty?
96
+ all_permissions.all? {|perm| perm.permit!(self)}
97
+ else
98
+ !DEFAULT_DENY
99
+ end
100
+ rescue AuthorizationError => e
101
+ auth_exception = e
102
+ end
103
+
104
+ unless allowed
105
+ if all_permissions.empty? and matching_permissions.empty?
106
+ logger.warn "Permission denied: No matching filter access " +
107
+ "rule found for #{self.class.controller_name}.#{action_name}"
108
+ elsif auth_exception
109
+ logger.info "Permission denied: #{auth_exception}"
110
+ end
111
+ if respond_to?(:permission_denied)
112
+ # permission_denied needs to render or redirect
113
+ send(:permission_denied)
114
+ else
115
+ send(:render, :text => "You are not allowed to access this action.",
116
+ :status => :forbidden)
117
+ end
118
+ end
119
+ end
120
+
121
+ def load_controller_object (context) # :nodoc:
122
+ instance_var = :"@#{context.to_s.singularize}"
123
+ model = context.to_s.classify.constantize
124
+ instance_variable_set(instance_var, model.find(params[:id]))
125
+ end
126
+
127
+ def load_parent_controller_object (parent_context) # :nodoc:
128
+ instance_var = :"@#{parent_context.to_s.singularize}"
129
+ model = parent_context.to_s.classify.constantize
130
+ instance_variable_set(instance_var, model.find(params[:"#{parent_context.to_s.singularize}_id"]))
131
+ end
132
+
133
+ def new_controller_object_from_params (context, parent_context) # :nodoc:
134
+ model_or_proxy = parent_context ?
135
+ instance_variable_get(:"@#{parent_context.to_s.singularize}").send(context.to_sym) :
136
+ context.to_s.classify.constantize
137
+ instance_var = :"@#{context.to_s.singularize}"
138
+ instance_variable_set(instance_var,
139
+ model_or_proxy.new(params[context.to_s.singularize]))
140
+ end
141
+
142
+ def new_controller_object_for_collection (context, parent_context) # :nodoc:
143
+ model_or_proxy = parent_context ?
144
+ instance_variable_get(:"@#{parent_context.to_s.singularize}").send(context.to_sym) :
145
+ context.to_s.classify.constantize
146
+ instance_var = :"@#{context.to_s.singularize}"
147
+ instance_variable_set(instance_var, model_or_proxy.new)
148
+ end
149
+
150
+ module ClassMethods
151
+ #
152
+ # Defines a filter to be applied according to the authorization of the
153
+ # current user. Requires at least one symbol corresponding to an
154
+ # action as parameter. The special symbol :+all+ refers to all action.
155
+ # The all :+all+ statement is only employed if no specific statement is
156
+ # present.
157
+ # class UserController < ApplicationController
158
+ # filter_access_to :index
159
+ # filter_access_to :new, :edit
160
+ # filter_access_to :all
161
+ # ...
162
+ # end
163
+ #
164
+ # The default is to allow access unconditionally if no rule matches.
165
+ # Thus, including the +filter_access_to+ :+all+ statement is a good
166
+ # idea, implementing a default-deny policy.
167
+ #
168
+ # When the access is denied, the method +permission_denied+ is called
169
+ # on the current controller, if defined. Else, a simple "you are not
170
+ # allowed" string is output. Log.info is given more information on the
171
+ # reasons of denial.
172
+ #
173
+ # def permission_denied
174
+ # flash[:error] = 'Sorry, you are not allowed to the requested page.'
175
+ # respond_to do |format|
176
+ # format.html { redirect_to(:back) rescue redirect_to('/') }
177
+ # format.xml { head :unauthorized }
178
+ # format.js { head :unauthorized }
179
+ # end
180
+ # end
181
+ #
182
+ # By default, required privileges are infered from the action name and
183
+ # the controller name. Thus, in UserController :+edit+ requires
184
+ # :+edit+ +users+. To specify required privilege, use the option :+require+
185
+ # filter_access_to :new, :create, :require => :create, :context => :users
186
+ #
187
+ # Without the :+attribute_check+ option, no constraints from the
188
+ # authorization rules are enforced because for some actions (collections,
189
+ # +new+, +create+), there is no object to evaluate conditions against. To
190
+ # allow attribute checks on all actions, it is a common pattern to provide
191
+ # custom objects through +before_filters+:
192
+ # class BranchesController < ApplicationController
193
+ # before_filter :load_company
194
+ # before_filter :new_branch_from_company_and_params,
195
+ # :only => [:index, :new, :create]
196
+ # filter_access_to :all, :attribute_check => true
197
+ #
198
+ # protected
199
+ # def new_branch_from_company_and_params
200
+ # @branch = @company.branches.new(params[:branch])
201
+ # end
202
+ # end
203
+ # NOTE: +before_filters+ need to be defined before the first
204
+ # +filter_access_to+ call.
205
+ #
206
+ # For further customization, a custom filter expression may be formulated
207
+ # in a block, which is then evaluated in the context of the controller
208
+ # on a matching request. That is, for checking two objects, use the
209
+ # following:
210
+ # filter_access_to :merge do
211
+ # permitted_to!(:update, User.find(params[:original_id])) and
212
+ # permitted_to!(:delete, User.find(params[:id]))
213
+ # end
214
+ # The block should raise a Authorization::AuthorizationError or return
215
+ # false if the access is to be denied.
216
+ #
217
+ # Later calls to filter_access_to with overlapping actions overwrite
218
+ # previous ones for that action.
219
+ #
220
+ # All options:
221
+ # [:+require+]
222
+ # Privilege required; defaults to action_name
223
+ # [:+context+]
224
+ # The privilege's context, defaults to controller_name, pluralized.
225
+ # [:+attribute_check+]
226
+ # Enables the check of attributes defined in the authorization rules.
227
+ # Defaults to false. If enabled, filter_access_to will use a context
228
+ # object from one of the following sources (in that order):
229
+ # * the method from the :+load_method+ option,
230
+ # * an instance variable named after the singular of the context
231
+ # (by default from the controller name, e.g. @post for PostsController),
232
+ # * a find on the context model, using +params+[:id] as id value.
233
+ # Any of these methods will only be employed if :+attribute_check+
234
+ # is enabled.
235
+ # [:+model+]
236
+ # The data model to load a context object from. Defaults to the
237
+ # context, singularized.
238
+ # [:+load_method+]
239
+ # Specify a method by symbol or a Proc object which should be used
240
+ # to load the object. Both should return the loaded object.
241
+ # If a Proc object is given, e.g. by way of
242
+ # +lambda+, it is called in the instance of the controller.
243
+ # Example demonstrating the default behaviour:
244
+ # filter_access_to :show, :attribute_check => true,
245
+ # :load_method => lambda { User.find(params[:id]) }
246
+ #
247
+
248
+ def filter_access_to (*args, &filter_block)
249
+ options = args.last.is_a?(Hash) ? args.pop : {}
250
+ options = {
251
+ :require => nil,
252
+ :context => nil,
253
+ :attribute_check => false,
254
+ :model => nil,
255
+ :load_method => nil
256
+ }.merge!(options)
257
+ privilege = options[:require]
258
+ context = options[:context]
259
+ actions = args.flatten
260
+
261
+ # collect permits in controller array for use in one before_filter
262
+ unless filter_chain.any? {|filter| filter.method == :filter_access_filter}
263
+ before_filter :filter_access_filter
264
+ end
265
+
266
+ filter_access_permissions.each do |perm|
267
+ perm.remove_actions(actions)
268
+ end
269
+ filter_access_permissions <<
270
+ ControllerPermission.new(actions, privilege, context,
271
+ options[:attribute_check],
272
+ options[:model],
273
+ options[:load_method],
274
+ filter_block)
275
+ end
276
+
277
+ # Collecting all the ControllerPermission objects from the controller
278
+ # hierarchy. Permissions for actions are overwritten by calls to
279
+ # filter_access_to in child controllers with the same action.
280
+ def all_filter_access_permissions # :nodoc:
281
+ ancestors.inject([]) do |perms, mod|
282
+ if mod.respond_to?(:filter_access_permissions)
283
+ perms +
284
+ mod.filter_access_permissions.collect do |p1|
285
+ p1.clone.remove_actions(perms.inject(Set.new) {|actions, p2| actions + p2.actions})
286
+ end
287
+ else
288
+ perms
289
+ end
290
+ end
291
+ end
292
+
293
+ # To DRY up the filter_access_to statements in restful controllers,
294
+ # filter_resource_access combines typical filter_access_to and
295
+ # before_filter calls, which set up the instance variables.
296
+ #
297
+ # The simplest case are top-level resource controllers with only the
298
+ # seven CRUD methods, e.g.
299
+ # class CompanyController < ApplicationController
300
+ # filter_resource_access
301
+ #
302
+ # def index...
303
+ # end
304
+ # Here, all CRUD actions are protected through a filter_access_to :all
305
+ # statement. :+attribute_check+ is enabled for all actions except for
306
+ # the collection action :+index+. To have an object for attribute checks
307
+ # available, filter_resource_access will set the instance variable
308
+ # @+company+ in before filters. For the member actions (:+show+, :+edit+,
309
+ # :+update+, :+destroy+) @company is set to Company.find(params[:id]).
310
+ # For +new+ actions (:+new+, :+create+), filter_resource_access creates
311
+ # a new object from company parameters: Company.new(params[:company].
312
+ #
313
+ # For nested resources, the parent object may be loaded automatically.
314
+ # class BranchController < ApplicationController
315
+ # filter_resource_access :nested_in => :companies
316
+ # end
317
+ # Again, the CRUD actions are protected. Now, for all CRUD actions,
318
+ # the parent object @company is loaded from params[:company_id]. It is
319
+ # also used when creating @branch for +new+ actions. Here, attribute_check
320
+ # is enabled for the collection :+index+ as well, checking attributes on a
321
+ # @company.branches.new method.
322
+ #
323
+ # You can override the default object loading by implementing any of the
324
+ # following instance methods on the controller. Examples are given for the
325
+ # BranchController (with +nested_in+ set to :+companies+):
326
+ # [+new_branch_from_params+]
327
+ # Used for +new+ actions.
328
+ # [+new_branch_for_collection+]
329
+ # Used for +collection+ actions if the +nested_in+ option is set.
330
+ # [+load_branch+]
331
+ # Used for +member+ actions.
332
+ # [+load_company+]
333
+ # Used for all +new+, +member+, and +collection+ actions if the
334
+ # +nested_in+ option is set.
335
+ #
336
+ # All options:
337
+ # [:+member+]
338
+ # Member methods are actions like +show+, which have an params[:id] from
339
+ # which to load the controller object and assign it to @controller_name,
340
+ # e.g. @+branch+. By default, member actions are [:+show+, :+edit+, :+update+,
341
+ # :+destroy+].
342
+ # [:+additional_member+]
343
+ # Allows to add additional member actions to the default resource +member+
344
+ # actions.
345
+ # [:+collection+]
346
+ # Collection actions are like :+index+, actions without any controller object
347
+ # to check attributes of. If +nested_in+ is given, a new object is
348
+ # created from the parent object, e.g. @company.branches.new. Without
349
+ # +nested_in+, attribute check is deactivated for these actions. By
350
+ # default, collection is set to :+index+.
351
+ # [:+additional_collection+]
352
+ # Allows to add additional collaction actions to the default resource +collection+
353
+ # actions.
354
+ # [:+new+]
355
+ # +new+ methods are actions such as +new+ and +create+, which don't
356
+ # receive a params[:id] to load an object from, but
357
+ # a params[:controller_name_singular] hash with attributes for a new
358
+ # object. The attributes will be used here to create a new object and
359
+ # check the object against the authorization rules. The object is
360
+ # assigned to @controller_name_singular, e.g. @branch.
361
+ #
362
+ # If +nested_in+ is given, the new object
363
+ # is created from the parent_object.controller_name
364
+ # proxy, e.g. company.branches.new(params[:branch]). By default,
365
+ # +new+ is set to [:new, :create].
366
+ # [:+additional_new+]
367
+ # Allows to add additional new actions to the default resource +new+ actions.
368
+ # [:+context+]
369
+ # The context is used to determine the model to load objects from for the
370
+ # before_filters and the context of privileges to use in authorization
371
+ # checks.
372
+ # [:+nested_in+]
373
+ # Specifies the parent controller if the resource is nested in another
374
+ # one. This is used to automatically load the parent object, e.g.
375
+ # @+company+ from params[:company_id] for a BranchController nested in
376
+ # a CompanyController.
377
+ # [:+no_attribute_check+]
378
+ # Allows to set actions for which no attribute check should be perfomed.
379
+ # See filter_access_to on details. By default, with no +nested_in+,
380
+ # +no_attribute_check+ is set to all collections. If +nested_in+ is given
381
+ # +no_attribute_check+ is empty by default.
382
+ #
383
+ def filter_resource_access(options = {})
384
+ options = {
385
+ :new => [:new, :create],
386
+ :additional_new => nil,
387
+ :member => [:show, :edit, :update, :destroy],
388
+ :additional_member => nil,
389
+ :collection => [:index],
390
+ :additional_collection => nil,
391
+ #:new_method_for_collection => nil, # only symbol method name
392
+ #:new_method => nil, # only symbol method name
393
+ #:load_method => nil, # only symbol method name
394
+ :no_attribute_check => nil,
395
+ :context => nil,
396
+ :nested_in => nil,
397
+ }.merge(options)
398
+
399
+ new_actions = actions_from_option(options[:new]).merge(
400
+ actions_from_option(options[:additional_new]))
401
+ members = actions_from_option(options[:member]).merge(
402
+ actions_from_option(options[:additional_member]))
403
+ collections = actions_from_option(options[:collection]).merge(
404
+ actions_from_option(options[:additional_collection]))
405
+
406
+ options[:no_attribute_check] ||= collections.keys unless options[:nested_in]
407
+
408
+ unless options[:nested_in].blank?
409
+ load_method = :"load_#{options[:nested_in].to_s.singularize}"
410
+ before_filter do |controller|
411
+ if controller.respond_to?(load_method)
412
+ controller.send(load_method)
413
+ else
414
+ controller.send(:load_parent_controller_object, options[:nested_in])
415
+ end
416
+ end
417
+
418
+ new_for_collection_method = :"new_#{controller_name.singularize}_for_collection"
419
+ before_filter :only => collections.keys do |controller|
420
+ # new_for_collection
421
+ if controller.respond_to?(new_for_collection_method)
422
+ controller.send(new_for_collection_method)
423
+ else
424
+ controller.send(:new_controller_object_for_collection,
425
+ options[:context] || controller_name, options[:nested_in])
426
+ end
427
+ end
428
+ end
429
+
430
+ new_from_params_method = :"new_#{controller_name.singularize}_from_params"
431
+ before_filter :only => new_actions.keys do |controller|
432
+ # new_from_params
433
+ if controller.respond_to?(new_from_params_method)
434
+ controller.send(new_from_params_method)
435
+ else
436
+ controller.send(:new_controller_object_from_params,
437
+ options[:context] || controller_name, options[:nested_in])
438
+ end
439
+ end
440
+ load_method = :"load_#{controller_name.singularize}"
441
+ before_filter :only => members.keys do |controller|
442
+ # load controller object
443
+ if controller.respond_to?(load_method)
444
+ controller.send(load_method)
445
+ else
446
+ controller.send(:load_controller_object, options[:context] || controller_name)
447
+ end
448
+ end
449
+ filter_access_to :all, :attribute_check => true, :context => options[:context]
450
+
451
+ members.merge(new_actions).merge(collections).each do |action, privilege|
452
+ if action != privilege or (options[:no_attribute_check] and options[:no_attribute_check].include?(action))
453
+ filter_options = {
454
+ :context => options[:context],
455
+ :attribute_check => !options[:no_attribute_check] || !options[:no_attribute_check].include?(action)
456
+ }
457
+ filter_options[:require] = privilege if action != privilege
458
+ filter_access_to(action, filter_options)
459
+ end
460
+ end
461
+ end
462
+
463
+ protected
464
+ def filter_access_permissions # :nodoc:
465
+ unless filter_access_permissions?
466
+ ancestors[1..-1].reverse.each do |mod|
467
+ mod.filter_access_permissions if mod.respond_to?(:filter_access_permissions)
468
+ end
469
+ end
470
+ class_variable_set(:@@declarative_authorization_permissions, {}) unless filter_access_permissions?
471
+ class_variable_get(:@@declarative_authorization_permissions)[self.name] ||= []
472
+ end
473
+
474
+ def filter_access_permissions? # :nodoc:
475
+ class_variable_defined?(:@@declarative_authorization_permissions)
476
+ end
477
+
478
+ def actions_from_option (option) # :nodoc:
479
+ case option
480
+ when nil
481
+ {}
482
+ when Symbol, String
483
+ {option.to_sym => option.to_sym}
484
+ when Hash
485
+ option
486
+ when Enumerable
487
+ option.each_with_object({}) do |action, hash|
488
+ if action.is_a?(Array)
489
+ raise "Unexpected option format: #{option.inspect}" if action.length != 2
490
+ hash[action.first] = action.last
491
+ else
492
+ hash[action.to_sym] = action.to_sym
493
+ end
494
+ end
495
+ end
496
+ end
497
+ end
498
+ end
499
+
500
+ class ControllerPermission # :nodoc:
501
+ attr_reader :actions, :privilege, :context, :attribute_check
502
+ def initialize (actions, privilege, context, attribute_check = false,
503
+ load_object_model = nil, load_object_method = nil,
504
+ filter_block = nil)
505
+ @actions = actions.to_set
506
+ @privilege = privilege
507
+ @context = context
508
+ @load_object_model = load_object_model
509
+ @load_object_method = load_object_method
510
+ @filter_block = filter_block
511
+ @attribute_check = attribute_check
512
+ end
513
+
514
+ def matches? (action_name)
515
+ @actions.include?(action_name.to_sym)
516
+ end
517
+
518
+ def permit! (contr)
519
+ if @filter_block
520
+ return contr.instance_eval(&@filter_block)
521
+ end
522
+ context = @context || contr.class.controller_name.to_sym
523
+ object = @attribute_check ? load_object(contr, context) : nil
524
+ privilege = @privilege || :"#{contr.action_name}"
525
+
526
+ #puts "Trying permit?(#{privilege.inspect}, "
527
+ #puts " :user => #{contr.send(:current_user).inspect}, "
528
+ #puts " :object => #{object.inspect},"
529
+ #puts " :skip_attribute_test => #{!@attribute_check},"
530
+ #puts " :context => #{contr.class.controller_name.pluralize.to_sym})"
531
+ res = contr.authorization_engine.permit!(privilege,
532
+ :user => contr.send(:current_user),
533
+ :object => object,
534
+ :skip_attribute_test => !@attribute_check,
535
+ :context => context)
536
+ #puts "permit? result: #{res.inspect}"
537
+ res
538
+ end
539
+
540
+ def remove_actions (actions)
541
+ @actions -= actions
542
+ self
543
+ end
544
+
545
+ private
546
+ def load_object(contr, context)
547
+ if @load_object_method and @load_object_method.is_a?(Symbol)
548
+ contr.send(@load_object_method)
549
+ elsif @load_object_method and @load_object_method.is_a?(Proc)
550
+ contr.instance_eval(&@load_object_method)
551
+ else
552
+ load_object_model = @load_object_model || context.to_s.classify.constantize
553
+ instance_var = :"@#{load_object_model.name.underscore}"
554
+ object = contr.instance_variable_get(instance_var)
555
+ unless object
556
+ begin
557
+ object = load_object_model.find(contr.params[:id])
558
+ rescue ActiveRecord::RecordNotFound
559
+ logger.debug("filter_access_to tried to find " +
560
+ "#{load_object_model.inspect} from params[:id] " +
561
+ "(#{contr.params[:id].inspect}), because attribute_check is enabled " +
562
+ "and #{instance_var.to_s} isn't set.")
563
+ raise
564
+ end
565
+ contr.instance_variable_set(instance_var, object)
566
+ end
567
+ object
568
+ end
569
+ end
570
+ end
571
+ end