ghart-declarative_authorization 0.3.2.4
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.
- data/CHANGELOG +83 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +510 -0
- data/Rakefile +43 -0
- data/app/controllers/authorization_rules_controller.rb +259 -0
- data/app/controllers/authorization_usages_controller.rb +23 -0
- data/app/helpers/authorization_rules_helper.rb +187 -0
- data/app/views/authorization_rules/_change.erb +58 -0
- data/app/views/authorization_rules/_show_graph.erb +37 -0
- data/app/views/authorization_rules/_suggestions.erb +48 -0
- data/app/views/authorization_rules/change.html.erb +152 -0
- data/app/views/authorization_rules/graph.dot.erb +68 -0
- data/app/views/authorization_rules/graph.html.erb +40 -0
- data/app/views/authorization_rules/index.html.erb +17 -0
- data/app/views/authorization_usages/index.html.erb +36 -0
- data/authorization_rules.dist.rb +20 -0
- data/config/routes.rb +7 -0
- data/garlic_example.rb +20 -0
- data/init.rb +5 -0
- data/lib/declarative_authorization.rb +15 -0
- data/lib/declarative_authorization/authorization.rb +634 -0
- data/lib/declarative_authorization/development_support/analyzer.rb +252 -0
- data/lib/declarative_authorization/development_support/change_analyzer.rb +253 -0
- data/lib/declarative_authorization/development_support/change_supporter.rb +620 -0
- data/lib/declarative_authorization/development_support/development_support.rb +243 -0
- data/lib/declarative_authorization/helper.rb +60 -0
- data/lib/declarative_authorization/in_controller.rb +597 -0
- data/lib/declarative_authorization/in_model.rb +159 -0
- data/lib/declarative_authorization/maintenance.rb +182 -0
- data/lib/declarative_authorization/obligation_scope.rb +308 -0
- data/lib/declarative_authorization/rails_legacy.rb +14 -0
- data/lib/declarative_authorization/reader.rb +441 -0
- data/test/authorization_test.rb +827 -0
- data/test/controller_filter_resource_access_test.rb +394 -0
- data/test/controller_test.rb +386 -0
- data/test/dsl_reader_test.rb +157 -0
- data/test/helper_test.rb +171 -0
- data/test/maintenance_test.rb +46 -0
- data/test/model_test.rb +1308 -0
- data/test/schema.sql +54 -0
- data/test/test_helper.rb +118 -0
- 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,597 @@
|
|
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
|
+
# In many cases, the default seven CRUD actions are not sufficient. As in
|
324
|
+
# the resource definition for routing you may thus give additional member,
|
325
|
+
# new and collection methods. The options allow you to specify the
|
326
|
+
# required privileges for each action by providing a hash or an array of
|
327
|
+
# pairs. By default, for each action the action name is taken as privilege
|
328
|
+
# (action search in the example below requires the privilege :index
|
329
|
+
# :companies). Any controller action that is not specified and does not
|
330
|
+
# belong to the seven CRUD actions is handled as a member method.
|
331
|
+
# class CompanyController < ApplicationController
|
332
|
+
# filter_resource_access :collection => [[:search, :index], :index],
|
333
|
+
# :additional_member => {:mark_as_key_company => :update}
|
334
|
+
# end
|
335
|
+
# The +additional_+* options add to the respective CRUD actions,
|
336
|
+
# the other options replace the respective CRUD actions.
|
337
|
+
#
|
338
|
+
# You can override the default object loading by implementing any of the
|
339
|
+
# following instance methods on the controller. Examples are given for the
|
340
|
+
# BranchController (with +nested_in+ set to :+companies+):
|
341
|
+
# [+new_branch_from_params+]
|
342
|
+
# Used for +new+ actions.
|
343
|
+
# [+new_branch_for_collection+]
|
344
|
+
# Used for +collection+ actions if the +nested_in+ option is set.
|
345
|
+
# [+load_branch+]
|
346
|
+
# Used for +member+ actions.
|
347
|
+
# [+load_company+]
|
348
|
+
# Used for all +new+, +member+, and +collection+ actions if the
|
349
|
+
# +nested_in+ option is set.
|
350
|
+
#
|
351
|
+
# All options:
|
352
|
+
# [:+member+]
|
353
|
+
# Member methods are actions like +show+, which have an params[:id] from
|
354
|
+
# which to load the controller object and assign it to @controller_name,
|
355
|
+
# e.g. @+branch+.
|
356
|
+
#
|
357
|
+
# By default, member actions are [:+show+, :+edit+, :+update+,
|
358
|
+
# :+destroy+]. Also, any action not belonging to the seven CRUD actions
|
359
|
+
# are handled as member actions.
|
360
|
+
#
|
361
|
+
# There are three different syntax to specify member, collection and
|
362
|
+
# new actions.
|
363
|
+
# * Hash: Lets you set the required privilege for each action:
|
364
|
+
# {:+show+ => :+show+, :+mark_as_important+ => :+update+}
|
365
|
+
# * Array of actions or pairs: [:+show+, [:+mark_as_important+, :+update+]],
|
366
|
+
# with single actions requiring the privilege of the same name as the method.
|
367
|
+
# * Single method symbol: :+show+
|
368
|
+
# [:+additional_member+]
|
369
|
+
# Allows to add additional member actions to the default resource +member+
|
370
|
+
# actions.
|
371
|
+
# [:+collection+]
|
372
|
+
# Collection actions are like :+index+, actions without any controller object
|
373
|
+
# to check attributes of. If +nested_in+ is given, a new object is
|
374
|
+
# created from the parent object, e.g. @company.branches.new. Without
|
375
|
+
# +nested_in+, attribute check is deactivated for these actions. By
|
376
|
+
# default, collection is set to :+index+.
|
377
|
+
# [:+additional_collection+]
|
378
|
+
# Allows to add additional collaction actions to the default resource +collection+
|
379
|
+
# actions.
|
380
|
+
# [:+new+]
|
381
|
+
# +new+ methods are actions such as +new+ and +create+, which don't
|
382
|
+
# receive a params[:id] to load an object from, but
|
383
|
+
# a params[:controller_name_singular] hash with attributes for a new
|
384
|
+
# object. The attributes will be used here to create a new object and
|
385
|
+
# check the object against the authorization rules. The object is
|
386
|
+
# assigned to @controller_name_singular, e.g. @branch.
|
387
|
+
#
|
388
|
+
# If +nested_in+ is given, the new object
|
389
|
+
# is created from the parent_object.controller_name
|
390
|
+
# proxy, e.g. company.branches.new(params[:branch]). By default,
|
391
|
+
# +new+ is set to [:new, :create].
|
392
|
+
# [:+additional_new+]
|
393
|
+
# Allows to add additional new actions to the default resource +new+ actions.
|
394
|
+
# [:+context+]
|
395
|
+
# The context is used to determine the model to load objects from for the
|
396
|
+
# before_filters and the context of privileges to use in authorization
|
397
|
+
# checks.
|
398
|
+
# [:+nested_in+]
|
399
|
+
# Specifies the parent controller if the resource is nested in another
|
400
|
+
# one. This is used to automatically load the parent object, e.g.
|
401
|
+
# @+company+ from params[:company_id] for a BranchController nested in
|
402
|
+
# a CompanyController.
|
403
|
+
# [:+no_attribute_check+]
|
404
|
+
# Allows to set actions for which no attribute check should be perfomed.
|
405
|
+
# See filter_access_to on details. By default, with no +nested_in+,
|
406
|
+
# +no_attribute_check+ is set to all collections. If +nested_in+ is given
|
407
|
+
# +no_attribute_check+ is empty by default.
|
408
|
+
#
|
409
|
+
def filter_resource_access(options = {})
|
410
|
+
options = {
|
411
|
+
:new => [:new, :create],
|
412
|
+
:additional_new => nil,
|
413
|
+
:member => [:show, :edit, :update, :destroy],
|
414
|
+
:additional_member => nil,
|
415
|
+
:collection => [:index],
|
416
|
+
:additional_collection => nil,
|
417
|
+
#:new_method_for_collection => nil, # only symbol method name
|
418
|
+
#:new_method => nil, # only symbol method name
|
419
|
+
#:load_method => nil, # only symbol method name
|
420
|
+
:no_attribute_check => nil,
|
421
|
+
:context => nil,
|
422
|
+
:nested_in => nil,
|
423
|
+
}.merge(options)
|
424
|
+
|
425
|
+
new_actions = actions_from_option(options[:new]).merge(
|
426
|
+
actions_from_option(options[:additional_new]))
|
427
|
+
members = actions_from_option(options[:member]).merge(
|
428
|
+
actions_from_option(options[:additional_member]))
|
429
|
+
collections = actions_from_option(options[:collection]).merge(
|
430
|
+
actions_from_option(options[:additional_collection]))
|
431
|
+
|
432
|
+
options[:no_attribute_check] ||= collections.keys unless options[:nested_in]
|
433
|
+
|
434
|
+
unless options[:nested_in].blank?
|
435
|
+
load_method = :"load_#{options[:nested_in].to_s.singularize}"
|
436
|
+
before_filter do |controller|
|
437
|
+
if controller.respond_to?(load_method)
|
438
|
+
controller.send(load_method)
|
439
|
+
else
|
440
|
+
controller.send(:load_parent_controller_object, options[:nested_in])
|
441
|
+
end
|
442
|
+
end
|
443
|
+
|
444
|
+
new_for_collection_method = :"new_#{controller_name.singularize}_for_collection"
|
445
|
+
before_filter :only => collections.keys do |controller|
|
446
|
+
# new_for_collection
|
447
|
+
if controller.respond_to?(new_for_collection_method)
|
448
|
+
controller.send(new_for_collection_method)
|
449
|
+
else
|
450
|
+
controller.send(:new_controller_object_for_collection,
|
451
|
+
options[:context] || controller_name, options[:nested_in])
|
452
|
+
end
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
new_from_params_method = :"new_#{controller_name.singularize}_from_params"
|
457
|
+
before_filter :only => new_actions.keys do |controller|
|
458
|
+
# new_from_params
|
459
|
+
if controller.respond_to?(new_from_params_method)
|
460
|
+
controller.send(new_from_params_method)
|
461
|
+
else
|
462
|
+
controller.send(:new_controller_object_from_params,
|
463
|
+
options[:context] || controller_name, options[:nested_in])
|
464
|
+
end
|
465
|
+
end
|
466
|
+
load_method = :"load_#{controller_name.singularize}"
|
467
|
+
before_filter :only => members.keys do |controller|
|
468
|
+
# load controller object
|
469
|
+
if controller.respond_to?(load_method)
|
470
|
+
controller.send(load_method)
|
471
|
+
else
|
472
|
+
controller.send(:load_controller_object, options[:context] || controller_name)
|
473
|
+
end
|
474
|
+
end
|
475
|
+
filter_access_to :all, :attribute_check => true, :context => options[:context]
|
476
|
+
|
477
|
+
members.merge(new_actions).merge(collections).each do |action, privilege|
|
478
|
+
if action != privilege or (options[:no_attribute_check] and options[:no_attribute_check].include?(action))
|
479
|
+
filter_options = {
|
480
|
+
:context => options[:context],
|
481
|
+
:attribute_check => !options[:no_attribute_check] || !options[:no_attribute_check].include?(action)
|
482
|
+
}
|
483
|
+
filter_options[:require] = privilege if action != privilege
|
484
|
+
filter_access_to(action, filter_options)
|
485
|
+
end
|
486
|
+
end
|
487
|
+
end
|
488
|
+
|
489
|
+
protected
|
490
|
+
def filter_access_permissions # :nodoc:
|
491
|
+
unless filter_access_permissions?
|
492
|
+
ancestors[1..-1].reverse.each do |mod|
|
493
|
+
mod.filter_access_permissions if mod.respond_to?(:filter_access_permissions)
|
494
|
+
end
|
495
|
+
end
|
496
|
+
class_variable_set(:@@declarative_authorization_permissions, {}) unless filter_access_permissions?
|
497
|
+
class_variable_get(:@@declarative_authorization_permissions)[self.name] ||= []
|
498
|
+
end
|
499
|
+
|
500
|
+
def filter_access_permissions? # :nodoc:
|
501
|
+
class_variable_defined?(:@@declarative_authorization_permissions)
|
502
|
+
end
|
503
|
+
|
504
|
+
def actions_from_option (option) # :nodoc:
|
505
|
+
case option
|
506
|
+
when nil
|
507
|
+
{}
|
508
|
+
when Symbol, String
|
509
|
+
{option.to_sym => option.to_sym}
|
510
|
+
when Hash
|
511
|
+
option
|
512
|
+
when Enumerable
|
513
|
+
option.each_with_object({}) do |action, hash|
|
514
|
+
if action.is_a?(Array)
|
515
|
+
raise "Unexpected option format: #{option.inspect}" if action.length != 2
|
516
|
+
hash[action.first] = action.last
|
517
|
+
else
|
518
|
+
hash[action.to_sym] = action.to_sym
|
519
|
+
end
|
520
|
+
end
|
521
|
+
end
|
522
|
+
end
|
523
|
+
end
|
524
|
+
end
|
525
|
+
|
526
|
+
class ControllerPermission # :nodoc:
|
527
|
+
attr_reader :actions, :privilege, :context, :attribute_check
|
528
|
+
def initialize (actions, privilege, context, attribute_check = false,
|
529
|
+
load_object_model = nil, load_object_method = nil,
|
530
|
+
filter_block = nil)
|
531
|
+
@actions = actions.to_set
|
532
|
+
@privilege = privilege
|
533
|
+
@context = context
|
534
|
+
@load_object_model = load_object_model
|
535
|
+
@load_object_method = load_object_method
|
536
|
+
@filter_block = filter_block
|
537
|
+
@attribute_check = attribute_check
|
538
|
+
end
|
539
|
+
|
540
|
+
def matches? (action_name)
|
541
|
+
@actions.include?(action_name.to_sym)
|
542
|
+
end
|
543
|
+
|
544
|
+
def permit! (contr)
|
545
|
+
if @filter_block
|
546
|
+
return contr.instance_eval(&@filter_block)
|
547
|
+
end
|
548
|
+
context = @context || contr.class.controller_name.to_sym
|
549
|
+
object = @attribute_check ? load_object(contr, context) : nil
|
550
|
+
privilege = @privilege || :"#{contr.action_name}"
|
551
|
+
|
552
|
+
#puts "Trying permit?(#{privilege.inspect}, "
|
553
|
+
#puts " :user => #{contr.send(:current_user).inspect}, "
|
554
|
+
#puts " :object => #{object.inspect},"
|
555
|
+
#puts " :skip_attribute_test => #{!@attribute_check},"
|
556
|
+
#puts " :context => #{contr.class.controller_name.pluralize.to_sym})"
|
557
|
+
res = contr.authorization_engine.permit!(privilege,
|
558
|
+
:user => contr.send(:current_user),
|
559
|
+
:object => object,
|
560
|
+
:skip_attribute_test => !@attribute_check,
|
561
|
+
:context => (@context || contr.class.name.gsub(/::/, '_').gsub(/Controller$/, "").underscore.to_sym))
|
562
|
+
#puts "permit? result: #{res.inspect}"
|
563
|
+
res
|
564
|
+
end
|
565
|
+
|
566
|
+
def remove_actions (actions)
|
567
|
+
@actions -= actions
|
568
|
+
self
|
569
|
+
end
|
570
|
+
|
571
|
+
private
|
572
|
+
def load_object(contr, context)
|
573
|
+
if @load_object_method and @load_object_method.is_a?(Symbol)
|
574
|
+
contr.send(@load_object_method)
|
575
|
+
elsif @load_object_method and @load_object_method.is_a?(Proc)
|
576
|
+
contr.instance_eval(&@load_object_method)
|
577
|
+
else
|
578
|
+
load_object_model = @load_object_model || context.to_s.classify.constantize
|
579
|
+
instance_var = :"@#{load_object_model.name.underscore}"
|
580
|
+
object = contr.instance_variable_get(instance_var)
|
581
|
+
unless object
|
582
|
+
begin
|
583
|
+
object = load_object_model.find(contr.params[:id])
|
584
|
+
rescue ActiveRecord::RecordNotFound, RuntimeError
|
585
|
+
contr.logger.debug("filter_access_to tried to find " +
|
586
|
+
"#{load_object_model.inspect} from params[:id] " +
|
587
|
+
"(#{contr.params[:id].inspect}), because attribute_check is enabled " +
|
588
|
+
"and #{instance_var.to_s} isn't set.")
|
589
|
+
raise
|
590
|
+
end
|
591
|
+
contr.instance_variable_set(instance_var, object)
|
592
|
+
end
|
593
|
+
object
|
594
|
+
end
|
595
|
+
end
|
596
|
+
end
|
597
|
+
end
|