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,385 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class BasicResource < MockDataObject
4
+ end
5
+ class BasicResourcesController < MocksController
6
+ filter_resource_access
7
+ define_resource_actions
8
+ end
9
+ class BasicResourcesControllerTest < ActionController::TestCase
10
+ def test_basic_filter_index
11
+ reader = Authorization::Reader::DSLReader.new
12
+ reader.parse %{
13
+ authorization do
14
+ role :allowed_role do
15
+ has_permission_on :basic_resources, :to => :index do
16
+ if_attribute :id => is {"1"}
17
+ end
18
+ end
19
+ end
20
+ }
21
+
22
+ allowed_user = MockUser.new(:allowed_role)
23
+ request!(MockUser.new(:another_role), :index, reader)
24
+ assert !@controller.authorized?
25
+ request!(allowed_user, :index, reader)
26
+ assert @controller.authorized?
27
+ end
28
+
29
+ def test_basic_filter_show_with_id
30
+ reader = Authorization::Reader::DSLReader.new
31
+ reader.parse %{
32
+ authorization do
33
+ role :allowed_role do
34
+ has_permission_on :basic_resources, :to => :show do
35
+ if_attribute :id => is {"1"}
36
+ end
37
+ end
38
+ end
39
+ }
40
+
41
+ allowed_user = MockUser.new(:allowed_role)
42
+ request!(allowed_user, :show, reader, :id => "2")
43
+ assert !@controller.authorized?
44
+ request!(allowed_user, :show, reader, :id => "1", :clear => [:@basic_resource])
45
+ assert @controller.authorized?
46
+ end
47
+
48
+ def test_basic_filter_new_with_params
49
+ reader = Authorization::Reader::DSLReader.new
50
+ reader.parse %{
51
+ authorization do
52
+ role :allowed_role do
53
+ has_permission_on :basic_resources, :to => :new do
54
+ if_attribute :id => is {"1"}
55
+ end
56
+ end
57
+ end
58
+ }
59
+
60
+ allowed_user = MockUser.new(:allowed_role)
61
+ request!(allowed_user, :new, reader, :basic_resource => {:id => "2"})
62
+ assert !@controller.authorized?
63
+ request!(allowed_user, :new, reader, :basic_resource => {:id => "1"},
64
+ :clear => [:@basic_resource])
65
+ assert @controller.authorized?
66
+ end
67
+ end
68
+
69
+
70
+ class NestedResource < MockDataObject
71
+ def initialize (attributes = {})
72
+ if attributes[:id]
73
+ attributes[:parent_mock] ||= ParentMock.new(:id => attributes[:id])
74
+ end
75
+ super(attributes)
76
+ end
77
+ end
78
+ class ParentMock < MockDataObject
79
+ def nested_resources
80
+ Class.new do
81
+ def initialize (parent_mock)
82
+ @parent_mock = parent_mock
83
+ end
84
+ def new (attributes = {})
85
+ NestedResource.new(attributes.merge(:parent_mock => @parent_mock))
86
+ end
87
+ end.new(self)
88
+ end
89
+
90
+ def == (other)
91
+ id == other.id
92
+ end
93
+ end
94
+ class NestedResourcesController < MocksController
95
+ filter_resource_access :nested_in => :parent_mocks
96
+ define_resource_actions
97
+ end
98
+ class NestedResourcesControllerTest < ActionController::TestCase
99
+ def test_nested_filter_index
100
+ reader = Authorization::Reader::DSLReader.new
101
+ reader.parse %{
102
+ authorization do
103
+ role :allowed_role do
104
+ has_permission_on :nested_resources, :to => :index do
105
+ if_attribute :parent_mock => is {ParentMock.find("1")}
106
+ end
107
+ end
108
+ end
109
+ }
110
+
111
+ allowed_user = MockUser.new(:allowed_role)
112
+ request!(MockUser.new(:another_role), :index, reader, :parent_mock_id => "2")
113
+ assert !@controller.authorized?
114
+ request!(allowed_user, :index, reader, :parent_mock_id => "2",
115
+ :clear => [:@nested_resource, :@parent_mock])
116
+ assert !@controller.authorized?
117
+ request!(allowed_user, :index, reader, :parent_mock_id => "1",
118
+ :clear => [:@nested_resource, :@parent_mock])
119
+ assert @controller.authorized?
120
+ end
121
+
122
+ def test_nested_filter_show_with_id
123
+ reader = Authorization::Reader::DSLReader.new
124
+ reader.parse %{
125
+ authorization do
126
+ role :allowed_role do
127
+ has_permission_on :nested_resources, :to => :show do
128
+ if_attribute :parent_mock => is {ParentMock.find("1")}
129
+ end
130
+ end
131
+ end
132
+ }
133
+
134
+ allowed_user = MockUser.new(:allowed_role)
135
+ request!(allowed_user, :show, reader, :id => "2", :parent_mock_id => "2")
136
+ assert !@controller.authorized?
137
+ request!(allowed_user, :show, reader, :id => "1", :parent_mock_id => "1",
138
+ :clear => [:@nested_resource, :@parent_mock])
139
+ assert @controller.authorized?
140
+ end
141
+
142
+ def test_nested_filter_new_with_params
143
+ reader = Authorization::Reader::DSLReader.new
144
+ reader.parse %{
145
+ authorization do
146
+ role :allowed_role do
147
+ has_permission_on :nested_resources, :to => :new do
148
+ if_attribute :parent_mock => is {ParentMock.find("1")}
149
+ end
150
+ end
151
+ end
152
+ }
153
+
154
+ allowed_user = MockUser.new(:allowed_role)
155
+ request!(allowed_user, :new, reader, :parent_mock_id => "2",
156
+ :nested_resource => {:id => "2"})
157
+ assert !@controller.authorized?
158
+ request!(allowed_user, :new, reader, :parent_mock_id => "1",
159
+ :nested_resource => {:id => "1"},
160
+ :clear => [:@nested_resource, :@parent_mock])
161
+ assert @controller.authorized?
162
+ end
163
+ end
164
+
165
+
166
+ class CustomMembersCollectionsResourceController < MocksController
167
+ def self.controller_name
168
+ "basic_resources"
169
+ end
170
+ filter_resource_access :member => [[:other_show, :read]],
171
+ :collection => {:search => :read}, :new => [:other_new]
172
+ define_action_methods :other_new, :search, :other_show
173
+ end
174
+ class CustomMembersCollectionsResourceControllerTest < ActionController::TestCase
175
+ def test_custom_members_filter_search
176
+ reader = Authorization::Reader::DSLReader.new
177
+ reader.parse %{
178
+ authorization do
179
+ role :allowed_role do
180
+ has_permission_on :basic_resources, :to => :read do
181
+ if_attribute :id => is {"1"}
182
+ end
183
+ end
184
+ end
185
+ }
186
+
187
+ request!(MockUser.new(:another_role), :search, reader)
188
+ assert !@controller.authorized?
189
+ request!(MockUser.new(:allowed_role), :search, reader)
190
+ assert @controller.authorized?
191
+ end
192
+
193
+ def test_custom_members_filter_other_show
194
+ reader = Authorization::Reader::DSLReader.new
195
+ reader.parse %{
196
+ authorization do
197
+ role :allowed_role do
198
+ has_permission_on :basic_resources, :to => :read do
199
+ if_attribute :id => is {"1"}
200
+ end
201
+ end
202
+ end
203
+ }
204
+
205
+ allowed_user = MockUser.new(:allowed_role)
206
+ request!(allowed_user, :other_show, reader, :id => "2")
207
+ assert !@controller.authorized?
208
+ request!(allowed_user, :other_show, reader, :id => "1", :clear => [:@basic_resource])
209
+ assert @controller.authorized?
210
+ end
211
+
212
+ def test_custom_members_filter_other_new
213
+ reader = Authorization::Reader::DSLReader.new
214
+ reader.parse %{
215
+ authorization do
216
+ role :allowed_role do
217
+ has_permission_on :basic_resources, :to => :other_new do
218
+ if_attribute :id => is {"1"}
219
+ end
220
+ end
221
+ end
222
+ }
223
+
224
+ allowed_user = MockUser.new(:allowed_role)
225
+ request!(allowed_user, :other_new, reader, :basic_resource => {:id => "2"})
226
+ assert !@controller.authorized?
227
+ request!(allowed_user, :other_new, reader, :basic_resource => {:id => "1"},
228
+ :clear => [:@basic_resource])
229
+ assert @controller.authorized?
230
+ end
231
+ end
232
+
233
+
234
+ class AdditionalMembersCollectionsResourceController < MocksController
235
+ def self.controller_name
236
+ "basic_resources"
237
+ end
238
+ filter_resource_access :additional_member => :other_show,
239
+ :additional_collection => [:search], :additional_new => {:other_new => :new}
240
+ define_resource_actions
241
+ define_action_methods :other_new, :search, :other_show
242
+ end
243
+ class AdditionalMembersCollectionsResourceControllerTest < ActionController::TestCase
244
+ def test_additional_members_filter_search_index
245
+ reader = Authorization::Reader::DSLReader.new
246
+ reader.parse %{
247
+ authorization do
248
+ role :allowed_role do
249
+ has_permission_on :basic_resources, :to => [:search, :index] do
250
+ if_attribute :id => is {"1"}
251
+ end
252
+ end
253
+ end
254
+ }
255
+
256
+ request!(MockUser.new(:another_role), :search, reader)
257
+ assert !@controller.authorized?
258
+ request!(MockUser.new(:another_role), :index, reader)
259
+ assert !@controller.authorized?
260
+ request!(MockUser.new(:allowed_role), :search, reader)
261
+ assert @controller.authorized?
262
+ request!(MockUser.new(:allowed_role), :index, reader)
263
+ assert @controller.authorized?
264
+ end
265
+
266
+ def test_additional_members_filter_other_show
267
+ reader = Authorization::Reader::DSLReader.new
268
+ reader.parse %{
269
+ authorization do
270
+ role :allowed_role do
271
+ has_permission_on :basic_resources, :to => [:show, :other_show] do
272
+ if_attribute :id => is {"1"}
273
+ end
274
+ end
275
+ end
276
+ }
277
+
278
+ allowed_user = MockUser.new(:allowed_role)
279
+ request!(allowed_user, :other_show, reader, :id => "2")
280
+ assert !@controller.authorized?
281
+ request!(allowed_user, :show, reader, :id => "2", :clear => [:@basic_resource])
282
+ assert !@controller.authorized?
283
+ request!(allowed_user, :other_show, reader, :id => "1", :clear => [:@basic_resource])
284
+ assert @controller.authorized?
285
+ request!(allowed_user, :show, reader, :id => "1", :clear => [:@basic_resource])
286
+ assert @controller.authorized?
287
+ end
288
+
289
+ def test_additional_members_filter_other_new
290
+ reader = Authorization::Reader::DSLReader.new
291
+ reader.parse %{
292
+ authorization do
293
+ role :allowed_role do
294
+ has_permission_on :basic_resources, :to => :new do
295
+ if_attribute :id => is {"1"}
296
+ end
297
+ end
298
+ end
299
+ }
300
+
301
+ allowed_user = MockUser.new(:allowed_role)
302
+ request!(allowed_user, :other_new, reader, :basic_resource => {:id => "2"})
303
+ assert !@controller.authorized?
304
+ request!(allowed_user, :new, reader, :basic_resource => {:id => "2"},
305
+ :clear => [:@basic_resource])
306
+ assert !@controller.authorized?
307
+
308
+ request!(allowed_user, :other_new, reader, :basic_resource => {:id => "1"},
309
+ :clear => [:@basic_resource])
310
+ assert @controller.authorized?
311
+ request!(allowed_user, :new, reader, :basic_resource => {:id => "1"},
312
+ :clear => [:@basic_resource])
313
+ assert @controller.authorized?
314
+ end
315
+ end
316
+
317
+
318
+ class CustomMethodsResourceController < MocksController
319
+ # not implemented yet
320
+ end
321
+
322
+
323
+ class ExplicitContextResourceController < MocksController
324
+ filter_resource_access :context => :basic_resources
325
+ define_resource_actions
326
+ end
327
+ class ExplicitContextResourceControllerTest < ActionController::TestCase
328
+ def test_explicit_context_filter_index
329
+ reader = Authorization::Reader::DSLReader.new
330
+ reader.parse %{
331
+ authorization do
332
+ role :allowed_role do
333
+ has_permission_on :basic_resources, :to => :index do
334
+ if_attribute :id => is {"1"}
335
+ end
336
+ end
337
+ end
338
+ }
339
+
340
+ allowed_user = MockUser.new(:allowed_role)
341
+ request!(MockUser.new(:another_role), :index, reader)
342
+ assert !@controller.authorized?
343
+ request!(allowed_user, :index, reader)
344
+ assert @controller.authorized?
345
+ end
346
+
347
+ def test_explicit_context_filter_show_with_id
348
+ reader = Authorization::Reader::DSLReader.new
349
+ reader.parse %{
350
+ authorization do
351
+ role :allowed_role do
352
+ has_permission_on :basic_resources, :to => :show do
353
+ if_attribute :id => is {"1"}
354
+ end
355
+ end
356
+ end
357
+ }
358
+
359
+ allowed_user = MockUser.new(:allowed_role)
360
+ request!(allowed_user, :show, reader, :id => "2")
361
+ assert !@controller.authorized?
362
+ request!(allowed_user, :show, reader, :id => "1", :clear => [:@basic_resource])
363
+ assert @controller.authorized?
364
+ end
365
+
366
+ def test_explicit_context_filter_new_with_params
367
+ reader = Authorization::Reader::DSLReader.new
368
+ reader.parse %{
369
+ authorization do
370
+ role :allowed_role do
371
+ has_permission_on :basic_resources, :to => :new do
372
+ if_attribute :id => is {"1"}
373
+ end
374
+ end
375
+ end
376
+ }
377
+
378
+ allowed_user = MockUser.new(:allowed_role)
379
+ request!(allowed_user, :new, reader, :basic_resource => {:id => "2"})
380
+ assert !@controller.authorized?
381
+ request!(allowed_user, :new, reader, :basic_resource => {:id => "1"},
382
+ :clear => [:@basic_resource])
383
+ assert @controller.authorized?
384
+ end
385
+ end
@@ -0,0 +1,369 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+
4
+ class LoadMockObject < MockDataObject
5
+ end
6
+
7
+ ##################
8
+ class SpecificMocksController < MocksController
9
+ filter_access_to :test_action, :require => :test, :context => :permissions
10
+ filter_access_to :test_action_2, :require => :test, :context => :permissions_2
11
+ filter_access_to :show
12
+ filter_access_to :edit, :create, :require => :test, :context => :permissions
13
+ filter_access_to :edit_2, :require => :test, :context => :permissions,
14
+ :attribute_check => true, :model => LoadMockObject
15
+ filter_access_to :new, :require => :test, :context => :permissions
16
+
17
+ filter_access_to [:action_group_action_1, :action_group_action_2]
18
+ define_action_methods :test_action, :test_action_2, :show, :edit, :create,
19
+ :edit_2, :new, :unprotected_action, :action_group_action_1, :action_group_action_2
20
+ end
21
+
22
+ class BasicControllerTest < ActionController::TestCase
23
+ tests SpecificMocksController
24
+
25
+ def test_filter_access_to_receiving_an_explicit_array
26
+ reader = Authorization::Reader::DSLReader.new
27
+
28
+ reader.parse %{
29
+ authorization do
30
+ role :test_action_group_2 do
31
+ has_permission_on :specific_mocks, :to => :action_group_action_2
32
+ end
33
+ end
34
+ }
35
+
36
+ request!(MockUser.new(:test_action_group_2), "action_group_action_2", reader)
37
+ assert @controller.authorized?
38
+ request!(MockUser.new(:test_action_group_2), "action_group_action_1", reader)
39
+ assert !@controller.authorized?
40
+ request!(nil, "action_group_action_2", reader)
41
+ assert !@controller.authorized?
42
+ end
43
+
44
+ def test_filter_access
45
+ assert !@controller.class.before_filters.empty?
46
+
47
+ reader = Authorization::Reader::DSLReader.new
48
+ reader.parse %{
49
+ authorization do
50
+ role :test_role do
51
+ has_permission_on :permissions, :to => :test
52
+ has_permission_on :specific_mocks, :to => :show
53
+ end
54
+ end
55
+ }
56
+
57
+ request!(MockUser.new(:test_role), "test_action", reader)
58
+ assert @controller.authorized?
59
+
60
+ request!(MockUser.new(:test_role), "test_action_2", reader)
61
+ assert !@controller.authorized?
62
+
63
+ request!(MockUser.new(:test_role_2), "test_action", reader)
64
+ assert_response :forbidden
65
+ assert !@controller.authorized?
66
+
67
+ request!(MockUser.new(:test_role), "show", reader)
68
+ assert @controller.authorized?
69
+ end
70
+
71
+ def test_filter_access_multi_actions
72
+ reader = Authorization::Reader::DSLReader.new
73
+ reader.parse %{
74
+ authorization do
75
+ role :test_role do
76
+ has_permission_on :permissions, :to => :test
77
+ end
78
+ end
79
+ }
80
+ request!(MockUser.new(:test_role), "create", reader)
81
+ assert @controller.authorized?
82
+ end
83
+
84
+ def test_filter_access_unprotected_actions
85
+ reader = Authorization::Reader::DSLReader.new
86
+ reader.parse %{
87
+ authorization do
88
+ role :test_role do
89
+ end
90
+ end
91
+ }
92
+ request!(MockUser.new(:test_role), "unprotected_action", reader)
93
+ assert @controller.authorized?
94
+ end
95
+
96
+ def test_filter_access_priv_hierarchy
97
+ reader = Authorization::Reader::DSLReader.new
98
+ reader.parse %{
99
+ privileges do
100
+ privilege :read do
101
+ includes :list, :show
102
+ end
103
+ end
104
+ authorization do
105
+ role :test_role do
106
+ has_permission_on :specific_mocks, :to => :read
107
+ end
108
+ end
109
+ }
110
+ request!(MockUser.new(:test_role), "show", reader)
111
+ assert @controller.authorized?
112
+ end
113
+
114
+ def test_filter_access_skip_attribute_test
115
+ reader = Authorization::Reader::DSLReader.new
116
+ reader.parse %{
117
+ authorization do
118
+ role :test_role do
119
+ has_permission_on :permissions, :to => :test do
120
+ if_attribute :id => is { user }
121
+ end
122
+ end
123
+ end
124
+ }
125
+ request!(MockUser.new(:test_role), "new", reader)
126
+ assert @controller.authorized?
127
+
128
+ request!(MockUser.new(:test_role), "edit_2", reader)
129
+ assert !@controller.authorized?
130
+ end
131
+
132
+ def test_existing_instance_var_remains_unchanged
133
+ reader = Authorization::Reader::DSLReader.new
134
+ reader.parse %{
135
+ authorization do
136
+ role :test_role do
137
+ has_permission_on :permissions, :to => :test do
138
+ if_attribute :id => is { 5 }
139
+ end
140
+ end
141
+ end
142
+ }
143
+ mock_object = MockDataObject.new(:id => 5)
144
+ @controller.send(:instance_variable_set, :"@load_mock_object",
145
+ mock_object)
146
+ request!(MockUser.new(:test_role), "edit_2", reader)
147
+ assert_equal mock_object,
148
+ @controller.send(:instance_variable_get, :"@load_mock_object")
149
+ assert @controller.authorized?
150
+ end
151
+
152
+ def test_permitted_to_without_context
153
+ reader = Authorization::Reader::DSLReader.new
154
+ reader.parse %{
155
+ authorization do
156
+ role :test_role do
157
+ has_permission_on :specific_mocks, :to => :test
158
+ end
159
+ end
160
+ }
161
+ @controller.current_user = MockUser.new(:test_role)
162
+ @controller.authorization_engine = Authorization::Engine.new(reader)
163
+ assert @controller.permitted_to?(:test)
164
+ end
165
+ end
166
+
167
+
168
+ ##################
169
+ class AllMocksController < MocksController
170
+ filter_access_to :all
171
+ filter_access_to :view, :require => :test, :context => :permissions
172
+ define_action_methods :show, :view
173
+ end
174
+ class AllActionsControllerTest < ActionController::TestCase
175
+ tests AllMocksController
176
+ def test_filter_access_all
177
+ reader = Authorization::Reader::DSLReader.new
178
+ reader.parse %{
179
+ authorization do
180
+ role :test_role do
181
+ has_permission_on :permissions, :to => :test
182
+ has_permission_on :all_mocks, :to => :show
183
+ end
184
+ end
185
+ }
186
+
187
+ request!(MockUser.new(:test_role), "show", reader)
188
+ assert @controller.authorized?
189
+
190
+ request!(MockUser.new(:test_role), "view", reader)
191
+ assert @controller.authorized?
192
+
193
+ request!(MockUser.new(:test_role_2), "show", reader)
194
+ assert !@controller.authorized?
195
+ end
196
+ end
197
+
198
+
199
+ ##################
200
+ class LoadMockObjectsController < MocksController
201
+ filter_access_to :show, :attribute_check => true, :model => LoadMockObject
202
+ filter_access_to :edit, :attribute_check => true
203
+ filter_access_to :update, :delete, :attribute_check => true,
204
+ :load_method => lambda {MockDataObject.new(:test => 1)}
205
+ filter_access_to :create do
206
+ permitted_to! :edit, :load_mock_objects
207
+ end
208
+ filter_access_to :view, :attribute_check => true, :load_method => :load_method
209
+ def load_method
210
+ MockDataObject.new(:test => 2)
211
+ end
212
+ define_action_methods :show, :edit, :update, :delete, :create, :view
213
+ end
214
+ class LoadObjectControllerTest < ActionController::TestCase
215
+ tests LoadMockObjectsController
216
+
217
+ def test_filter_access_with_object_load
218
+ reader = Authorization::Reader::DSLReader.new
219
+ reader.parse %{
220
+ authorization do
221
+ role :test_role do
222
+ has_permission_on :load_mock_objects, :to => [:show, :edit] do
223
+ if_attribute :id => is {"1"}
224
+ end
225
+ end
226
+ end
227
+ }
228
+
229
+ request!(MockUser.new(:test_role), "show", reader, :id => 2)
230
+ assert !@controller.authorized?
231
+
232
+ request!(MockUser.new(:test_role), "show", reader, :id => 1,
233
+ :clear => [:@load_mock_object])
234
+ assert @controller.authorized?
235
+
236
+ request!(MockUser.new(:test_role), "edit", reader, :id => 1,
237
+ :clear => [:@load_mock_object])
238
+ assert @controller.authorized?
239
+ assert @controller.instance_variable_defined?(:@load_mock_object)
240
+ end
241
+
242
+ def test_filter_access_with_object_load_custom
243
+ reader = Authorization::Reader::DSLReader.new
244
+ reader.parse %{
245
+ authorization do
246
+ role :test_role do
247
+ has_permission_on :load_mock_objects, :to => :view do
248
+ if_attribute :test => is {2}
249
+ end
250
+ has_permission_on :load_mock_objects, :to => :update do
251
+ if_attribute :test => is {1}
252
+ end
253
+ has_permission_on :load_mock_objects, :to => :delete do
254
+ if_attribute :test => is {2}
255
+ end
256
+ end
257
+ end
258
+ }
259
+
260
+ request!(MockUser.new(:test_role), "delete", reader)
261
+ assert !@controller.authorized?
262
+
263
+ request!(MockUser.new(:test_role), "view", reader)
264
+ assert @controller.authorized?
265
+
266
+ request!(MockUser.new(:test_role), "update", reader)
267
+ assert @controller.authorized?
268
+ end
269
+
270
+ def test_filter_access_custom
271
+ reader = Authorization::Reader::DSLReader.new
272
+ reader.parse %{
273
+ authorization do
274
+ role :test_role do
275
+ has_permission_on :load_mock_objects, :to => :edit
276
+ end
277
+ role :test_role_2 do
278
+ has_permission_on :load_mock_objects, :to => :create
279
+ end
280
+ end
281
+ }
282
+
283
+ request!(MockUser.new(:test_role), "create", reader)
284
+ assert @controller.authorized?
285
+
286
+ request!(MockUser.new(:test_role_2), "create", reader)
287
+ assert !@controller.authorized?
288
+ end
289
+ end
290
+
291
+
292
+ ##################
293
+ class AccessOverwritesController < MocksController
294
+ filter_access_to :test_action, :test_action_2,
295
+ :require => :test, :context => :permissions_2
296
+ filter_access_to :test_action, :require => :test, :context => :permissions
297
+ define_action_methods :test_action, :test_action_2
298
+ end
299
+ class AccessOverwritesControllerTest < ActionController::TestCase
300
+ def test_filter_access_overwrite
301
+ reader = Authorization::Reader::DSLReader.new
302
+ reader.parse %{
303
+ authorization do
304
+ role :test_role do
305
+ has_permission_on :permissions, :to => :test
306
+ end
307
+ end
308
+ }
309
+ request!(MockUser.new(:test_role), "test_action_2", reader)
310
+ assert !@controller.authorized?
311
+
312
+ request!(MockUser.new(:test_role), "test_action", reader)
313
+ assert @controller.authorized?
314
+ end
315
+ end
316
+
317
+
318
+ ##################
319
+ class PeopleController < MocksController
320
+ filter_access_to :all
321
+ define_action_methods :show
322
+ end
323
+ class PluralizationControllerTest < ActionController::TestCase
324
+ tests PeopleController
325
+
326
+ def test_filter_access_people_controller
327
+ reader = Authorization::Reader::DSLReader.new
328
+ reader.parse %{
329
+ authorization do
330
+ role :test_role do
331
+ has_permission_on :people, :to => :show
332
+ end
333
+ end
334
+ }
335
+ request!(MockUser.new(:test_role), "show", reader)
336
+ assert @controller.authorized?
337
+ end
338
+ end
339
+
340
+
341
+ ##################
342
+ class CommonController < MocksController
343
+ filter_access_to :delete, :context => :common
344
+ filter_access_to :all
345
+ end
346
+ class CommonChild1Controller < CommonController
347
+ filter_access_to :all, :context => :context_1
348
+ end
349
+ class CommonChild2Controller < CommonController
350
+ filter_access_to :delete
351
+ define_action_methods :show
352
+ end
353
+ class HierachicalControllerTest < ActionController::TestCase
354
+ tests CommonChild2Controller
355
+ def test_controller_hierarchy
356
+ reader = Authorization::Reader::DSLReader.new
357
+ reader.parse %{
358
+ authorization do
359
+ role :test_role do
360
+ has_permission_on :mocks, :to => [:delete, :show]
361
+ end
362
+ end
363
+ }
364
+ request!(MockUser.new(:test_role), "show", reader)
365
+ assert !@controller.authorized?
366
+ request!(MockUser.new(:test_role), "delete", reader)
367
+ assert !@controller.authorized?
368
+ end
369
+ end