declarative_authorization 0.3.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/CHANGELOG +83 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +510 -0
  4. data/Rakefile +43 -0
  5. data/app/controllers/authorization_rules_controller.rb +259 -0
  6. data/app/controllers/authorization_usages_controller.rb +23 -0
  7. data/app/helpers/authorization_rules_helper.rb +187 -0
  8. data/app/views/authorization_rules/_change.erb +58 -0
  9. data/app/views/authorization_rules/_show_graph.erb +37 -0
  10. data/app/views/authorization_rules/_suggestions.erb +48 -0
  11. data/app/views/authorization_rules/change.html.erb +152 -0
  12. data/app/views/authorization_rules/graph.dot.erb +68 -0
  13. data/app/views/authorization_rules/graph.html.erb +40 -0
  14. data/app/views/authorization_rules/index.html.erb +17 -0
  15. data/app/views/authorization_usages/index.html.erb +36 -0
  16. data/authorization_rules.dist.rb +20 -0
  17. data/config/routes.rb +7 -0
  18. data/garlic_example.rb +20 -0
  19. data/init.rb +5 -0
  20. data/lib/declarative_authorization.rb +15 -0
  21. data/lib/declarative_authorization/authorization.rb +634 -0
  22. data/lib/declarative_authorization/development_support/analyzer.rb +252 -0
  23. data/lib/declarative_authorization/development_support/change_analyzer.rb +253 -0
  24. data/lib/declarative_authorization/development_support/change_supporter.rb +620 -0
  25. data/lib/declarative_authorization/development_support/development_support.rb +243 -0
  26. data/lib/declarative_authorization/helper.rb +60 -0
  27. data/lib/declarative_authorization/in_controller.rb +597 -0
  28. data/lib/declarative_authorization/in_model.rb +159 -0
  29. data/lib/declarative_authorization/maintenance.rb +182 -0
  30. data/lib/declarative_authorization/obligation_scope.rb +308 -0
  31. data/lib/declarative_authorization/rails_legacy.rb +14 -0
  32. data/lib/declarative_authorization/reader.rb +441 -0
  33. data/test/authorization_test.rb +827 -0
  34. data/test/controller_filter_resource_access_test.rb +394 -0
  35. data/test/controller_test.rb +386 -0
  36. data/test/dsl_reader_test.rb +157 -0
  37. data/test/helper_test.rb +171 -0
  38. data/test/maintenance_test.rb +46 -0
  39. data/test/model_test.rb +1308 -0
  40. data/test/schema.sql +54 -0
  41. data/test/test_helper.rb +118 -0
  42. metadata +105 -0
