declarative_authorization 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/README.rdoc +24 -4
- data/config/routes.rb +19 -9
- data/lib/declarative_authorization/authorization.rb +15 -6
- data/lib/declarative_authorization/in_controller.rb +2 -0
- data/test/authorization_test.rb +20 -0
- data/test/controller_test.rb +15 -0
- data/test/model_test.rb +46 -0
- data/test/test_helper.rb +1 -1
- metadata +4 -4
data/CHANGELOG
CHANGED
data/README.rdoc
CHANGED
@@ -332,6 +332,12 @@ In your test_helper.rb, to enable the helpers add
|
|
332
332
|
...
|
333
333
|
end
|
334
334
|
|
335
|
+
For using the test helpers with RSpec, just add the following lines to your
|
336
|
+
spec_helper.rb (somewhere after require 'spec/rails'):
|
337
|
+
|
338
|
+
require 'declarative_authorization/maintenance'
|
339
|
+
include Authorization::TestHelper
|
340
|
+
|
335
341
|
Now, in unit tests, you may deactivate authorization if needed e.g. for test
|
336
342
|
setup and assume certain identities for tests:
|
337
343
|
|
@@ -347,6 +353,19 @@ setup and assume certain identities for tests:
|
|
347
353
|
end
|
348
354
|
end
|
349
355
|
end
|
356
|
+
|
357
|
+
Or, with RSpec, it would work like this:
|
358
|
+
|
359
|
+
describe Employee do
|
360
|
+
it "should read" do
|
361
|
+
without_access_control do
|
362
|
+
Employee.create(...)
|
363
|
+
end
|
364
|
+
with_user(admin) do
|
365
|
+
Employee.find(:first)
|
366
|
+
end
|
367
|
+
end
|
368
|
+
end
|
350
369
|
|
351
370
|
In functional tests, get, posts, etc. may be tested in the name of certain users:
|
352
371
|
|
@@ -491,10 +510,11 @@ sbartsch at tzi.org
|
|
491
510
|
|
492
511
|
= Contributors
|
493
512
|
|
494
|
-
Thanks to John Joseph Bachir, Eike Carls, Kai Chen, Erik Dahlstrand,
|
495
|
-
Alexander Dobriakov, Sebastian Dyck, Ari Epstein, Jeremy Friesen,
|
496
|
-
|
497
|
-
|
513
|
+
Thanks to John Joseph Bachir, Eike Carls, Dennis Blöte, Kai Chen, Erik Dahlstrand,
|
514
|
+
Jeroen van Dijk, Alexander Dobriakov, Sebastian Dyck, Ari Epstein, Jeremy Friesen,
|
515
|
+
Tim Harper, hollownest, Daniel Kristensen, Brad Langhorst, Brian Langenfeld,
|
516
|
+
Georg Ledermann, Geoff Longman, Olly Lylo, Mark Mansour, Thomas Maurer, Sharagoz,
|
517
|
+
TJ Singleton, Mike Vincent
|
498
518
|
|
499
519
|
|
500
520
|
= Licence
|
data/config/routes.rb
CHANGED
@@ -1,10 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
if Authorization::activate_authorization_rules_browser?
|
2
|
+
if Rails.respond_to?(:application)
|
3
|
+
Rails.application.routes.draw do
|
4
|
+
resources :authorization_rules, :only => [:index] do
|
5
|
+
collection do
|
6
|
+
get :graph
|
7
|
+
get :change
|
8
|
+
get :suggest_change
|
9
|
+
end
|
10
|
+
end
|
11
|
+
resources :authorization_usages, :only => :index
|
12
|
+
end
|
13
|
+
else
|
14
|
+
ActionController::Routing::Routes.draw do |map|
|
15
|
+
map.resources :authorization_rules, :only => [:index],
|
16
|
+
:collection => {:graph => :get, :change => :get, :suggest_change => :get}
|
17
|
+
map.resources :authorization_usages, :only => :index
|
18
|
+
end
|
9
19
|
end
|
10
|
-
end
|
20
|
+
end
|
@@ -296,7 +296,7 @@ module Authorization
|
|
296
296
|
|
297
297
|
def flatten_roles (roles)
|
298
298
|
# TODO caching?
|
299
|
-
flattened_roles = roles.
|
299
|
+
flattened_roles = roles.dup.to_a
|
300
300
|
flattened_roles.each do |role|
|
301
301
|
flattened_roles.concat(@role_hierarchy[role]).uniq! if @role_hierarchy[role]
|
302
302
|
end
|
@@ -373,18 +373,27 @@ module Authorization
|
|
373
373
|
exceptions << e
|
374
374
|
nil
|
375
375
|
end
|
376
|
-
end
|
376
|
+
end
|
377
377
|
|
378
378
|
if exceptions.length > 0 and (@join_operator == :and or exceptions.length == @attributes.length)
|
379
379
|
raise NotAuthorized, "Missing authorization in collecting obligations: #{exceptions.map(&:to_s) * ", "}"
|
380
380
|
end
|
381
381
|
|
382
382
|
if @join_operator == :and and !obligations.empty?
|
383
|
-
|
384
|
-
obligations[
|
385
|
-
|
383
|
+
# cross product of OR'ed obligations in arrays
|
384
|
+
arrayed_obligations = obligations.map {|obligation| obligation.is_a?(Hash) ? [obligation] : obligation}
|
385
|
+
merged_obligations = arrayed_obligations.first
|
386
|
+
arrayed_obligations[1..-1].each do |inner_obligations|
|
387
|
+
previous_merged_obligations = merged_obligations
|
388
|
+
merged_obligations = inner_obligations.collect do |inner_obligation|
|
389
|
+
previous_merged_obligations.collect do |merged_obligation|
|
390
|
+
merged_obligation.deep_merge(inner_obligation)
|
391
|
+
end
|
392
|
+
end.flatten
|
386
393
|
end
|
387
|
-
obligations =
|
394
|
+
obligations = merged_obligations
|
395
|
+
else
|
396
|
+
obligations = obligations.flatten.compact
|
388
397
|
end
|
389
398
|
obligations.empty? ? [{}] : obligations
|
390
399
|
end
|
@@ -274,6 +274,8 @@ module Authorization
|
|
274
274
|
context = options[:context]
|
275
275
|
actions = args.flatten
|
276
276
|
|
277
|
+
# prevent setting filter_access_filter multiple times
|
278
|
+
skip_before_filter :filter_access_filter
|
277
279
|
before_filter :filter_access_filter
|
278
280
|
|
279
281
|
filter_access_permissions.each do |perm|
|
data/test/authorization_test.rb
CHANGED
@@ -69,6 +69,26 @@ class AuthorizationTest < Test::Unit::TestCase
|
|
69
69
|
assert engine.permit?(:test, :context => :permissions_4, :user => MockUser.new(:test_role))
|
70
70
|
assert engine.permit?(:test, :context => :permissions_5, :user => MockUser.new(:test_role))
|
71
71
|
end
|
72
|
+
|
73
|
+
def test_permit_with_frozen_roles
|
74
|
+
reader = Authorization::Reader::DSLReader.new
|
75
|
+
reader.parse %{
|
76
|
+
authorization do
|
77
|
+
role :other_role do
|
78
|
+
includes :test_role
|
79
|
+
end
|
80
|
+
role :test_role do
|
81
|
+
has_permission_on :permissions, :to => :test
|
82
|
+
end
|
83
|
+
end
|
84
|
+
}
|
85
|
+
engine = Authorization::Engine.new(reader)
|
86
|
+
roles = [:other_role].freeze
|
87
|
+
assert_nothing_raised do
|
88
|
+
assert engine.permit?(:test, :context => :permissions,
|
89
|
+
:user => MockUser.new(:role_symbols => roles))
|
90
|
+
end
|
91
|
+
end
|
72
92
|
|
73
93
|
def test_obligations_without_conditions
|
74
94
|
reader = Authorization::Reader::DSLReader.new
|
data/test/controller_test.rb
CHANGED
@@ -198,6 +198,7 @@ end
|
|
198
198
|
|
199
199
|
##################
|
200
200
|
class LoadMockObjectsController < MocksController
|
201
|
+
before_filter { @@load_method_call_count = 0 }
|
201
202
|
filter_access_to :show, :attribute_check => true, :model => LoadMockObject
|
202
203
|
filter_access_to :edit, :attribute_check => true
|
203
204
|
filter_access_to :update, :delete, :attribute_check => true,
|
@@ -207,9 +208,18 @@ class LoadMockObjectsController < MocksController
|
|
207
208
|
end
|
208
209
|
filter_access_to :view, :attribute_check => true, :load_method => :load_method
|
209
210
|
def load_method
|
211
|
+
self.class.load_method_called
|
210
212
|
MockDataObject.new(:test => 2)
|
211
213
|
end
|
212
214
|
define_action_methods :show, :edit, :update, :delete, :create, :view
|
215
|
+
|
216
|
+
def self.load_method_called
|
217
|
+
@@load_method_call_count ||= 0
|
218
|
+
@@load_method_call_count += 1
|
219
|
+
end
|
220
|
+
def self.load_method_call_count
|
221
|
+
@@load_method_call_count || 0
|
222
|
+
end
|
213
223
|
end
|
214
224
|
class LoadObjectControllerTest < ActionController::TestCase
|
215
225
|
tests LoadMockObjectsController
|
@@ -287,7 +297,12 @@ class LoadObjectControllerTest < ActionController::TestCase
|
|
287
297
|
|
288
298
|
request!(MockUser.new(:test_role), "view", reader)
|
289
299
|
assert @controller.authorized?
|
300
|
+
assert_equal 1, @controller.class.load_method_call_count
|
290
301
|
|
302
|
+
request!(MockUser.new(:test_role_2), "view", reader)
|
303
|
+
assert !@controller.authorized?
|
304
|
+
assert_equal 1, @controller.class.load_method_call_count
|
305
|
+
|
291
306
|
request!(MockUser.new(:test_role), "update", reader)
|
292
307
|
assert @controller.authorized?
|
293
308
|
end
|
data/test/model_test.rb
CHANGED
@@ -1099,6 +1099,52 @@ class NamedScopeModelTest < Test::Unit::TestCase
|
|
1099
1099
|
TestAttr.delete_all
|
1100
1100
|
end
|
1101
1101
|
|
1102
|
+
def test_with_anded_if_permitted_to
|
1103
|
+
reader = Authorization::Reader::DSLReader.new
|
1104
|
+
reader.parse %{
|
1105
|
+
authorization do
|
1106
|
+
role :base_role do
|
1107
|
+
has_permission_on :test_attrs, :to => :read, :join_by => :and do
|
1108
|
+
if_permitted_to :read, :test_model
|
1109
|
+
if_attribute :attr => 1
|
1110
|
+
end
|
1111
|
+
end
|
1112
|
+
role :first_role do
|
1113
|
+
includes :base_role
|
1114
|
+
has_permission_on :test_models, :to => :read do
|
1115
|
+
if_attribute :content => "first test"
|
1116
|
+
end
|
1117
|
+
end
|
1118
|
+
role :second_role do
|
1119
|
+
includes :base_role
|
1120
|
+
has_permission_on :test_models, :to => :read do
|
1121
|
+
if_attribute :country_id => 2
|
1122
|
+
end
|
1123
|
+
end
|
1124
|
+
end
|
1125
|
+
}
|
1126
|
+
Authorization::Engine.instance(reader)
|
1127
|
+
|
1128
|
+
test_model_1 = TestModel.create!(:content => "first test")
|
1129
|
+
test_model_1.test_attrs.create!(:attr => 1)
|
1130
|
+
test_model_for_second_role = TestModel.create!(:country_id => 2)
|
1131
|
+
test_model_for_second_role.test_attrs.create!(:attr => 1)
|
1132
|
+
test_model_for_second_role.test_attrs.create!(:attr => 2)
|
1133
|
+
|
1134
|
+
user = MockUser.new(:first_role)
|
1135
|
+
assert Authorization::Engine.instance.permit?(:read, :object => test_model_1.test_attrs.first, :user => user)
|
1136
|
+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
|
1137
|
+
|
1138
|
+
user_with_both_roles = MockUser.new(:first_role, :second_role)
|
1139
|
+
assert Authorization::Engine.instance.permit?(:read, :object => test_model_1.test_attrs.first, :user => user_with_both_roles)
|
1140
|
+
assert Authorization::Engine.instance.permit?(:read, :object => test_model_for_second_role.test_attrs.first, :user => user_with_both_roles)
|
1141
|
+
#p Authorization::Engine.instance.obligations(:read, :user => user_with_both_roles, :context => :test_attrs)
|
1142
|
+
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user_with_both_roles).length
|
1143
|
+
|
1144
|
+
TestModel.delete_all
|
1145
|
+
TestAttr.delete_all
|
1146
|
+
end
|
1147
|
+
|
1102
1148
|
def test_with_if_permitted_to_with_no_child_permissions
|
1103
1149
|
reader = Authorization::Reader::DSLReader.new
|
1104
1150
|
reader.parse %{
|
data/test/test_helper.rb
CHANGED
@@ -70,7 +70,7 @@ end
|
|
70
70
|
class MockUser < MockDataObject
|
71
71
|
def initialize (*roles)
|
72
72
|
options = roles.last.is_a?(::Hash) ? roles.pop : {}
|
73
|
-
super(
|
73
|
+
super({:role_symbols => roles, :login => hash}.merge(options))
|
74
74
|
end
|
75
75
|
|
76
76
|
def initialize_copy (other)
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 5
|
8
|
-
-
|
9
|
-
version: 0.5.
|
8
|
+
- 2
|
9
|
+
version: 0.5.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Steffen Bartsch
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-12-31 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -102,6 +102,6 @@ rubyforge_project:
|
|
102
102
|
rubygems_version: 1.3.6
|
103
103
|
signing_key:
|
104
104
|
specification_version: 3
|
105
|
-
summary: declarative_authorization is a Rails plugin for authorization based on readable authorization rules.
|
105
|
+
summary: declarative_authorization is a Rails plugin for maintainable authorization based on readable authorization rules.
|
106
106
|
test_files: []
|
107
107
|
|