@@ -0,0 +1,157 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class DSLReaderTest < Test::Unit::TestCase
4
+ def test_privileges
5
+ reader = Authorization::Reader::DSLReader.new
6
+ reader.parse %{
7
+ privileges do
8
+ privilege :test_priv do
9
+ includes :lower_priv
10
+ end
11
+ end
12
+ }
13
+ assert_equal 2, reader.privileges_reader.privileges.length
14
+ assert_equal [[:lower_priv, nil]],
15
+ reader.privileges_reader.privilege_hierarchy[:test_priv]
16
+ end
17
+
18
+ def test_privileges_with_context
19
+ reader = Authorization::Reader::DSLReader.new
20
+ reader.parse %{
21
+ privileges do
22
+ privilege :test_priv, :test_context do
23
+ includes :lower_priv
24
+ end
25
+ end
26
+ }
27
+ assert_equal [[:lower_priv, :test_context]],
28
+ reader.privileges_reader.privilege_hierarchy[:test_priv]
29
+ end
30
+
31
+ def test_privileges_one_line
32
+ reader = Authorization::Reader::DSLReader.new
33
+ reader.parse %{
34
+ privileges do
35
+ privilege :test_priv, :test_context, :includes => :lower_priv
36
+ privilege :test_priv_2, :test_context, :includes => [:lower_priv]
37
+ privilege :test_priv_3, :includes => [:lower_priv]
38
+ end
39
+ }
40
+ assert_equal [[:lower_priv, :test_context]],
41
+ reader.privileges_reader.privilege_hierarchy[:test_priv]
42
+ assert_equal [[:lower_priv, :test_context]],
43
+ reader.privileges_reader.privilege_hierarchy[:test_priv_2]
44
+ assert_equal [[:lower_priv, nil]],
45
+ reader.privileges_reader.privilege_hierarchy[:test_priv_3]
46
+ end
47
+
48
+ def test_auth_role
49
+ reader = Authorization::Reader::DSLReader.new
50
+ reader.parse %{
51
+ authorization do
52
+ role :test_role do
53
+ includes :lesser_role
54
+ has_permission_on :items, :to => :read
55
+ end
56
+ end
57
+ }
58
+ assert_equal 1, reader.auth_rules_reader.roles.length
59
+ assert_equal [:lesser_role], reader.auth_rules_reader.role_hierarchy[:test_role]
60
+ assert_equal 1, reader.auth_rules_reader.auth_rules.length
61
+ end
62
+
63
+ def test_auth_role_permit_on
64
+ reader = Authorization::Reader::DSLReader.new
65
+ reader.parse %|
66
+ authorization do
67
+ role :test_role do
68
+ has_permission_on :test_context do
69
+ to :test_perm, :manage
70
+ if_attribute :test_attr => is { user.test_attr }
71
+ end
72
+ end
73
+ end
74
+ |
75
+ assert_equal 1, reader.auth_rules_reader.roles.length
76
+ assert_equal 1, reader.auth_rules_reader.auth_rules.length
77
+ assert reader.auth_rules_reader.auth_rules[0].matches?(:test_role, [:test_perm], :test_context)
78
+ assert reader.auth_rules_reader.auth_rules[0].matches?(:test_role, [:manage], :test_context)
79
+ end
80
+
81
+ def test_permit_block
82
+ reader = Authorization::Reader::DSLReader.new
83
+ reader.parse %|
84
+ authorization do
85
+ role :test_role do
86
+ has_permission_on :perms, :to => :test do
87
+ if_attribute :test_attr => is { user.test_attr }
88
+ if_attribute :test_attr_2 => is_not { user.test_attr }
89
+ if_attribute :test_attr_3 => contains { user.test_attr }
90
+ if_attribute :test_attr_4 => does_not_contain { user.test_attr }
91
+ if_attribute :test_attr_5 => is_in { user.test_attr }
92
+ if_attribute :test_attr_5 => is_not_in { user.test_attr }
93
+ end
94
+ end
95
+ end
96
+ |
97
+ assert_equal 1, reader.auth_rules_reader.roles.length
98
+ assert_equal 1, reader.auth_rules_reader.auth_rules.length
99
+ assert reader.auth_rules_reader.auth_rules[0].matches?(:test_role, [:test], :perms)
100
+ end
101
+
102
+ def test_has_permission_to_with_context
103
+ reader = Authorization::Reader::DSLReader.new
104
+ reader.parse %|
105
+ authorization do
106
+ role :test_role do
107
+ has_permission_on :perms, :to => :test
108
+ end
109
+ end
110
+ |
111
+ assert_equal 1, reader.auth_rules_reader.roles.length
112
+ assert_equal 1, reader.auth_rules_reader.auth_rules.length
113
+ assert reader.auth_rules_reader.auth_rules[0].matches?(:test_role, [:test], :perms)
114
+ end
115
+
116
+ def test_context
117
+ reader = Authorization::Reader::DSLReader.new
118
+ reader.parse %{
119
+ contexts do
120
+ context :high_level_context do
121
+ includes :low_level_context_1, :low_level_context_2
122
+ end
123
+ end
124
+ }
125
+ end
126
+
127
+ def test_dsl_error
128
+ reader = Authorization::Reader::DSLReader.new
129
+ assert_raise(Authorization::Reader::DSLError) do
130
+ reader.parse %{
131
+ authorization do
132
+ includes :lesser_role
133
+ end
134
+ }
135
+ end
136
+ end
137
+
138
+ def test_syntax_error
139
+ reader = Authorization::Reader::DSLReader.new
140
+ assert_raise(Authorization::Reader::DSLSyntaxError) do
141
+ reader.parse %{
142
+ authorizations do
143
+ end
144
+ }
145
+ end
146
+ end
147
+
148
+ def test_syntax_error_2
149
+ reader = Authorization::Reader::DSLReader.new
150
+ assert_raise(Authorization::Reader::DSLSyntaxError) do
151
+ reader.parse %{
152
+ authorizations
153
+ end
154
+ }
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,171 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+ require File.join(File.dirname(__FILE__), %w{.. lib declarative_authorization helper})
3
+
4
+
5
+ class HelperMocksController < MocksController
6
+ filter_access_to :action, :require => :show, :context => :mocks
7
+ define_action_methods :action
8
+ end
9
+ class HelperTest < ActionController::TestCase
10
+ tests HelperMocksController
11
+ include Authorization::AuthorizationHelper
12
+ attr_reader :controller
13
+
14
+ def test_permit
15
+ reader = Authorization::Reader::DSLReader.new
16
+ reader.parse %{
17
+ authorization do
18
+ role :test_role do
19
+ has_permission_on :mocks, :to => :show
20
+ end
21
+ role :test_role_2 do
22
+ has_permission_on :mocks, :to => :update
23
+ end
24
+ end
25
+ }
26
+ user = MockUser.new(:test_role)
27
+ request!(user, :action, reader)
28
+
29
+ assert permitted_to?(:show, :mocks)
30
+ assert !permitted_to?(:update, :mocks)
31
+
32
+ block_evaled = false
33
+ permitted_to?(:show, :mocks) do
34
+ block_evaled = true
35
+ end
36
+ assert block_evaled
37
+
38
+ block_evaled = false
39
+ permitted_to?(:update, :mocks) do
40
+ block_evaled = true
41
+ end
42
+ assert !block_evaled
43
+ end
44
+
45
+ def test_permit_with_object
46
+ reader = Authorization::Reader::DSLReader.new
47
+ reader.parse %{
48
+ authorization do
49
+ role :test_role do
50
+ has_permission_on :mocks do
51
+ to :show
52
+ if_attribute :test_attr => is {user.test_attr}
53
+ end
54
+ end
55
+ end
56
+ }
57
+ user = MockUser.new(:test_role, :test_attr => 1)
58
+ mock = MockDataObject.new(:test_attr => 1)
59
+ mock_2 = MockDataObject.new(:test_attr => 2)
60
+ request!(user, :action, reader)
61
+
62
+ assert permitted_to?(:show, mock)
63
+ assert permitted_to?(:show, :mocks)
64
+ assert !permitted_to?(:show, mock_2)
65
+ end
66
+
67
+ def test_permit_with_object_and_context
68
+ reader = Authorization::Reader::DSLReader.new
69
+ reader.parse %{
70
+ authorization do
71
+ role :test_role do
72
+ has_permission_on :other_mocks do
73
+ to :show
74
+ if_attribute :test_attr => is {user.test_attr}
75
+ end
76
+ end
77
+ end
78
+ }
79
+ user = MockUser.new(:test_role, :test_attr => 1)
80
+ mock = MockDataObject.new(:test_attr => 1)
81
+ mock_2 = MockDataObject.new(:test_attr => 2)
82
+ request!(user, :action, reader)
83
+
84
+ assert permitted_to?(:show, mock, :context => :other_mocks)
85
+ assert !permitted_to?(:show, mock_2, :context => :other_mocks)
86
+ end
87
+
88
+ def test_has_role
89
+ reader = Authorization::Reader::DSLReader.new
90
+ reader.parse %{
91
+ authorization do
92
+ role :test_role do
93
+ has_permission_on :mocks, :to => :show
94
+ end
95
+ end
96
+ }
97
+ user = MockUser.new(:test_role)
98
+ request!(user, :action, reader)
99
+
100
+ assert has_role?(:test_role)
101
+ assert !has_role?(:test_role2)
102
+
103
+ block_evaled = false
104
+ has_role?(:test_role) do
105
+ block_evaled = true
106
+ end
107
+ assert block_evaled
108
+
109
+ block_evaled = false
110
+ has_role?(:test_role2) do
111
+ block_evaled = true
112
+ end
113
+ assert !block_evaled
114
+ end
115
+
116
+ def test_has_role_with_guest_user
117
+ reader = Authorization::Reader::DSLReader.new
118
+ reader.parse %{
119
+ authorization do
120
+ end
121
+ }
122
+ request!(nil, :action, reader)
123
+
124
+ assert !has_role?(:test_role)
125
+
126
+ block_evaled = false
127
+ has_role?(:test_role) do
128
+ block_evaled = true
129
+ end
130
+ assert !block_evaled
131
+ end
132
+
133
+ def test_has_role_with_hierarchy
134
+ reader = Authorization::Reader::DSLReader.new
135
+ reader.parse %{
136
+ authorization do
137
+ role :test_role do
138
+ has_permission_on :mocks, :to => :show
139
+ end
140
+ role :other_role do
141
+ has_permission_on :another_mocks, :to => :show
142
+ end
143
+
144
+ role :root do
145
+ includes :test_role
146
+ end
147
+ end
148
+ }
149
+
150
+ user = MockUser.new(:root)
151
+ request!(user, :action, reader)
152
+
153
+ assert has_role_with_hierarchy?(:test_role)
154
+ assert !has_role_with_hierarchy?(:other_role)
155
+
156
+ block_evaled = false
157
+ has_role_with_hierarchy?(:test_role) do
158
+ block_evaled = true
159
+ end
160
+ assert block_evaled
161
+
162
+ block_evaled = false
163
+ has_role_with_hierarchy?(:test_role2) do
164
+ block_evaled = true
165
+ end
166
+ assert !block_evaled
167
+
168
+ end
169
+
170
+
171
+ end
@@ -0,0 +1,46 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+ require File.join(File.dirname(__FILE__), %w{.. lib declarative_authorization maintenance})
3
+
4
+ class MaintenanceTest < Test::Unit::TestCase
5
+ include Authorization::TestHelper
6
+
7
+ def test_usages_by_controllers
8
+ usage_test_controller = Class.new(ActionController::Base)
9
+ usage_test_controller.send(:define_method, :an_action) {}
10
+ usage_test_controller.filter_access_to :an_action
11
+
12
+ assert Authorization::Maintenance::Usage::usages_by_controller.
13
+ include?(usage_test_controller)
14
+ end
15
+
16
+ def test_without_access_control
17
+ reader = Authorization::Reader::DSLReader.new
18
+ reader.parse %{
19
+ authorization do
20
+ role :test_role do
21
+ has_permission_on :permissions, :to => :test
22
+ end
23
+ end
24
+ }
25
+ engine = Authorization::Engine.new(reader)
26
+ assert !engine.permit?(:test_2, :context => :permissions,
27
+ :user => MockUser.new(:test_role))
28
+ Authorization::Maintenance::without_access_control do
29
+ assert engine.permit!(:test_2, :context => :permissions,
30
+ :user => MockUser.new(:test_role))
31
+ end
32
+ without_access_control do
33
+ assert engine.permit?(:test_2, :context => :permissions,
34
+ :user => MockUser.new(:test_role))
35
+ end
36
+ Authorization::Maintenance::without_access_control do
37
+ Authorization::Maintenance::without_access_control do
38
+ assert engine.permit?(:test_2, :context => :permissions,
39
+ :user => MockUser.new(:test_role))
40
+ end
41
+ assert engine.permit?(:test_2, :context => :permissions,
42
+ :user => MockUser.new(:test_role))
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,1308 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+ require File.join(File.dirname(__FILE__), %w{.. lib declarative_authorization in_model})
3
+
4
+ ActiveRecord::Base.send :include, Authorization::AuthorizationInModel
5
+ #ActiveRecord::Base.logger = Logger.new(STDOUT)
6
+
7
+ options = {:adapter => 'sqlite3', :timeout => 500, :database => ':memory:'}
8
+ ActiveRecord::Base.establish_connection(options)
9
+ ActiveRecord::Base.configurations = { 'sqlite3_ar_integration' => options }
10
+ ActiveRecord::Base.connection
11
+
12
+ File.read(File.dirname(__FILE__) + "/schema.sql").split(';').each do |sql|
13
+ ActiveRecord::Base.connection.execute(sql) unless sql.blank?
14
+ end
15
+
16
+ class TestModel < ActiveRecord::Base
17
+ has_many :test_attrs
18
+ has_many :test_attr_throughs, :through => :test_attrs
19
+ has_many :test_attrs_with_attr, :class_name => "TestAttr", :conditions => {:attr => 1}
20
+ has_many :test_attr_throughs_with_attr, :through => :test_attrs,
21
+ :class_name => "TestAttrThrough", :source => :test_attr_throughs,
22
+ :conditions => "test_attrs.attr = 1"
23
+ has_one :test_attr_has_one, :class_name => "TestAttr"
24
+ has_one :test_attr_throughs_with_attr_and_has_one, :through => :test_attrs,
25
+ :class_name => "TestAttrThrough", :source => :test_attr_throughs,
26
+ :conditions => "test_attrs.attr = 1"
27
+
28
+ has_and_belongs_to_many :test_attr_throughs_habtm, :join_table => :test_attrs,
29
+ :class_name => "TestAttrThrough"
30
+
31
+ named_scope :with_content, :conditions => "test_models.content IS NOT NULL"
32
+
33
+ # Primary key test
34
+ # take this out for Rails prior to 2.2
35
+ if ([Rails::VERSION::MAJOR, Rails::VERSION::MINOR] <=> [2, 2]) > -1
36
+ has_many :test_attrs_with_primary_id, :class_name => "TestAttr",
37
+ :primary_key => :test_attr_through_id, :foreign_key => :test_attr_through_id
38
+ has_many :test_attr_throughs_with_primary_id,
39
+ :through => :test_attrs_with_primary_id, :class_name => "TestAttrThrough",
40
+ :source => :n_way_join_item
41
+ end
42
+ end
43
+
44
+ class NWayJoinItem < ActiveRecord::Base
45
+ has_many :test_attrs
46
+ has_many :others, :through => :test_attrs, :source => :n_way_join_item
47
+ end
48
+
49
+ class TestAttr < ActiveRecord::Base
50
+ belongs_to :test_model
51
+ belongs_to :test_another_model, :class_name => "TestModel", :foreign_key => :test_another_model_id
52
+ belongs_to :test_a_third_model, :class_name => "TestModel", :foreign_key => :test_a_third_model_id
53
+ belongs_to :n_way_join_item
54
+ belongs_to :test_attr
55
+ belongs_to :branch
56
+ belongs_to :company
57
+ has_many :test_attr_throughs
58
+ attr_reader :role_symbols
59
+ def initialize (*args)
60
+ @role_symbols = []
61
+ super(*args)
62
+ end
63
+ end
64
+
65
+ class TestAttrThrough < ActiveRecord::Base
66
+ belongs_to :test_attr
67
+ end
68
+
69
+ class TestModelSecurityModel < ActiveRecord::Base
70
+ has_many :test_attrs
71
+ using_access_control
72
+ end
73
+ class TestModelSecurityModelWithFind < ActiveRecord::Base
74
+ set_table_name "test_model_security_models"
75
+ has_many :test_attrs
76
+ using_access_control :include_read => true,
77
+ :context => :test_model_security_models
78
+ end
79
+
80
+ class Branch < ActiveRecord::Base
81
+ has_many :test_attrs
82
+ belongs_to :company
83
+ end
84
+ class Company < ActiveRecord::Base
85
+ has_many :test_attrs
86
+ has_many :branches
87
+ belongs_to :country
88
+ end
89
+ class SmallCompany < Company
90
+ def self.decl_auth_context
91
+ :companies
92
+ end
93
+ end
94
+ class Country < ActiveRecord::Base
95
+ has_many :test_models
96
+ has_many :companies
97
+ end
98
+
99
+ class ModelTest < Test::Unit::TestCase
100
+ def test_named_scope_multiple_deep_ored_belongs_to
101
+ reader = Authorization::Reader::DSLReader.new
102
+ reader.parse %{
103
+ authorization do
104
+ role :test_role do
105
+ has_permission_on :test_attrs, :to => :read do
106
+ if_attribute :test_model => {:test_attrs => contains {user}}
107
+ if_attribute :test_another_model => {:test_attrs => contains {user}}
108
+ end
109
+ end
110
+ end
111
+ }
112
+ Authorization::Engine.instance(reader)
113
+
114
+ test_model_1 = TestModel.create!
115
+ test_model_2 = TestModel.create!
116
+ test_attr_1 = TestAttr.create! :test_model_id => test_model_1.id,
117
+ :test_another_model_id => test_model_2.id
118
+
119
+ user = MockUser.new(:test_role, :id => test_attr_1)
120
+ assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
121
+ TestAttr.delete_all
122
+ TestModel.delete_all
123
+ end
124
+
125
+ def test_named_scope_with_belongs_to_and_has_many_with_contains
126
+ reader = Authorization::Reader::DSLReader.new
127
+ reader.parse %{
128
+ authorization do
129
+ role :test_role do
130
+ has_permission_on :test_attrs, :to => :read do
131
+ if_attribute :test_model => { :test_attrs => contains { user.test_attr_value } }
132
+ end
133
+ end
134
+ end
135
+ }
136
+ Authorization::Engine.instance(reader)
137
+
138
+ test_attr_1 = TestAttr.create!
139
+ test_model_1 = TestModel.create!
140
+ test_model_1.test_attrs.create!
141
+
142
+ user = MockUser.new(:test_role, :test_attr_value => test_model_1.test_attrs.first.id )
143
+ assert_equal 1, TestAttr.with_permissions_to( :read, :context => :test_attrs, :user => user ).length
144
+ assert_equal 1, TestAttr.with_permissions_to( :read, :user => user ).length
145
+ assert_raise Authorization::NotAuthorized do
146
+ TestAttr.with_permissions_to( :update_test_attrs, :user => user )
147
+ end
148
+ TestAttr.delete_all
149
+ TestModel.delete_all
150
+ end
151
+
152
+ def test_named_scope_with_is
153
+ reader = Authorization::Reader::DSLReader.new
154
+ reader.parse %{
155
+ authorization do
156
+ role :test_role do
157
+ has_permission_on :test_models, :to => :read do
158
+ if_attribute :id => is { user.test_attr_value }
159
+ end
160
+ end
161
+ end
162
+ }
163
+ Authorization::Engine.instance(reader)
164
+
165
+ test_model_1 = TestModel.create!
166
+ TestModel.create!
167
+
168
+ user = MockUser.new(:test_role, :test_attr_value => test_model_1.id)
169
+ assert_equal 1, TestModel.with_permissions_to(:read,
170
+ :context => :test_models, :user => user).length
171
+ assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
172
+ assert_raise Authorization::NotAuthorized do
173
+ TestModel.with_permissions_to(:update_test_models, :user => user)
174
+ end
175
+ TestModel.delete_all
176
+ end
177
+
178
+ def test_named_scope_on_proxy
179
+ reader = Authorization::Reader::DSLReader.new
180
+ reader.parse %{
181
+ authorization do
182
+ role :test_role do
183
+ has_permission_on :test_attrs, :to => :read do
184
+ if_attribute :id => is { user.test_attr_value }
185
+ end
186
+ end
187
+ end
188
+ }
189
+ Authorization::Engine.instance(reader)
190
+
191
+ test_model_1 = TestModel.create!
192
+ test_attr_1 = test_model_1.test_attrs.create!
193
+ test_model_1.test_attrs.create!
194
+ TestAttr.create!
195
+
196
+ user = MockUser.new(:test_role, :test_attr_value => test_attr_1.id)
197
+ assert_equal 1, test_model_1.test_attrs.with_permissions_to(:read, :user => user).length
198
+ TestModel.delete_all
199
+ TestAttr.delete_all
200
+ end
201
+
202
+ def test_named_scope_on_named_scope
203
+ reader = Authorization::Reader::DSLReader.new
204
+ reader.parse %{
205
+ authorization do
206
+ role :test_role do
207
+ has_permission_on :test_models, :to => :read do
208
+ if_attribute :country_id => 1
209
+ end
210
+ end
211
+ end
212
+ }
213
+ Authorization::Engine.instance(reader)
214
+
215
+ TestModel.create!(:country_id => 1, :content => "Content")
216
+ TestModel.create!(:country_id => 1)
217
+ TestModel.create!(:country_id => 2, :content => "Content")
218
+
219
+ user = MockUser.new(:test_role)
220
+ assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
221
+ assert_equal 1, TestModel.with_content.with_permissions_to(:read, :user => user).length
222
+ TestModel.delete_all
223
+ end
224
+
225
+ def test_named_scope_with_modified_context
226
+ reader = Authorization::Reader::DSLReader.new
227
+ reader.parse %{
228
+ authorization do
229
+ role :test_role do
230
+ has_permission_on :companies, :to => :read do
231
+ if_attribute :id => is { user.test_company_id }
232
+ end
233
+ end
234
+ end
235
+ }
236
+ Authorization::Engine.instance(reader)
237
+
238
+ test_company = SmallCompany.create!
239
+
240
+ user = MockUser.new(:test_role, :test_company_id => test_company.id)
241
+ assert_equal 1, SmallCompany.with_permissions_to(:read,
242
+ :user => user).length
243
+ SmallCompany.delete_all
244
+ end
245
+
246
+ def test_named_scope_with_is_nil
247
+ reader = Authorization::Reader::DSLReader.new
248
+ reader.parse %{
249
+ authorization do
250
+ role :test_role do
251
+ has_permission_on :test_models, :to => :read do
252
+ if_attribute :content => nil
253
+ end
254
+ end
255
+ role :test_role_not_nil do
256
+ has_permission_on :test_models, :to => :read do
257
+ if_attribute :content => is_not { nil }
258
+ end
259
+ end
260
+ end
261
+ }
262
+ Authorization::Engine.instance(reader)
263
+
264
+ test_model_1 = TestModel.create!
265
+ test_model_2 = TestModel.create! :content => "Content"
266
+
267
+ assert_equal test_model_1, TestModel.with_permissions_to(:read,
268
+ :context => :test_models, :user => MockUser.new(:test_role)).first
269
+ assert_equal test_model_2, TestModel.with_permissions_to(:read,
270
+ :context => :test_models, :user => MockUser.new(:test_role_not_nil)).first
271
+ TestModel.delete_all
272
+ end
273
+
274
+ def test_named_scope_with_not_is
275
+ reader = Authorization::Reader::DSLReader.new
276
+ reader.parse %{
277
+ authorization do
278
+ role :test_role do
279
+ has_permission_on :test_models, :to => :read do
280
+ if_attribute :id => is_not { user.test_attr_value }
281
+ end
282
+ end
283
+ end
284
+ }
285
+ Authorization::Engine.instance(reader)
286
+
287
+ test_model_1 = TestModel.create!
288
+ TestModel.create!
289
+
290
+ user = MockUser.new(:test_role, :test_attr_value => test_model_1.id)
291
+ assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
292
+ TestModel.delete_all
293
+ end
294
+
295
+ def test_named_scope_with_empty_obligations
296
+ reader = Authorization::Reader::DSLReader.new
297
+ reader.parse %{
298
+ authorization do
299
+ role :test_role do
300
+ has_permission_on :test_models, :to => :read
301
+ end
302
+ end
303
+ }
304
+ Authorization::Engine.instance(reader)
305
+
306
+ TestModel.create!
307
+
308
+ user = MockUser.new(:test_role)
309
+ assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
310
+ assert_raise Authorization::NotAuthorized do
311
+ TestModel.with_permissions_to(:update, :user => user)
312
+ end
313
+ TestModel.delete_all
314
+ end
315
+
316
+ def test_named_scope_multiple_obligations
317
+ reader = Authorization::Reader::DSLReader.new
318
+ reader.parse %{
319
+ authorization do
320
+ role :test_role do
321
+ has_permission_on :test_models, :to => :read do
322
+ if_attribute :id => is { user.test_attr_value }
323
+ end
324
+ has_permission_on :test_models, :to => :read do
325
+ if_attribute :id => is { user.test_attr_value_2 }
326
+ end
327
+ end
328
+ end
329
+ }
330
+ Authorization::Engine.instance(reader)
331
+
332
+ test_model_1 = TestModel.create!
333
+ test_model_2 = TestModel.create!
334
+
335
+ user = MockUser.new(:test_role, :test_attr_value => test_model_1.id,
336
+ :test_attr_value_2 => test_model_2.id)
337
+ assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
338
+ TestModel.delete_all
339
+ end
340
+
341
+ def test_named_scope_multiple_roles
342
+ reader = Authorization::Reader::DSLReader.new
343
+ reader.parse %{
344
+ authorization do
345
+ role :test_role do
346
+ has_permission_on :test_attrs, :to => :read do
347
+ if_attribute :attr => [1,2]
348
+ end
349
+ end
350
+
351
+ role :test_role_2 do
352
+ has_permission_on :test_attrs, :to => :read do
353
+ if_attribute :attr => [2,3]
354
+ end
355
+ end
356
+ end
357
+ }
358
+ Authorization::Engine.instance(reader)
359
+
360
+ TestAttr.create! :attr => 1
361
+ TestAttr.create! :attr => 2
362
+ TestAttr.create! :attr => 3
363
+
364
+ user = MockUser.new(:test_role)
365
+ assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
366
+ TestAttr.delete_all
367
+ end
368
+
369
+ def test_named_scope_multiple_and_empty_obligations
370
+ reader = Authorization::Reader::DSLReader.new
371
+ reader.parse %{
372
+ authorization do
373
+ role :test_role do
374
+ has_permission_on :test_models, :to => :read do
375
+ if_attribute :id => is { user.test_attr_value }
376
+ end
377
+ has_permission_on :test_models, :to => :read
378
+ end
379
+ end
380
+ }
381
+ Authorization::Engine.instance(reader)
382
+
383
+ test_model_1 = TestModel.create!
384
+ TestModel.create!
385
+
386
+ user = MockUser.new(:test_role, :test_attr_value => test_model_1.id)
387
+ assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
388
+ TestModel.delete_all
389
+ end
390
+
391
+ def test_named_scope_multiple_attributes
392
+ reader = Authorization::Reader::DSLReader.new
393
+ reader.parse %{
394
+ authorization do
395
+ role :test_role do
396
+ has_permission_on :test_models, :to => :read do
397
+ if_attribute :id => is { user.test_attr_value }, :content => "bla"
398
+ end
399
+ end
400
+ end
401
+ }
402
+ Authorization::Engine.instance(reader)
403
+
404
+ test_model_1 = TestModel.create! :content => 'bla'
405
+ TestModel.create! :content => 'bla'
406
+ TestModel.create!
407
+
408
+ user = MockUser.new(:test_role, :test_attr_value => test_model_1.id)
409
+ assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
410
+ TestModel.delete_all
411
+ end
412
+
413
+ def test_named_scope_multiple_belongs_to
414
+ reader = Authorization::Reader::DSLReader.new
415
+ reader.parse %{
416
+ authorization do
417
+ role :test_role do
418
+ has_permission_on :test_attrs, :to => :read do
419
+ if_attribute :test_model => is {user}
420
+ if_attribute :test_another_model => is {user}
421
+ end
422
+ end
423
+ end
424
+ }
425
+ Authorization::Engine.instance(reader)
426
+
427
+ test_attr_1 = TestAttr.create! :test_model_id => 1, :test_another_model_id => 2
428
+
429
+ user = MockUser.new(:test_role, :id => 1)
430
+ assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
431
+ TestAttr.delete_all
432
+ end
433
+
434
+ def test_named_scope_with_is_and_priv_hierarchy
435
+ reader = Authorization::Reader::DSLReader.new
436
+ reader.parse %{
437
+ privileges do
438
+ privilege :read do
439
+ includes :list, :show
440
+ end
441
+ end
442
+ authorization do
443
+ role :test_role do
444
+ has_permission_on :test_models, :to => :read do
445
+ if_attribute :id => is { user.test_attr_value }
446
+ end
447
+ end
448
+ end
449
+ }
450
+ Authorization::Engine.instance(reader)
451
+
452
+ test_model_1 = TestModel.create!
453
+ TestModel.create!
454
+
455
+ user = MockUser.new(:test_role, :test_attr_value => test_model_1.id)
456
+ assert_equal 1, TestModel.with_permissions_to(:list,
457
+ :context => :test_models, :user => user).length
458
+ assert_equal 1, TestModel.with_permissions_to(:list, :user => user).length
459
+
460
+ TestModel.delete_all
461
+ end
462
+
463
+ def test_named_scope_with_is_and_belongs_to
464
+ reader = Authorization::Reader::DSLReader.new
465
+ reader.parse %{
466
+ authorization do
467
+ role :test_role do
468
+ has_permission_on :test_attrs, :to => :read do
469
+ if_attribute :test_model => is { user.test_model }
470
+ end
471
+ end
472
+ end
473
+ }
474
+ Authorization::Engine.instance(reader)
475
+
476
+ test_model_1 = TestModel.create!
477
+ test_model_1.test_attrs.create!
478
+ TestModel.create!.test_attrs.create!
479
+
480
+ user = MockUser.new(:test_role, :test_model => test_model_1)
481
+ assert_equal 1, TestAttr.with_permissions_to(:read,
482
+ :context => :test_attrs, :user => user).length
483
+
484
+ TestModel.delete_all
485
+ TestAttr.delete_all
486
+ end
487
+
488
+ def test_named_scope_with_deep_attribute
489
+ reader = Authorization::Reader::DSLReader.new
490
+ reader.parse %{
491
+ authorization do
492
+ role :test_role do
493
+ has_permission_on :test_attrs, :to => :read do
494
+ if_attribute :test_model => {:id => is { user.test_model_id } }
495
+ end
496
+ end
497
+ end
498
+ }
499
+ Authorization::Engine.instance(reader)
500
+
501
+ test_model_1 = TestModel.create!
502
+ test_model_1.test_attrs.create!
503
+ TestModel.create!.test_attrs.create!
504
+
505
+ user = MockUser.new(:test_role, :test_model_id => test_model_1.id)
506
+ assert_equal 1, TestAttr.with_permissions_to(:read,
507
+ :context => :test_attrs, :user => user).length
508
+
509
+ TestModel.delete_all
510
+ TestAttr.delete_all
511
+ end
512
+
513
+ def test_named_scope_with_anded_rules
514
+ reader = Authorization::Reader::DSLReader.new
515
+ reader.parse %{
516
+ authorization do
517
+ role :test_role do
518
+ has_permission_on :test_attrs, :to => :read, :join_by => :and do
519
+ if_attribute :test_model => is { user.test_model }
520
+ if_attribute :attr => 1
521
+ end
522
+ end
523
+ end
524
+ }
525
+ Authorization::Engine.instance(reader)
526
+
527
+ test_model_1 = TestModel.create!
528
+ test_model_1.test_attrs.create!(:attr => 1)
529
+ TestModel.create!.test_attrs.create!(:attr => 1)
530
+ TestModel.create!.test_attrs.create!
531
+
532
+ user = MockUser.new(:test_role, :test_model => test_model_1)
533
+ assert_equal 1, TestAttr.with_permissions_to(:read,
534
+ :context => :test_attrs, :user => user).length
535
+
536
+ TestModel.delete_all
537
+ TestAttr.delete_all
538
+ end
539
+
540
+ def test_named_scope_with_contains
541
+ reader = Authorization::Reader::DSLReader.new
542
+ reader.parse %{
543
+ authorization do
544
+ role :test_role do
545
+ has_permission_on :test_models, :to => :read do
546
+ if_attribute :test_attrs => contains { user }
547
+ end
548
+ end
549
+ end
550
+ }
551
+ Authorization::Engine.instance(reader)
552
+
553
+ test_model_1 = TestModel.create!
554
+ test_model_2 = TestModel.create!
555
+ test_model_1.test_attrs.create!
556
+ test_model_1.test_attrs.create!
557
+ test_model_2.test_attrs.create!
558
+
559
+ user = MockUser.new(:test_role,
560
+ :id => test_model_1.test_attrs.first.id)
561
+ assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
562
+ assert_equal 1, TestModel.with_permissions_to(:read, :user => user).find(:all, :conditions => {:id => test_model_1.id}).length
563
+
564
+ TestModel.delete_all
565
+ TestAttr.delete_all
566
+ end
567
+
568
+ def test_named_scope_with_does_not_contain
569
+ reader = Authorization::Reader::DSLReader.new
570
+ reader.parse %{
571
+ authorization do
572
+ role :test_role do
573
+ has_permission_on :test_models, :to => :read do
574
+ if_attribute :test_attrs => does_not_contain { user }
575
+ end
576
+ end
577
+ end
578
+ }
579
+ Authorization::Engine.instance(reader)
580
+
581
+ test_model_1 = TestModel.create!
582
+ test_model_2 = TestModel.create!
583
+ test_model_1.test_attrs.create!
584
+ test_model_2.test_attrs.create!
585
+
586
+ user = MockUser.new(:test_role,
587
+ :id => test_model_1.test_attrs.first.id)
588
+ assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
589
+
590
+ TestModel.delete_all
591
+ TestAttr.delete_all
592
+ end
593
+
594
+ def test_named_scope_with_contains_conditions
595
+ reader = Authorization::Reader::DSLReader.new
596
+ reader.parse %{
597
+ authorization do
598
+ role :test_role do
599
+ has_permission_on :test_models, :to => :read do
600
+ if_attribute :test_attrs_with_attr => contains { user }
601
+ end
602
+ end
603
+ end
604
+ }
605
+ Authorization::Engine.instance(reader)
606
+
607
+ test_model_1 = TestModel.create!
608
+ test_model_2 = TestModel.create!
609
+ test_model_1.test_attrs_with_attr.create!
610
+ test_model_1.test_attrs.create!(:attr => 2)
611
+ test_model_2.test_attrs_with_attr.create!
612
+ test_model_2.test_attrs.create!(:attr => 2)
613
+
614
+ #assert_equal 1, test_model_1.test_attrs_with_attr.length
615
+ user = MockUser.new(:test_role,
616
+ :id => test_model_1.test_attrs.first.id)
617
+ assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
618
+ user = MockUser.new(:test_role,
619
+ :id => test_model_1.test_attrs.last.id)
620
+ assert_equal 0, TestModel.with_permissions_to(:read, :user => user).length
621
+
622
+ TestModel.delete_all
623
+ TestAttr.delete_all
624
+ end
625
+
626
+ def test_named_scope_with_contains_through_conditions
627
+ reader = Authorization::Reader::DSLReader.new
628
+ reader.parse %{
629
+ authorization do
630
+ role :test_role do
631
+ has_permission_on :test_models, :to => :read do
632
+ if_attribute :test_attr_throughs_with_attr => contains { user }
633
+ end
634
+ end
635
+ end
636
+ }
637
+ Authorization::Engine.instance(reader)
638
+
639
+ test_model_1 = TestModel.create!
640
+ test_model_2 = TestModel.create!
641
+ test_model_1.test_attrs.create!(:attr => 1).test_attr_throughs.create!
642
+ test_model_1.test_attrs.create!(:attr => 2).test_attr_throughs.create!
643
+ test_model_2.test_attrs.create!(:attr => 1).test_attr_throughs.create!
644
+ test_model_2.test_attrs.create!(:attr => 2).test_attr_throughs.create!
645
+
646
+ #assert_equal 1, test_model_1.test_attrs_with_attr.length
647
+ user = MockUser.new(:test_role,
648
+ :id => test_model_1.test_attr_throughs.first.id)
649
+ assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
650
+ user = MockUser.new(:test_role,
651
+ :id => test_model_1.test_attr_throughs.last.id)
652
+ assert_equal 0, TestModel.with_permissions_to(:read, :user => user).length
653
+
654
+ TestModel.delete_all
655
+ TestAttrThrough.delete_all
656
+ TestAttr.delete_all
657
+ end
658
+
659
+ def test_named_scope_with_contains_habtm
660
+ reader = Authorization::Reader::DSLReader.new
661
+ reader.parse %{
662
+ authorization do
663
+ role :test_role do
664
+ has_permission_on :test_models, :to => :read do
665
+ if_attribute :test_attr_throughs_habtm => contains { user.test_attr_through_id }
666
+ end
667
+ end
668
+ end
669
+ }
670
+ Authorization::Engine.instance(reader)
671
+
672
+ test_model_1 = TestModel.create!
673
+ test_model_2 = TestModel.create!
674
+ test_attr_through_1 = TestAttrThrough.create!
675
+ test_attr_through_2 = TestAttrThrough.create!
676
+ TestAttr.create! :test_model_id => test_model_1.id, :test_attr_through_id => test_attr_through_1.id
677
+ TestAttr.create! :test_model_id => test_model_2.id, :test_attr_through_id => test_attr_through_2.id
678
+
679
+ user = MockUser.new(:test_role,
680
+ :test_attr_through_id => test_model_1.test_attr_throughs_habtm.first.id)
681
+ assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
682
+ assert_equal test_model_1, TestModel.with_permissions_to(:read, :user => user)[0]
683
+
684
+ TestModel.delete_all
685
+ TestAttrThrough.delete_all
686
+ TestAttr.delete_all
687
+ end
688
+
689
+ # take this out for Rails prior to 2.2
690
+ if ([Rails::VERSION::MAJOR, Rails::VERSION::MINOR] <=> [2, 2]) > -1
691
+ def test_named_scope_with_contains_through_primary_key
692
+ reader = Authorization::Reader::DSLReader.new
693
+ reader.parse %{
694
+ authorization do
695
+ role :test_role do
696
+ has_permission_on :test_models, :to => :read do
697
+ if_attribute :test_attr_throughs_with_primary_id => contains { user }
698
+ end
699
+ end
700
+ end
701
+ }
702
+ Authorization::Engine.instance(reader)
703
+
704
+ test_attr_through_1 = TestAttrThrough.create!
705
+ test_item = NWayJoinItem.create!
706
+ test_model_1 = TestModel.create!(:test_attr_through_id => test_attr_through_1.id)
707
+ test_attr_1 = TestAttr.create!(:test_attr_through_id => test_attr_through_1.id,
708
+ :n_way_join_item_id => test_item.id)
709
+
710
+ user = MockUser.new(:test_role,
711
+ :id => test_attr_through_1.id)
712
+ assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
713
+
714
+ TestModel.delete_all
715
+ TestAttrThrough.delete_all
716
+ TestAttr.delete_all
717
+ end
718
+ end
719
+
720
+ def test_named_scope_with_intersects_with
721
+ reader = Authorization::Reader::DSLReader.new
722
+ reader.parse %{
723
+ authorization do
724
+ role :test_role do
725
+ has_permission_on :test_models, :to => :read do
726
+ if_attribute :test_attrs => intersects_with { user.test_attrs }
727
+ end
728
+ end
729
+ end
730
+ }
731
+ Authorization::Engine.instance(reader)
732
+
733
+ test_model_1 = TestModel.create!
734
+ test_model_2 = TestModel.create!
735
+ test_model_1.test_attrs.create!
736
+ test_model_1.test_attrs.create!
737
+ test_model_1.test_attrs.create!
738
+ test_model_2.test_attrs.create!
739
+
740
+ user = MockUser.new(:test_role,
741
+ :test_attrs => [test_model_1.test_attrs.first, TestAttr.create!])
742
+ assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
743
+
744
+ user = MockUser.new(:test_role,
745
+ :test_attrs => [TestAttr.create!])
746
+ assert_equal 0, TestModel.with_permissions_to(:read, :user => user).length
747
+
748
+ TestModel.delete_all
749
+ TestAttr.delete_all
750
+ end
751
+
752
+ def test_named_scope_with_is_and_has_one
753
+ reader = Authorization::Reader::DSLReader.new
754
+ reader.parse %{
755
+ authorization do :test_attr_has_one
756
+ role :test_role do
757
+ has_permission_on :test_models, :to => :read do
758
+ if_attribute :test_attr_has_one => is { user.test_attr }
759
+ end
760
+ end
761
+ end
762
+ }
763
+ Authorization::Engine.instance(reader)
764
+
765
+ test_model_1 = TestModel.create!
766
+ test_attr_1 = test_model_1.test_attrs.create!
767
+ TestModel.create!.test_attrs.create!
768
+
769
+ user = MockUser.new(:test_role, :test_attr => test_attr_1)
770
+ assert_equal 1, TestModel.with_permissions_to(:read,
771
+ :context => :test_models, :user => user).length
772
+
773
+ TestModel.delete_all
774
+ TestAttr.delete_all
775
+ end
776
+
777
+ def test_permit_with_has_one_raises_no_name_error
778
+ reader = Authorization::Reader::DSLReader.new
779
+ reader.parse %{
780
+ authorization do :test_attr_has_one
781
+ role :test_role do
782
+ has_permission_on :test_attrs, :to => :update do
783
+ if_attribute :id => is { user.test_attr.id }
784
+ end
785
+ end
786
+ end
787
+ }
788
+ instance = Authorization::Engine.instance(reader)
789
+
790
+ test_model = TestModel.create!
791
+ test_attr = test_model.create_test_attr_has_one
792
+ assert !test_attr.new_record?
793
+
794
+ user = MockUser.new(:test_role, :test_attr => test_attr)
795
+
796
+ assert_nothing_raised do
797
+ assert instance.permit?(:update, :user => user, :object => test_model.test_attr_has_one)
798
+ end
799
+
800
+ TestModel.delete_all
801
+ TestAttr.delete_all
802
+ end
803
+
804
+ def test_named_scope_with_is_and_has_one_through_conditions
805
+ reader = Authorization::Reader::DSLReader.new
806
+ reader.parse %{
807
+ authorization do
808
+ role :test_role do
809
+ has_permission_on :test_models, :to => :read do
810
+ if_attribute :test_attr_throughs_with_attr_and_has_one => contains { user }
811
+ end
812
+ end
813
+ end
814
+ }
815
+ Authorization::Engine.instance(reader)
816
+
817
+ test_model_1 = TestModel.create!
818
+ test_model_2 = TestModel.create!
819
+ test_model_1.test_attrs.create!(:attr => 1).test_attr_throughs.create!
820
+ test_model_1.test_attrs.create!(:attr => 2).test_attr_throughs.create!
821
+ test_model_2.test_attrs.create!(:attr => 1).test_attr_throughs.create!
822
+ test_model_2.test_attrs.create!(:attr => 2).test_attr_throughs.create!
823
+
824
+ #assert_equal 1, test_model_1.test_attrs_with_attr.length
825
+ user = MockUser.new(:test_role,
826
+ :id => test_model_1.test_attr_throughs.first.id)
827
+ assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
828
+ user = MockUser.new(:test_role,
829
+ :id => test_model_1.test_attr_throughs.last.id)
830
+ assert_equal 0, TestModel.with_permissions_to(:read, :user => user).length
831
+
832
+ TestModel.delete_all
833
+ TestAttr.delete_all
834
+ end
835
+
836
+ def test_named_scope_with_is_in
837
+ reader = Authorization::Reader::DSLReader.new
838
+ reader.parse %{
839
+ authorization do
840
+ role :test_role do
841
+ has_permission_on :test_attrs, :to => :read do
842
+ if_attribute :test_model => is_in { [user.test_model, user.test_model_2] }
843
+ end
844
+ end
845
+ end
846
+ }
847
+ Authorization::Engine.instance(reader)
848
+
849
+ test_model_1 = TestModel.create!
850
+ test_model_2 = TestModel.create!
851
+ test_model_1.test_attrs.create!
852
+ TestModel.create!.test_attrs.create!
853
+
854
+ user = MockUser.new(:test_role, :test_model => test_model_1,
855
+ :test_model_2 => test_model_2)
856
+ assert_equal 1, TestAttr.with_permissions_to(:read,
857
+ :context => :test_attrs, :user => user).length
858
+
859
+ TestModel.delete_all
860
+ TestAttr.delete_all
861
+ end
862
+
863
+ def test_named_scope_with_not_is_in
864
+ reader = Authorization::Reader::DSLReader.new
865
+ reader.parse %{
866
+ authorization do
867
+ role :test_role do
868
+ has_permission_on :test_attrs, :to => :read do
869
+ if_attribute :test_model => is_not_in { [user.test_model, user.test_model_2] }
870
+ end
871
+ end
872
+ end
873
+ }
874
+ Authorization::Engine.instance(reader)
875
+
876
+ test_model_1 = TestModel.create!
877
+ test_model_2 = TestModel.create!
878
+ test_model_1.test_attrs.create!
879
+ TestModel.create!.test_attrs.create!
880
+
881
+ user = MockUser.new(:test_role, :test_model => test_model_1,
882
+ :test_model_2 => test_model_2)
883
+ assert_equal 1, TestAttr.with_permissions_to(:read,
884
+ :context => :test_attrs, :user => user).length
885
+
886
+ TestModel.delete_all
887
+ TestAttr.delete_all
888
+ end
889
+
890
+ def test_named_scope_with_if_permitted_to
891
+ reader = Authorization::Reader::DSLReader.new
892
+ reader.parse %{
893
+ authorization do
894
+ role :test_role do
895
+ has_permission_on :test_models, :to => :read do
896
+ if_attribute :test_attrs => contains { user }
897
+ end
898
+ has_permission_on :test_attrs, :to => :read do
899
+ if_permitted_to :read, :test_model
900
+ end
901
+ end
902
+ end
903
+ }
904
+ Authorization::Engine.instance(reader)
905
+
906
+ test_model_1 = TestModel.create!
907
+ test_attr_1 = test_model_1.test_attrs.create!
908
+
909
+ user = MockUser.new(:test_role, :id => test_attr_1.id)
910
+ assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
911
+ TestModel.delete_all
912
+ TestAttr.delete_all
913
+ end
914
+
915
+ def test_named_scope_with_if_permitted_to_and_empty_obligations
916
+ reader = Authorization::Reader::DSLReader.new
917
+ reader.parse %{
918
+ authorization do
919
+ role :test_role do
920
+ has_permission_on :test_models, :to => :read
921
+ has_permission_on :test_attrs, :to => :read do
922
+ if_permitted_to :read, :test_model
923
+ end
924
+ end
925
+ end
926
+ }
927
+ Authorization::Engine.instance(reader)
928
+
929
+ test_model_1 = TestModel.create!
930
+ test_attr_1 = test_model_1.test_attrs.create!
931
+
932
+ user = MockUser.new(:test_role)
933
+ assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
934
+ TestModel.delete_all
935
+ TestAttr.delete_all
936
+ end
937
+
938
+ def test_named_scope_with_if_permitted_to_nil
939
+ reader = Authorization::Reader::DSLReader.new
940
+ reader.parse %{
941
+ authorization do
942
+ role :test_role do
943
+ has_permission_on :test_models, :to => :read do
944
+ if_attribute :test_attrs => contains { user }
945
+ end
946
+ has_permission_on :test_attrs, :to => :read do
947
+ if_permitted_to :read, :test_model
948
+ end
949
+ end
950
+ end
951
+ }
952
+ Authorization::Engine.instance(reader)
953
+
954
+ test_attr_1 = TestAttr.create!
955
+
956
+ user = MockUser.new(:test_role, :id => test_attr_1.id)
957
+ assert_equal 0, TestAttr.with_permissions_to(:read, :user => user).length
958
+ TestAttr.delete_all
959
+ end
960
+
961
+ def test_named_scope_with_if_permitted_to_self
962
+ reader = Authorization::Reader::DSLReader.new
963
+ reader.parse %{
964
+ authorization do
965
+ role :test_role do
966
+ has_permission_on :test_models, :to => :read do
967
+ if_attribute :test_attrs => contains { user }
968
+ end
969
+ has_permission_on :test_models, :to => :update do
970
+ if_permitted_to :read
971
+ end
972
+ end
973
+ end
974
+ }
975
+ Authorization::Engine.instance(reader)
976
+
977
+ test_model_1 = TestModel.create!
978
+ test_attr_1 = test_model_1.test_attrs.create!
979
+ test_attr_2 = TestAttr.create!
980
+
981
+ user = MockUser.new(:test_role, :id => test_attr_1.id)
982
+ assert_equal 1, TestModel.with_permissions_to(:update, :user => user).length
983
+ TestAttr.delete_all
984
+ TestModel.delete_all
985
+ end
986
+
987
+ def test_model_security
988
+ reader = Authorization::Reader::DSLReader.new
989
+ reader.parse %{
990
+ authorization do
991
+ role :test_role_unrestricted do
992
+ has_permission_on :test_model_security_models do
993
+ to :read, :create, :update, :delete
994
+ end
995
+ end
996
+ role :test_role do
997
+ has_permission_on :test_model_security_models do
998
+ to :read, :create, :update, :delete
999
+ if_attribute :attr => is { 1 }
1000
+ end
1001
+ end
1002
+ role :test_role_restricted do
1003
+ end
1004
+ end
1005
+ }
1006
+ Authorization::Engine.instance(reader)
1007
+
1008
+ Authorization.current_user = MockUser.new(:test_role)
1009
+ assert(object = TestModelSecurityModel.create)
1010
+ Authorization.current_user = MockUser.new(:test_role_restricted)
1011
+ assert_raise Authorization::NotAuthorized do
1012
+ object.update_attributes(:attr_2 => 2)
1013
+ end
1014
+ Authorization.current_user = MockUser.new(:test_role)
1015
+ assert_nothing_raised { object.update_attributes(:attr_2 => 2) }
1016
+ object.reload
1017
+ assert_equal 2, object.attr_2
1018
+ object.destroy
1019
+ assert_raise ActiveRecord::RecordNotFound do
1020
+ TestModelSecurityModel.find(object.id)
1021
+ end
1022
+
1023
+ assert_raise Authorization::AttributeAuthorizationError do
1024
+ TestModelSecurityModel.create :attr => 2
1025
+ end
1026
+ object = TestModelSecurityModel.create
1027
+ assert_raise Authorization::AttributeAuthorizationError do
1028
+ object.update_attributes(:attr => 2)
1029
+ end
1030
+ Authorization.current_user = MockUser.new(:test_role_unrestricted)
1031
+ object = TestModelSecurityModel.create :attr => 2
1032
+ object_with_find = TestModelSecurityModelWithFind.create :attr => 2
1033
+ Authorization.current_user = MockUser.new(:test_role)
1034
+ assert_nothing_raised do
1035
+ object.class.find(object.id)
1036
+ end
1037
+ assert_raise Authorization::AttributeAuthorizationError do
1038
+ object_with_find.class.find(object_with_find.id)
1039
+ end
1040
+ assert_raise Authorization::AttributeAuthorizationError do
1041
+ object.update_attributes(:attr_2 => 2)
1042
+ end
1043
+ # TODO test this:
1044
+ #assert_raise Authorization::AuthorizationError do
1045
+ # object.update_attributes(:attr => 1)
1046
+ #end
1047
+ assert_raise Authorization::AttributeAuthorizationError do
1048
+ object.destroy
1049
+ end
1050
+
1051
+ Authorization.current_user = MockUser.new(:test_role_2)
1052
+ assert_raise Authorization::NotAuthorized do
1053
+ TestModelSecurityModel.create
1054
+ end
1055
+ end
1056
+
1057
+ def test_model_security_with_assoc
1058
+ reader = Authorization::Reader::DSLReader.new
1059
+ reader.parse %{
1060
+ authorization do
1061
+ role :test_role do
1062
+ has_permission_on :test_model_security_models do
1063
+ to :create, :update, :delete
1064
+ if_attribute :test_attrs => contains { user }
1065
+ end
1066
+ end
1067
+ end
1068
+ }
1069
+ Authorization::Engine.instance(reader)
1070
+
1071
+ test_attr = TestAttr.create
1072
+ test_attr.role_symbols << :test_role
1073
+ Authorization.current_user = test_attr
1074
+ assert(object = TestModelSecurityModel.create(:test_attrs => [test_attr]))
1075
+ assert_nothing_raised do
1076
+ object.update_attributes(:attr_2 => 2)
1077
+ end
1078
+ object.reload
1079
+ assert_equal 2, object.attr_2
1080
+ object.destroy
1081
+ assert_raise ActiveRecord::RecordNotFound do
1082
+ TestModelSecurityModel.find(object.id)
1083
+ end
1084
+ end
1085
+
1086
+ def test_using_access_control
1087
+ assert !TestModel.using_access_control?
1088
+ assert TestModelSecurityModel.using_access_control?
1089
+ end
1090
+
1091
+ def test_authorization_permit_association_proxy
1092
+ reader = Authorization::Reader::DSLReader.new
1093
+ reader.parse %{
1094
+ authorization do
1095
+ role :test_role do
1096
+ has_permission_on :test_attrs, :to => :read do
1097
+ if_attribute :test_model => {:content => "content" }
1098
+ end
1099
+ end
1100
+ end
1101
+ }
1102
+ engine = Authorization::Engine.instance(reader)
1103
+
1104
+ test_model = TestModel.create(:content => "content")
1105
+ assert engine.permit?(:read, :object => test_model.test_attrs,
1106
+ :user => MockUser.new(:test_role))
1107
+ assert !engine.permit?(:read, :object => TestAttr.new,
1108
+ :user => MockUser.new(:test_role))
1109
+ TestModel.delete_all
1110
+ end
1111
+
1112
+ def test_multiple_roles_with_has_many_through
1113
+ reader = Authorization::Reader::DSLReader.new
1114
+ reader.parse %{
1115
+ authorization do
1116
+ role :test_role_1 do
1117
+ has_permission_on :test_models, :to => :read do
1118
+ if_attribute :test_attr_throughs => contains {user.test_attr_through_id},
1119
+ :content => 'test_1'
1120
+ end
1121
+ end
1122
+
1123
+ role :test_role_2 do
1124
+ has_permission_on :test_models, :to => :read do
1125
+ if_attribute :test_attr_throughs_2 => contains {user.test_attr_through_2_id},
1126
+ :content => 'test_2'
1127
+ end
1128
+ end
1129
+ end
1130
+ }
1131
+ Authorization::Engine.instance(reader)
1132
+
1133
+ test_model_1 = TestModel.create! :content => 'test_1'
1134
+ test_model_2 = TestModel.create! :content => 'test_2'
1135
+ test_model_1.test_attrs.create!.test_attr_throughs.create!
1136
+ test_model_2.test_attrs.create!.test_attr_throughs.create!
1137
+
1138
+ user = MockUser.new(:test_role_1, :test_role_2,
1139
+ :test_attr_through_id => test_model_1.test_attr_throughs.first.id,
1140
+ :test_attr_through_2_id => test_model_2.test_attr_throughs.first.id)
1141
+ assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
1142
+ TestModel.delete_all
1143
+ TestAttr.delete_all
1144
+ TestAttrThrough.delete_all
1145
+ end
1146
+
1147
+ def test_named_scope_with_has_many_and_reoccuring_tables
1148
+ reader = Authorization::Reader::DSLReader.new
1149
+ reader.parse %{
1150
+ authorization do
1151
+ role :test_role do
1152
+ has_permission_on :test_attrs, :to => :read do
1153
+ if_attribute :test_another_model => { :content => 'test_1_2' },
1154
+ :test_model => { :content => 'test_1_1' }
1155
+ end
1156
+ end
1157
+ end
1158
+ }
1159
+ Authorization::Engine.instance(reader)
1160
+
1161
+ test_attr_1 = TestAttr.create!(
1162
+ :test_model => TestModel.create!(:content => 'test_1_1'),
1163
+ :test_another_model => TestModel.create!(:content => 'test_1_2')
1164
+ )
1165
+ test_attr_2 = TestAttr.create!(
1166
+ :test_model => TestModel.create!(:content => 'test_2_1'),
1167
+ :test_another_model => TestModel.create!(:content => 'test_2_2')
1168
+ )
1169
+
1170
+ user = MockUser.new(:test_role)
1171
+ assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
1172
+ TestModel.delete_all
1173
+ TestAttr.delete_all
1174
+ end
1175
+
1176
+ def test_named_scope_with_ored_rules_and_reoccuring_tables
1177
+ reader = Authorization::Reader::DSLReader.new
1178
+ reader.parse %{
1179
+ authorization do
1180
+ role :test_role do
1181
+ has_permission_on :test_attrs, :to => :read do
1182
+ if_attribute :test_another_model => { :content => 'test_1_2' },
1183
+ :test_model => { :content => 'test_1_1' }
1184
+ end
1185
+ has_permission_on :test_attrs, :to => :read do
1186
+ if_attribute :test_another_model => { :content => 'test_2_2' },
1187
+ :test_model => { :test_attrs => contains {user.test_attr} }
1188
+ end
1189
+ end
1190
+ end
1191
+ }
1192
+ Authorization::Engine.instance(reader)
1193
+
1194
+ test_attr_1 = TestAttr.create!(
1195
+ :test_model => TestModel.create!(:content => 'test_1_1'),
1196
+ :test_another_model => TestModel.create!(:content => 'test_1_2')
1197
+ )
1198
+ test_attr_2 = TestAttr.create!(
1199
+ :test_model => TestModel.create!(:content => 'test_2_1'),
1200
+ :test_another_model => TestModel.create!(:content => 'test_2_2')
1201
+ )
1202
+ test_attr_2.test_model.test_attrs.create!
1203
+
1204
+ user = MockUser.new(:test_role, :test_attr => test_attr_2.test_model.test_attrs.last)
1205
+ assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
1206
+ TestModel.delete_all
1207
+ TestAttr.delete_all
1208
+ end
1209
+
1210
+ def test_named_scope_with_many_ored_rules_and_reoccuring_tables
1211
+ reader = Authorization::Reader::DSLReader.new
1212
+ reader.parse %{
1213
+ authorization do
1214
+ role :test_role do
1215
+ has_permission_on :test_attrs, :to => :read do
1216
+ if_attribute :branch => { :company => { :country => {
1217
+ :test_models => contains { user.test_model }
1218
+ }} }
1219
+ if_attribute :company => { :country => {
1220
+ :test_models => contains { user.test_model }
1221
+ }}
1222
+ end
1223
+ end
1224
+ end
1225
+ }
1226
+ Authorization::Engine.instance(reader)
1227
+
1228
+ country = Country.create!(:name => 'country_1')
1229
+ country.test_models.create!
1230
+ test_attr_1 = TestAttr.create!(
1231
+ :branch => Branch.create!(:name => 'branch_1',
1232
+ :company => Company.create!(:name => 'company_1',
1233
+ :country => country))
1234
+ )
1235
+ test_attr_2 = TestAttr.create!(
1236
+ :company => Company.create!(:name => 'company_2',
1237
+ :country => country)
1238
+ )
1239
+
1240
+ user = MockUser.new(:test_role, :test_model => country.test_models.first)
1241
+
1242
+ assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
1243
+ TestModel.delete_all
1244
+ TestAttr.delete_all
1245
+ end
1246
+
1247
+ def test_model_permitted_to
1248
+ reader = Authorization::Reader::DSLReader.new
1249
+ reader.parse %{
1250
+ authorization do
1251
+ role :test_role do
1252
+ has_permission_on :companies, :to => :read do
1253
+ if_attribute :name => "company_1"
1254
+ end
1255
+ end
1256
+ end
1257
+ }
1258
+ Authorization::Engine.instance(reader)
1259
+
1260
+ user = MockUser.new(:test_role)
1261
+ allowed_read_company = Company.new(:name => 'company_1')
1262
+ prohibited_company = Company.new(:name => 'company_2')
1263
+
1264
+ assert allowed_read_company.permitted_to?(:read, :user => user)
1265
+ assert !allowed_read_company.permitted_to?(:update, :user => user)
1266
+ assert !prohibited_company.permitted_to?(:read, :user => user)
1267
+
1268
+ executed_block = false
1269
+ allowed_read_company.permitted_to?(:read, :user => user) do
1270
+ executed_block = true
1271
+ end
1272
+ assert executed_block
1273
+
1274
+ executed_block = false
1275
+ prohibited_company.permitted_to?(:read, :user => user) do
1276
+ executed_block = true
1277
+ end
1278
+ assert !executed_block
1279
+
1280
+ assert_nothing_raised do
1281
+ allowed_read_company.permitted_to!(:read, :user => user)
1282
+ end
1283
+ assert_raise Authorization::NotAuthorized do
1284
+ prohibited_company.permitted_to!(:update, :user => user)
1285
+ end
1286
+ assert_raise Authorization::AttributeAuthorizationError do
1287
+ prohibited_company.permitted_to!(:read, :user => user)
1288
+ end
1289
+ end
1290
+
1291
+ def test_model_permitted_to_with_modified_context
1292
+ reader = Authorization::Reader::DSLReader.new
1293
+ reader.parse %{
1294
+ authorization do
1295
+ role :test_role do
1296
+ has_permission_on :companies, :to => :read
1297
+ end
1298
+ end
1299
+ }
1300
+ Authorization::Engine.instance(reader)
1301
+
1302
+ user = MockUser.new(:test_role)
1303
+ allowed_read_company = SmallCompany.new(:name => 'small_company_1')
1304
+
1305
+ assert allowed_read_company.permitted_to?(:read, :user => user)
1306
+ assert !allowed_read_company.permitted_to?(:update, :user => user)
1307
+ end
1308
+ end