declarative_authorization-dta 0.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.
- data/CHANGELOG +148 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +504 -0
- data/Rakefile +35 -0
- data/app/controllers/authorization_rules_controller.rb +259 -0
- data/app/controllers/authorization_usages_controller.rb +23 -0
- data/app/helpers/authorization_rules_helper.rb +218 -0
- data/app/views/authorization_rules/_change.erb +58 -0
- data/app/views/authorization_rules/_show_graph.erb +37 -0
- data/app/views/authorization_rules/_suggestions.erb +48 -0
- data/app/views/authorization_rules/change.html.erb +169 -0
- data/app/views/authorization_rules/graph.dot.erb +68 -0
- data/app/views/authorization_rules/graph.html.erb +40 -0
- data/app/views/authorization_rules/index.html.erb +17 -0
- data/app/views/authorization_usages/index.html.erb +36 -0
- data/authorization_rules.dist.rb +20 -0
- data/config/routes.rb +10 -0
- data/garlic_example.rb +20 -0
- data/init.rb +5 -0
- data/lib/declarative_authorization.rb +17 -0
- data/lib/declarative_authorization/authorization.rb +687 -0
- data/lib/declarative_authorization/development_support/analyzer.rb +252 -0
- data/lib/declarative_authorization/development_support/change_analyzer.rb +253 -0
- data/lib/declarative_authorization/development_support/change_supporter.rb +620 -0
- data/lib/declarative_authorization/development_support/development_support.rb +243 -0
- data/lib/declarative_authorization/helper.rb +60 -0
- data/lib/declarative_authorization/in_controller.rb +623 -0
- data/lib/declarative_authorization/in_model.new.rb +298 -0
- data/lib/declarative_authorization/in_model.rb +463 -0
- data/lib/declarative_authorization/maintenance.rb +212 -0
- data/lib/declarative_authorization/obligation_scope.rb +354 -0
- data/lib/declarative_authorization/rails_legacy.rb +22 -0
- data/lib/declarative_authorization/railsengine.rb +6 -0
- data/lib/declarative_authorization/reader.rb +521 -0
- data/lib/tasks/authorization_tasks.rake +82 -0
- data/test/authorization_test.rb +1065 -0
- data/test/controller_filter_resource_access_test.rb +511 -0
- data/test/controller_test.rb +465 -0
- data/test/dsl_reader_test.rb +178 -0
- data/test/helper_test.rb +172 -0
- data/test/maintenance_test.rb +46 -0
- data/test/model_test.rb +2216 -0
- data/test/schema.sql +62 -0
- data/test/test_helper.rb +152 -0
- metadata +108 -0
@@ -0,0 +1,178 @@
|
|
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
|
+
if_attribute :test_attr_6 => lt { user.test_attr }
|
94
|
+
if_attribute :test_attr_6 => lte { user.test_attr }
|
95
|
+
if_attribute :test_attr_6 => gt { user.test_attr }
|
96
|
+
if_attribute :test_attr_6 => gte { user.test_attr }
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
|
101
|
+
assert_equal 1, reader.auth_rules_reader.roles.length
|
102
|
+
assert_equal 1, reader.auth_rules_reader.auth_rules.length
|
103
|
+
assert reader.auth_rules_reader.auth_rules[0].matches?(:test_role, [:test], :perms)
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_has_permission_to_with_context
|
107
|
+
reader = Authorization::Reader::DSLReader.new
|
108
|
+
reader.parse %|
|
109
|
+
authorization do
|
110
|
+
role :test_role do
|
111
|
+
has_permission_on :perms, :to => :test
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
|
115
|
+
assert_equal 1, reader.auth_rules_reader.roles.length
|
116
|
+
assert_equal 1, reader.auth_rules_reader.auth_rules.length
|
117
|
+
assert reader.auth_rules_reader.auth_rules[0].matches?(:test_role, [:test], :perms)
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_context
|
121
|
+
reader = Authorization::Reader::DSLReader.new
|
122
|
+
reader.parse %{
|
123
|
+
contexts do
|
124
|
+
context :high_level_context do
|
125
|
+
includes :low_level_context_1, :low_level_context_2
|
126
|
+
end
|
127
|
+
end
|
128
|
+
}
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_dsl_error
|
132
|
+
reader = Authorization::Reader::DSLReader.new
|
133
|
+
assert_raise(Authorization::Reader::DSLError) do
|
134
|
+
reader.parse %{
|
135
|
+
authorization do
|
136
|
+
includes :lesser_role
|
137
|
+
end
|
138
|
+
}
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_syntax_error
|
143
|
+
reader = Authorization::Reader::DSLReader.new
|
144
|
+
assert_raise(Authorization::Reader::DSLSyntaxError) do
|
145
|
+
reader.parse %{
|
146
|
+
authorizations do
|
147
|
+
end
|
148
|
+
}
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_syntax_error_2
|
153
|
+
reader = Authorization::Reader::DSLReader.new
|
154
|
+
assert_raise(Authorization::Reader::DSLSyntaxError) do
|
155
|
+
reader.parse %{
|
156
|
+
authorizations
|
157
|
+
end
|
158
|
+
}
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_factory_returns_self
|
163
|
+
reader = Authorization::Reader::DSLReader.new
|
164
|
+
assert_equal(Authorization::Reader::DSLReader.factory(reader).object_id, reader.object_id)
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_factory_loads_file
|
168
|
+
reader = Authorization::Reader::DSLReader.factory((DA_ROOT + "authorization_rules.dist.rb").to_s)
|
169
|
+
assert_equal(Authorization::Reader::DSLReader, reader.class)
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_load_file_not_found
|
173
|
+
assert_raise(Authorization::Reader::DSLFileNotFoundError) do
|
174
|
+
Authorization::Reader::DSLReader.load("nonexistent_file.rb")
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
data/test/helper_test.rb
ADDED
@@ -0,0 +1,172 @@
|
|
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
|
+
assert !has_role?(:test_role, :test_role2)
|
103
|
+
|
104
|
+
block_evaled = false
|
105
|
+
has_role?(:test_role) do
|
106
|
+
block_evaled = true
|
107
|
+
end
|
108
|
+
assert block_evaled
|
109
|
+
|
110
|
+
block_evaled = false
|
111
|
+
has_role?(:test_role2) do
|
112
|
+
block_evaled = true
|
113
|
+
end
|
114
|
+
assert !block_evaled
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_has_role_with_guest_user
|
118
|
+
reader = Authorization::Reader::DSLReader.new
|
119
|
+
reader.parse %{
|
120
|
+
authorization do
|
121
|
+
end
|
122
|
+
}
|
123
|
+
request!(nil, :action, reader)
|
124
|
+
|
125
|
+
assert !has_role?(:test_role)
|
126
|
+
|
127
|
+
block_evaled = false
|
128
|
+
has_role?(:test_role) do
|
129
|
+
block_evaled = true
|
130
|
+
end
|
131
|
+
assert !block_evaled
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_has_role_with_hierarchy
|
135
|
+
reader = Authorization::Reader::DSLReader.new
|
136
|
+
reader.parse %{
|
137
|
+
authorization do
|
138
|
+
role :test_role do
|
139
|
+
has_permission_on :mocks, :to => :show
|
140
|
+
end
|
141
|
+
role :other_role do
|
142
|
+
has_permission_on :another_mocks, :to => :show
|
143
|
+
end
|
144
|
+
|
145
|
+
role :root do
|
146
|
+
includes :test_role
|
147
|
+
end
|
148
|
+
end
|
149
|
+
}
|
150
|
+
|
151
|
+
user = MockUser.new(:root)
|
152
|
+
request!(user, :action, reader)
|
153
|
+
|
154
|
+
assert has_role_with_hierarchy?(:test_role)
|
155
|
+
assert !has_role_with_hierarchy?(:other_role)
|
156
|
+
|
157
|
+
block_evaled = false
|
158
|
+
has_role_with_hierarchy?(:test_role) do
|
159
|
+
block_evaled = true
|
160
|
+
end
|
161
|
+
assert block_evaled
|
162
|
+
|
163
|
+
block_evaled = false
|
164
|
+
has_role_with_hierarchy?(:test_role2) do
|
165
|
+
block_evaled = true
|
166
|
+
end
|
167
|
+
assert !block_evaled
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
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
|
data/test/model_test.rb
ADDED
@@ -0,0 +1,2216 @@
|
|
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_another_attrs, :class_name => "TestAttr", :foreign_key => :test_another_model_id
|
19
|
+
has_many :test_attr_throughs, :through => :test_attrs
|
20
|
+
has_many :test_attrs_with_attr, :class_name => "TestAttr", :conditions => {:attr => 1}
|
21
|
+
has_many :test_attr_throughs_with_attr, :through => :test_attrs,
|
22
|
+
:class_name => "TestAttrThrough", :source => :test_attr_throughs,
|
23
|
+
:conditions => "test_attrs.attr = 1"
|
24
|
+
has_one :test_attr_has_one, :class_name => "TestAttr"
|
25
|
+
has_one :test_attr_throughs_with_attr_and_has_one, :through => :test_attrs,
|
26
|
+
:class_name => "TestAttrThrough", :source => :test_attr_throughs,
|
27
|
+
:conditions => "test_attrs.attr = 1"
|
28
|
+
|
29
|
+
# TODO currently not working in Rails 3
|
30
|
+
if Rails.version < "3"
|
31
|
+
has_and_belongs_to_many :test_attr_throughs_habtm, :join_table => :test_attrs,
|
32
|
+
:class_name => "TestAttrThrough"
|
33
|
+
end
|
34
|
+
|
35
|
+
if Rails.version < "3"
|
36
|
+
named_scope :with_content, :conditions => "test_models.content IS NOT NULL"
|
37
|
+
else
|
38
|
+
scope :with_content, :conditions => "test_models.content IS NOT NULL"
|
39
|
+
end
|
40
|
+
|
41
|
+
# Primary key test
|
42
|
+
# :primary_key only available from Rails 2.2
|
43
|
+
unless Rails.version < "2.2"
|
44
|
+
has_many :test_attrs_with_primary_id, :class_name => "TestAttr",
|
45
|
+
:primary_key => :test_attr_through_id, :foreign_key => :test_attr_through_id
|
46
|
+
has_many :test_attr_throughs_with_primary_id,
|
47
|
+
:through => :test_attrs_with_primary_id, :class_name => "TestAttrThrough",
|
48
|
+
:source => :n_way_join_item
|
49
|
+
end
|
50
|
+
|
51
|
+
# for checking for unnecessary queries
|
52
|
+
mattr_accessor :query_count
|
53
|
+
def self.find(*args)
|
54
|
+
self.query_count ||= 0
|
55
|
+
self.query_count += 1
|
56
|
+
super(*args)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class NWayJoinItem < ActiveRecord::Base
|
61
|
+
has_many :test_attrs
|
62
|
+
has_many :others, :through => :test_attrs, :source => :n_way_join_item
|
63
|
+
end
|
64
|
+
|
65
|
+
class TestAttr < ActiveRecord::Base
|
66
|
+
belongs_to :test_model
|
67
|
+
belongs_to :test_another_model, :class_name => "TestModel", :foreign_key => :test_another_model_id
|
68
|
+
belongs_to :test_a_third_model, :class_name => "TestModel", :foreign_key => :test_a_third_model_id
|
69
|
+
belongs_to :n_way_join_item
|
70
|
+
belongs_to :test_attr
|
71
|
+
belongs_to :branch
|
72
|
+
belongs_to :company
|
73
|
+
has_many :test_attr_throughs
|
74
|
+
attr_reader :role_symbols
|
75
|
+
def initialize (*args)
|
76
|
+
@role_symbols = []
|
77
|
+
super(*args)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class TestAttrThrough < ActiveRecord::Base
|
82
|
+
belongs_to :test_attr
|
83
|
+
end
|
84
|
+
|
85
|
+
class TestModelSecurityModel < ActiveRecord::Base
|
86
|
+
has_many :test_attrs
|
87
|
+
using_access_control
|
88
|
+
end
|
89
|
+
|
90
|
+
class TestModelSecurityModelWithFind < ActiveRecord::Base
|
91
|
+
set_table_name "test_model_security_models"
|
92
|
+
has_many :test_attrs
|
93
|
+
using_access_control :include_read => true,
|
94
|
+
:context => :test_model_security_models
|
95
|
+
end
|
96
|
+
|
97
|
+
class TestModelSecurityModelWithIncludeAttributesNoRead < ActiveRecord::Base
|
98
|
+
set_table_name "test_model_security_model_with_include_attributes"
|
99
|
+
has_many :test_attrs
|
100
|
+
using_access_control :include_attributes => [
|
101
|
+
:protect_ar => [:attributes]
|
102
|
+
]
|
103
|
+
end
|
104
|
+
|
105
|
+
class TestModelSecurityModelWithIncludeAttributesDefault < ActiveRecord::Base
|
106
|
+
set_table_name "test_model_security_model_with_include_attributes"
|
107
|
+
has_many :test_attrs
|
108
|
+
using_access_control :include_read => true, :include_attributes => [
|
109
|
+
:protect_ar => [:attributes]
|
110
|
+
]
|
111
|
+
end
|
112
|
+
|
113
|
+
class TestModelSecurityModelWithIncludeAttributesWhiteList < ActiveRecord::Base
|
114
|
+
set_table_name "test_model_security_model_with_include_attributes"
|
115
|
+
has_many :test_attrs
|
116
|
+
using_access_control :include_read => true, :include_attributes => [
|
117
|
+
:read_all_privilege => :da_read_all,
|
118
|
+
:write_all_privilege => :da_write_all,
|
119
|
+
:protect_ar => [:attributes],
|
120
|
+
:whitelist => [:attr_1, :attr_1=, :attr_2, :attr_3=]
|
121
|
+
]
|
122
|
+
end
|
123
|
+
|
124
|
+
class TestModelSecurityModelWithIncludeAttributesApplicationDefaultAttributes < ActiveRecord::Base
|
125
|
+
set_table_name "test_model_security_model_with_include_attributes"
|
126
|
+
has_many :test_attrs
|
127
|
+
using_access_control :include_read => true, :include_attributes => [
|
128
|
+
:read_all_privilege => :da_read_all,
|
129
|
+
:write_all_privilege => :da_write_all,
|
130
|
+
:application_default_attributes => [:attr_4],
|
131
|
+
:protect_ar => [:attributes],
|
132
|
+
:whitelist => [:attr_1, :attr_1=]
|
133
|
+
]
|
134
|
+
end
|
135
|
+
|
136
|
+
class TestModelSecurityModelWithIncludeAttributesReadOverride < ActiveRecord::Base
|
137
|
+
set_table_name "test_model_security_model_with_include_attributes"
|
138
|
+
has_many :test_attrs
|
139
|
+
using_access_control :include_read => true, :include_attributes => [
|
140
|
+
:read_all_privilege => :da_read_all,
|
141
|
+
:protect_ar => [:attributes]
|
142
|
+
]
|
143
|
+
end
|
144
|
+
|
145
|
+
class TestModelSecurityModelWithIncludeAttributesWriteOverride < ActiveRecord::Base
|
146
|
+
set_table_name "test_model_security_model_with_include_attributes"
|
147
|
+
has_many :test_attrs
|
148
|
+
using_access_control :include_read => true, :include_attributes => [
|
149
|
+
:write_all_privilege => :da_write_all,
|
150
|
+
:protect_ar => [:attributes]
|
151
|
+
]
|
152
|
+
end
|
153
|
+
|
154
|
+
class Branch < ActiveRecord::Base
|
155
|
+
has_many :test_attrs
|
156
|
+
belongs_to :company
|
157
|
+
end
|
158
|
+
class Company < ActiveRecord::Base
|
159
|
+
has_many :test_attrs
|
160
|
+
has_many :branches
|
161
|
+
belongs_to :country
|
162
|
+
end
|
163
|
+
class SmallCompany < Company
|
164
|
+
def self.decl_auth_context
|
165
|
+
:companies
|
166
|
+
end
|
167
|
+
end
|
168
|
+
class Country < ActiveRecord::Base
|
169
|
+
has_many :test_models
|
170
|
+
has_many :companies
|
171
|
+
end
|
172
|
+
|
173
|
+
class NamedScopeModelTest < Test::Unit::TestCase
|
174
|
+
def test_multiple_deep_ored_belongs_to
|
175
|
+
reader = Authorization::Reader::DSLReader.new
|
176
|
+
reader.parse %{
|
177
|
+
authorization do
|
178
|
+
role :test_role do
|
179
|
+
has_permission_on :test_attrs, :to => :read do
|
180
|
+
if_attribute :test_model => {:test_attrs => contains {user}}
|
181
|
+
if_attribute :test_another_model => {:test_attrs => contains {user}}
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
}
|
186
|
+
Authorization::Engine.instance(reader)
|
187
|
+
|
188
|
+
test_model_1 = TestModel.create!
|
189
|
+
test_model_2 = TestModel.create!
|
190
|
+
test_attr_1 = TestAttr.create! :test_model_id => test_model_1.id,
|
191
|
+
:test_another_model_id => test_model_2.id
|
192
|
+
|
193
|
+
user = MockUser.new(:test_role, :id => test_attr_1)
|
194
|
+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
|
195
|
+
TestAttr.delete_all
|
196
|
+
TestModel.delete_all
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_with_belongs_to_and_has_many_with_contains
|
200
|
+
reader = Authorization::Reader::DSLReader.new
|
201
|
+
reader.parse %{
|
202
|
+
authorization do
|
203
|
+
role :test_role do
|
204
|
+
has_permission_on :test_attrs, :to => :read do
|
205
|
+
if_attribute :test_model => { :test_attrs => contains { user.test_attr_value } }
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
}
|
210
|
+
Authorization::Engine.instance(reader)
|
211
|
+
|
212
|
+
test_attr_1 = TestAttr.create!
|
213
|
+
test_model_1 = TestModel.create!
|
214
|
+
test_model_1.test_attrs.create!
|
215
|
+
|
216
|
+
user = MockUser.new(:test_role, :test_attr_value => test_model_1.test_attrs.first.id )
|
217
|
+
assert_equal 1, TestAttr.with_permissions_to( :read, :context => :test_attrs, :user => user ).length
|
218
|
+
assert_equal 1, TestAttr.with_permissions_to( :read, :user => user ).length
|
219
|
+
assert_raise Authorization::NotAuthorized do
|
220
|
+
TestAttr.with_permissions_to( :update_test_attrs, :user => user )
|
221
|
+
end
|
222
|
+
TestAttr.delete_all
|
223
|
+
TestModel.delete_all
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_with_nested_has_many
|
227
|
+
reader = Authorization::Reader::DSLReader.new
|
228
|
+
reader.parse %{
|
229
|
+
authorization do
|
230
|
+
role :test_role do
|
231
|
+
has_permission_on :companies, :to => :read do
|
232
|
+
if_attribute :branches => { :test_attrs => { :attr => is { user.test_attr_value } } }
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
}
|
237
|
+
Authorization::Engine.instance(reader)
|
238
|
+
|
239
|
+
allowed_company = Company.create!
|
240
|
+
allowed_company.branches.create!.test_attrs.create!(:attr => 1)
|
241
|
+
allowed_company.branches.create!.test_attrs.create!(:attr => 2)
|
242
|
+
|
243
|
+
prohibited_company = Company.create!
|
244
|
+
prohibited_company.branches.create!.test_attrs.create!(:attr => 3)
|
245
|
+
|
246
|
+
user = MockUser.new(:test_role, :test_attr_value => 1)
|
247
|
+
prohibited_user = MockUser.new(:test_role, :test_attr_value => 4)
|
248
|
+
assert_equal 1, Company.with_permissions_to(:read, :user => user).length
|
249
|
+
assert_equal 0, Company.with_permissions_to(:read, :user => prohibited_user).length
|
250
|
+
|
251
|
+
Company.delete_all
|
252
|
+
Branch.delete_all
|
253
|
+
TestAttr.delete_all
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_with_nested_has_many_through
|
257
|
+
reader = Authorization::Reader::DSLReader.new
|
258
|
+
reader.parse %{
|
259
|
+
authorization do
|
260
|
+
role :test_role do
|
261
|
+
has_permission_on :test_models, :to => :read do
|
262
|
+
if_attribute :test_attr_throughs => { :test_attr => { :attr => is { user.test_attr_value } } }
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
266
|
+
}
|
267
|
+
Authorization::Engine.instance(reader)
|
268
|
+
|
269
|
+
allowed_model = TestModel.create!
|
270
|
+
allowed_model.test_attrs.create!(:attr => 1).test_attr_throughs.create!
|
271
|
+
allowed_model.test_attrs.create!(:attr => 2).test_attr_throughs.create!
|
272
|
+
|
273
|
+
prohibited_model = TestModel.create!
|
274
|
+
prohibited_model.test_attrs.create!(:attr => 3).test_attr_throughs.create!
|
275
|
+
|
276
|
+
user = MockUser.new(:test_role, :test_attr_value => 1)
|
277
|
+
prohibited_user = MockUser.new(:test_role, :test_attr_value => 4)
|
278
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
279
|
+
assert_equal 0, TestModel.with_permissions_to(:read, :user => prohibited_user).length
|
280
|
+
|
281
|
+
TestModel.delete_all
|
282
|
+
TestAttrThrough.delete_all
|
283
|
+
TestAttr.delete_all
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_with_is
|
287
|
+
reader = Authorization::Reader::DSLReader.new
|
288
|
+
reader.parse %{
|
289
|
+
authorization do
|
290
|
+
role :test_role do
|
291
|
+
has_permission_on :test_models, :to => :read do
|
292
|
+
if_attribute :id => is { user.test_attr_value }
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
}
|
297
|
+
Authorization::Engine.instance(reader)
|
298
|
+
|
299
|
+
test_model_1 = TestModel.create!
|
300
|
+
TestModel.create!
|
301
|
+
|
302
|
+
user = MockUser.new(:test_role, :test_attr_value => test_model_1.id)
|
303
|
+
assert_equal 1, TestModel.with_permissions_to(:read,
|
304
|
+
:context => :test_models, :user => user).length
|
305
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
306
|
+
assert_raise Authorization::NotAuthorized do
|
307
|
+
TestModel.with_permissions_to(:update_test_models, :user => user)
|
308
|
+
end
|
309
|
+
TestModel.delete_all
|
310
|
+
end
|
311
|
+
|
312
|
+
def test_named_scope_on_proxy
|
313
|
+
reader = Authorization::Reader::DSLReader.new
|
314
|
+
reader.parse %{
|
315
|
+
authorization do
|
316
|
+
role :test_role do
|
317
|
+
has_permission_on :test_attrs, :to => :read do
|
318
|
+
if_attribute :id => is { user.test_attr_value }
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
}
|
323
|
+
Authorization::Engine.instance(reader)
|
324
|
+
|
325
|
+
test_model_1 = TestModel.create!
|
326
|
+
test_attr_1 = test_model_1.test_attrs.create!
|
327
|
+
test_model_1.test_attrs.create!
|
328
|
+
TestAttr.create!
|
329
|
+
|
330
|
+
user = MockUser.new(:test_role, :test_attr_value => test_attr_1.id)
|
331
|
+
assert_equal 1, test_model_1.test_attrs.with_permissions_to(:read, :user => user).length
|
332
|
+
TestModel.delete_all
|
333
|
+
TestAttr.delete_all
|
334
|
+
end
|
335
|
+
|
336
|
+
def test_named_scope_on_named_scope
|
337
|
+
reader = Authorization::Reader::DSLReader.new
|
338
|
+
reader.parse %{
|
339
|
+
authorization do
|
340
|
+
role :test_role do
|
341
|
+
has_permission_on :test_models, :to => :read do
|
342
|
+
if_attribute :test_attr_through_id => 1
|
343
|
+
end
|
344
|
+
has_permission_on :test_attrs, :to => :read do
|
345
|
+
if_permitted_to :read, :test_model
|
346
|
+
end
|
347
|
+
end
|
348
|
+
end
|
349
|
+
}
|
350
|
+
Authorization::Engine.instance(reader)
|
351
|
+
|
352
|
+
country = Country.create!
|
353
|
+
model_1 = TestModel.create!(:test_attr_through_id => 1, :content => "Content")
|
354
|
+
country.test_models << model_1
|
355
|
+
TestModel.create!(:test_attr_through_id => 1)
|
356
|
+
TestModel.create!(:test_attr_through_id => 2, :content => "Content")
|
357
|
+
|
358
|
+
user = MockUser.new(:test_role)
|
359
|
+
|
360
|
+
# TODO implement query_count for Rails 3
|
361
|
+
TestModel.query_count = 0
|
362
|
+
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
|
363
|
+
assert_equal 1, TestModel.query_count if Rails.version < "3"
|
364
|
+
|
365
|
+
TestModel.query_count = 0
|
366
|
+
assert_equal 1, TestModel.with_content.with_permissions_to(:read, :user => user).length
|
367
|
+
assert_equal 1, TestModel.query_count if Rails.version < "3"
|
368
|
+
|
369
|
+
TestModel.query_count = 0
|
370
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).with_content.length
|
371
|
+
assert_equal 1, TestModel.query_count if Rails.version < "3"
|
372
|
+
|
373
|
+
TestModel.query_count = 0
|
374
|
+
assert_equal 1, country.test_models.with_permissions_to(:read, :user => user).length
|
375
|
+
assert_equal 1, TestModel.query_count if Rails.version < "3"
|
376
|
+
|
377
|
+
TestModel.delete_all
|
378
|
+
Country.delete_all
|
379
|
+
end
|
380
|
+
|
381
|
+
def test_with_modified_context
|
382
|
+
reader = Authorization::Reader::DSLReader.new
|
383
|
+
reader.parse %{
|
384
|
+
authorization do
|
385
|
+
role :test_role do
|
386
|
+
has_permission_on :companies, :to => :read do
|
387
|
+
if_attribute :id => is { user.test_company_id }
|
388
|
+
end
|
389
|
+
end
|
390
|
+
end
|
391
|
+
}
|
392
|
+
Authorization::Engine.instance(reader)
|
393
|
+
|
394
|
+
test_company = SmallCompany.create!
|
395
|
+
|
396
|
+
user = MockUser.new(:test_role, :test_company_id => test_company.id)
|
397
|
+
assert_equal 1, SmallCompany.with_permissions_to(:read,
|
398
|
+
:user => user).length
|
399
|
+
SmallCompany.delete_all
|
400
|
+
end
|
401
|
+
|
402
|
+
def test_with_is_nil
|
403
|
+
reader = Authorization::Reader::DSLReader.new
|
404
|
+
reader.parse %{
|
405
|
+
authorization do
|
406
|
+
role :test_role do
|
407
|
+
has_permission_on :test_models, :to => :read do
|
408
|
+
if_attribute :content => nil
|
409
|
+
end
|
410
|
+
end
|
411
|
+
role :test_role_not_nil do
|
412
|
+
has_permission_on :test_models, :to => :read do
|
413
|
+
if_attribute :content => is_not { nil }
|
414
|
+
end
|
415
|
+
end
|
416
|
+
end
|
417
|
+
}
|
418
|
+
Authorization::Engine.instance(reader)
|
419
|
+
|
420
|
+
test_model_1 = TestModel.create!
|
421
|
+
test_model_2 = TestModel.create! :content => "Content"
|
422
|
+
|
423
|
+
assert_equal test_model_1, TestModel.with_permissions_to(:read,
|
424
|
+
:context => :test_models, :user => MockUser.new(:test_role)).first
|
425
|
+
assert_equal test_model_2, TestModel.with_permissions_to(:read,
|
426
|
+
:context => :test_models, :user => MockUser.new(:test_role_not_nil)).first
|
427
|
+
TestModel.delete_all
|
428
|
+
end
|
429
|
+
|
430
|
+
def test_with_not_is
|
431
|
+
reader = Authorization::Reader::DSLReader.new
|
432
|
+
reader.parse %{
|
433
|
+
authorization do
|
434
|
+
role :test_role do
|
435
|
+
has_permission_on :test_models, :to => :read do
|
436
|
+
if_attribute :id => is_not { user.test_attr_value }
|
437
|
+
end
|
438
|
+
end
|
439
|
+
end
|
440
|
+
}
|
441
|
+
Authorization::Engine.instance(reader)
|
442
|
+
|
443
|
+
test_model_1 = TestModel.create!
|
444
|
+
TestModel.create!
|
445
|
+
|
446
|
+
user = MockUser.new(:test_role, :test_attr_value => test_model_1.id)
|
447
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
448
|
+
TestModel.delete_all
|
449
|
+
end
|
450
|
+
|
451
|
+
def test_with_lt
|
452
|
+
reader = Authorization::Reader::DSLReader.new
|
453
|
+
reader.parse %{
|
454
|
+
authorization do
|
455
|
+
role :test_role do
|
456
|
+
has_permission_on :test_models, :to => :read do
|
457
|
+
if_attribute :id => lt { user.test_attr_value }
|
458
|
+
end
|
459
|
+
end
|
460
|
+
end
|
461
|
+
}
|
462
|
+
Authorization::Engine.instance(reader)
|
463
|
+
|
464
|
+
test_model_1 = TestModel.create!
|
465
|
+
TestModel.create!
|
466
|
+
|
467
|
+
user = MockUser.new(:test_role, :test_attr_value => test_model_1.id + 1)
|
468
|
+
assert_equal 1, TestModel.with_permissions_to(:read,
|
469
|
+
:context => :test_models, :user => user).length
|
470
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
471
|
+
assert_raise Authorization::NotAuthorized do
|
472
|
+
TestModel.with_permissions_to(:update_test_models, :user => user)
|
473
|
+
end
|
474
|
+
TestModel.delete_all
|
475
|
+
end
|
476
|
+
|
477
|
+
def test_with_lte
|
478
|
+
reader = Authorization::Reader::DSLReader.new
|
479
|
+
reader.parse %{
|
480
|
+
authorization do
|
481
|
+
role :test_role do
|
482
|
+
has_permission_on :test_models, :to => :read do
|
483
|
+
if_attribute :id => lte { user.test_attr_value }
|
484
|
+
end
|
485
|
+
end
|
486
|
+
end
|
487
|
+
}
|
488
|
+
Authorization::Engine.instance(reader)
|
489
|
+
|
490
|
+
test_model_1 = TestModel.create!
|
491
|
+
2.times { TestModel.create! }
|
492
|
+
|
493
|
+
user = MockUser.new(:test_role, :test_attr_value => test_model_1.id + 1)
|
494
|
+
assert_equal 2, TestModel.with_permissions_to(:read,
|
495
|
+
:context => :test_models, :user => user).length
|
496
|
+
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
|
497
|
+
assert_raise Authorization::NotAuthorized do
|
498
|
+
TestModel.with_permissions_to(:update_test_models, :user => user)
|
499
|
+
end
|
500
|
+
TestModel.delete_all
|
501
|
+
end
|
502
|
+
|
503
|
+
def test_with_gt
|
504
|
+
reader = Authorization::Reader::DSLReader.new
|
505
|
+
reader.parse %{
|
506
|
+
authorization do
|
507
|
+
role :test_role do
|
508
|
+
has_permission_on :test_models, :to => :read do
|
509
|
+
if_attribute :id => gt { user.test_attr_value }
|
510
|
+
end
|
511
|
+
end
|
512
|
+
end
|
513
|
+
}
|
514
|
+
Authorization::Engine.instance(reader)
|
515
|
+
|
516
|
+
TestModel.create!
|
517
|
+
test_model_1 = TestModel.create!
|
518
|
+
|
519
|
+
user = MockUser.new(:test_role, :test_attr_value => test_model_1.id - 1)
|
520
|
+
assert_equal 1, TestModel.with_permissions_to(:read,
|
521
|
+
:context => :test_models, :user => user).length
|
522
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
523
|
+
assert_raise Authorization::NotAuthorized do
|
524
|
+
TestModel.with_permissions_to(:update_test_models, :user => user)
|
525
|
+
end
|
526
|
+
TestModel.delete_all
|
527
|
+
end
|
528
|
+
|
529
|
+
def test_with_gte
|
530
|
+
reader = Authorization::Reader::DSLReader.new
|
531
|
+
reader.parse %{
|
532
|
+
authorization do
|
533
|
+
role :test_role do
|
534
|
+
has_permission_on :test_models, :to => :read do
|
535
|
+
if_attribute :id => gte { user.test_attr_value }
|
536
|
+
end
|
537
|
+
end
|
538
|
+
end
|
539
|
+
}
|
540
|
+
Authorization::Engine.instance(reader)
|
541
|
+
|
542
|
+
2.times { TestModel.create! }
|
543
|
+
test_model_1 = TestModel.create!
|
544
|
+
|
545
|
+
user = MockUser.new(:test_role, :test_attr_value => test_model_1.id - 1)
|
546
|
+
assert_equal 2, TestModel.with_permissions_to(:read,
|
547
|
+
:context => :test_models, :user => user).length
|
548
|
+
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
|
549
|
+
assert_raise Authorization::NotAuthorized do
|
550
|
+
TestModel.with_permissions_to(:update_test_models, :user => user)
|
551
|
+
end
|
552
|
+
TestModel.delete_all
|
553
|
+
end
|
554
|
+
|
555
|
+
def test_with_empty_obligations
|
556
|
+
reader = Authorization::Reader::DSLReader.new
|
557
|
+
reader.parse %{
|
558
|
+
authorization do
|
559
|
+
role :test_role do
|
560
|
+
has_permission_on :test_models, :to => :read
|
561
|
+
end
|
562
|
+
end
|
563
|
+
}
|
564
|
+
Authorization::Engine.instance(reader)
|
565
|
+
|
566
|
+
TestModel.create!
|
567
|
+
|
568
|
+
user = MockUser.new(:test_role)
|
569
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
570
|
+
assert_raise Authorization::NotAuthorized do
|
571
|
+
TestModel.with_permissions_to(:update, :user => user)
|
572
|
+
end
|
573
|
+
TestModel.delete_all
|
574
|
+
end
|
575
|
+
|
576
|
+
def test_multiple_obligations
|
577
|
+
reader = Authorization::Reader::DSLReader.new
|
578
|
+
reader.parse %{
|
579
|
+
authorization do
|
580
|
+
role :test_role do
|
581
|
+
has_permission_on :test_models, :to => :read do
|
582
|
+
if_attribute :id => is { user.test_attr_value }
|
583
|
+
end
|
584
|
+
has_permission_on :test_models, :to => :read do
|
585
|
+
if_attribute :id => is { user.test_attr_value_2 }
|
586
|
+
end
|
587
|
+
end
|
588
|
+
end
|
589
|
+
}
|
590
|
+
Authorization::Engine.instance(reader)
|
591
|
+
|
592
|
+
test_model_1 = TestModel.create!
|
593
|
+
test_model_2 = TestModel.create!
|
594
|
+
|
595
|
+
user = MockUser.new(:test_role, :test_attr_value => test_model_1.id,
|
596
|
+
:test_attr_value_2 => test_model_2.id)
|
597
|
+
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
|
598
|
+
TestModel.delete_all
|
599
|
+
end
|
600
|
+
|
601
|
+
def test_multiple_roles
|
602
|
+
reader = Authorization::Reader::DSLReader.new
|
603
|
+
reader.parse %{
|
604
|
+
authorization do
|
605
|
+
role :test_role do
|
606
|
+
has_permission_on :test_attrs, :to => :read do
|
607
|
+
if_attribute :attr => [1,2]
|
608
|
+
end
|
609
|
+
end
|
610
|
+
|
611
|
+
role :test_role_2 do
|
612
|
+
has_permission_on :test_attrs, :to => :read do
|
613
|
+
if_attribute :attr => [2,3]
|
614
|
+
end
|
615
|
+
end
|
616
|
+
end
|
617
|
+
}
|
618
|
+
Authorization::Engine.instance(reader)
|
619
|
+
|
620
|
+
TestAttr.create! :attr => 1
|
621
|
+
TestAttr.create! :attr => 2
|
622
|
+
TestAttr.create! :attr => 3
|
623
|
+
|
624
|
+
user = MockUser.new(:test_role)
|
625
|
+
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
|
626
|
+
TestAttr.delete_all
|
627
|
+
end
|
628
|
+
|
629
|
+
def test_multiple_and_empty_obligations
|
630
|
+
reader = Authorization::Reader::DSLReader.new
|
631
|
+
reader.parse %{
|
632
|
+
authorization do
|
633
|
+
role :test_role do
|
634
|
+
has_permission_on :test_models, :to => :read do
|
635
|
+
if_attribute :id => is { user.test_attr_value }
|
636
|
+
end
|
637
|
+
has_permission_on :test_models, :to => :read
|
638
|
+
end
|
639
|
+
end
|
640
|
+
}
|
641
|
+
Authorization::Engine.instance(reader)
|
642
|
+
|
643
|
+
test_model_1 = TestModel.create!
|
644
|
+
TestModel.create!
|
645
|
+
|
646
|
+
user = MockUser.new(:test_role, :test_attr_value => test_model_1.id)
|
647
|
+
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
|
648
|
+
TestModel.delete_all
|
649
|
+
end
|
650
|
+
|
651
|
+
def test_multiple_attributes
|
652
|
+
reader = Authorization::Reader::DSLReader.new
|
653
|
+
reader.parse %{
|
654
|
+
authorization do
|
655
|
+
role :test_role do
|
656
|
+
has_permission_on :test_models, :to => :read do
|
657
|
+
if_attribute :id => is { user.test_attr_value }, :content => "bla"
|
658
|
+
end
|
659
|
+
end
|
660
|
+
end
|
661
|
+
}
|
662
|
+
Authorization::Engine.instance(reader)
|
663
|
+
|
664
|
+
test_model_1 = TestModel.create! :content => 'bla'
|
665
|
+
TestModel.create! :content => 'bla'
|
666
|
+
TestModel.create!
|
667
|
+
|
668
|
+
user = MockUser.new(:test_role, :test_attr_value => test_model_1.id)
|
669
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
670
|
+
TestModel.delete_all
|
671
|
+
end
|
672
|
+
|
673
|
+
def test_multiple_belongs_to
|
674
|
+
reader = Authorization::Reader::DSLReader.new
|
675
|
+
reader.parse %{
|
676
|
+
authorization do
|
677
|
+
role :test_role do
|
678
|
+
has_permission_on :test_attrs, :to => :read do
|
679
|
+
if_attribute :test_model => is {user}
|
680
|
+
if_attribute :test_another_model => is {user}
|
681
|
+
end
|
682
|
+
end
|
683
|
+
end
|
684
|
+
}
|
685
|
+
Authorization::Engine.instance(reader)
|
686
|
+
|
687
|
+
test_attr_1 = TestAttr.create! :test_model_id => 1, :test_another_model_id => 2
|
688
|
+
|
689
|
+
user = MockUser.new(:test_role, :id => 1)
|
690
|
+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
|
691
|
+
TestAttr.delete_all
|
692
|
+
end
|
693
|
+
|
694
|
+
def test_with_is_and_priv_hierarchy
|
695
|
+
reader = Authorization::Reader::DSLReader.new
|
696
|
+
reader.parse %{
|
697
|
+
privileges do
|
698
|
+
privilege :read do
|
699
|
+
includes :list, :show
|
700
|
+
end
|
701
|
+
end
|
702
|
+
authorization do
|
703
|
+
role :test_role do
|
704
|
+
has_permission_on :test_models, :to => :read do
|
705
|
+
if_attribute :id => is { user.test_attr_value }
|
706
|
+
end
|
707
|
+
end
|
708
|
+
end
|
709
|
+
}
|
710
|
+
Authorization::Engine.instance(reader)
|
711
|
+
|
712
|
+
test_model_1 = TestModel.create!
|
713
|
+
TestModel.create!
|
714
|
+
|
715
|
+
user = MockUser.new(:test_role, :test_attr_value => test_model_1.id)
|
716
|
+
assert_equal 1, TestModel.with_permissions_to(:list,
|
717
|
+
:context => :test_models, :user => user).length
|
718
|
+
assert_equal 1, TestModel.with_permissions_to(:list, :user => user).length
|
719
|
+
|
720
|
+
TestModel.delete_all
|
721
|
+
end
|
722
|
+
|
723
|
+
def test_with_is_and_belongs_to
|
724
|
+
reader = Authorization::Reader::DSLReader.new
|
725
|
+
reader.parse %{
|
726
|
+
authorization do
|
727
|
+
role :test_role do
|
728
|
+
has_permission_on :test_attrs, :to => :read do
|
729
|
+
if_attribute :test_model => is { user.test_model }
|
730
|
+
end
|
731
|
+
end
|
732
|
+
end
|
733
|
+
}
|
734
|
+
Authorization::Engine.instance(reader)
|
735
|
+
|
736
|
+
test_model_1 = TestModel.create!
|
737
|
+
test_model_1.test_attrs.create!
|
738
|
+
TestModel.create!.test_attrs.create!
|
739
|
+
|
740
|
+
user = MockUser.new(:test_role, :test_model => test_model_1)
|
741
|
+
assert_equal 1, TestAttr.with_permissions_to(:read,
|
742
|
+
:context => :test_attrs, :user => user).length
|
743
|
+
|
744
|
+
TestModel.delete_all
|
745
|
+
TestAttr.delete_all
|
746
|
+
end
|
747
|
+
|
748
|
+
def test_with_deep_attribute
|
749
|
+
reader = Authorization::Reader::DSLReader.new
|
750
|
+
reader.parse %{
|
751
|
+
authorization do
|
752
|
+
role :test_role do
|
753
|
+
has_permission_on :test_attrs, :to => :read do
|
754
|
+
if_attribute :test_model => {:id => is { user.test_model_id } }
|
755
|
+
end
|
756
|
+
end
|
757
|
+
end
|
758
|
+
}
|
759
|
+
Authorization::Engine.instance(reader)
|
760
|
+
|
761
|
+
test_model_1 = TestModel.create!
|
762
|
+
test_model_1.test_attrs.create!
|
763
|
+
TestModel.create!.test_attrs.create!
|
764
|
+
|
765
|
+
user = MockUser.new(:test_role, :test_model_id => test_model_1.id)
|
766
|
+
assert_equal 1, TestAttr.with_permissions_to(:read,
|
767
|
+
:context => :test_attrs, :user => user).length
|
768
|
+
|
769
|
+
TestModel.delete_all
|
770
|
+
TestAttr.delete_all
|
771
|
+
end
|
772
|
+
|
773
|
+
def test_with_anded_rules
|
774
|
+
reader = Authorization::Reader::DSLReader.new
|
775
|
+
reader.parse %{
|
776
|
+
authorization do
|
777
|
+
role :test_role do
|
778
|
+
has_permission_on :test_attrs, :to => :read, :join_by => :and do
|
779
|
+
if_attribute :test_model => is { user.test_model }
|
780
|
+
if_attribute :attr => 1
|
781
|
+
end
|
782
|
+
end
|
783
|
+
end
|
784
|
+
}
|
785
|
+
Authorization::Engine.instance(reader)
|
786
|
+
|
787
|
+
test_model_1 = TestModel.create!
|
788
|
+
test_model_1.test_attrs.create!(:attr => 1)
|
789
|
+
TestModel.create!.test_attrs.create!(:attr => 1)
|
790
|
+
TestModel.create!.test_attrs.create!
|
791
|
+
|
792
|
+
user = MockUser.new(:test_role, :test_model => test_model_1)
|
793
|
+
assert_equal 1, TestAttr.with_permissions_to(:read,
|
794
|
+
:context => :test_attrs, :user => user).length
|
795
|
+
|
796
|
+
TestModel.delete_all
|
797
|
+
TestAttr.delete_all
|
798
|
+
end
|
799
|
+
|
800
|
+
def test_with_contains
|
801
|
+
reader = Authorization::Reader::DSLReader.new
|
802
|
+
reader.parse %{
|
803
|
+
authorization do
|
804
|
+
role :test_role do
|
805
|
+
has_permission_on :test_models, :to => :read do
|
806
|
+
if_attribute :test_attrs => contains { user }
|
807
|
+
end
|
808
|
+
end
|
809
|
+
end
|
810
|
+
}
|
811
|
+
Authorization::Engine.instance(reader)
|
812
|
+
|
813
|
+
test_model_1 = TestModel.create!
|
814
|
+
test_model_2 = TestModel.create!
|
815
|
+
test_model_1.test_attrs.create!
|
816
|
+
test_model_1.test_attrs.create!
|
817
|
+
test_model_2.test_attrs.create!
|
818
|
+
|
819
|
+
user = MockUser.new(:test_role,
|
820
|
+
:id => test_model_1.test_attrs.first.id)
|
821
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
822
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).find(:all, :conditions => {:id => test_model_1.id}).length
|
823
|
+
|
824
|
+
TestModel.delete_all
|
825
|
+
TestAttr.delete_all
|
826
|
+
end
|
827
|
+
|
828
|
+
def test_with_does_not_contain
|
829
|
+
reader = Authorization::Reader::DSLReader.new
|
830
|
+
reader.parse %{
|
831
|
+
authorization do
|
832
|
+
role :test_role do
|
833
|
+
has_permission_on :test_models, :to => :read do
|
834
|
+
if_attribute :test_attrs => does_not_contain { user }
|
835
|
+
end
|
836
|
+
end
|
837
|
+
end
|
838
|
+
}
|
839
|
+
Authorization::Engine.instance(reader)
|
840
|
+
|
841
|
+
test_model_1 = TestModel.create!
|
842
|
+
test_model_2 = TestModel.create!
|
843
|
+
test_model_1.test_attrs.create!
|
844
|
+
test_model_2.test_attrs.create!
|
845
|
+
|
846
|
+
user = MockUser.new(:test_role,
|
847
|
+
:id => test_model_1.test_attrs.first.id)
|
848
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
849
|
+
|
850
|
+
TestModel.delete_all
|
851
|
+
TestAttr.delete_all
|
852
|
+
end
|
853
|
+
|
854
|
+
def test_with_contains_conditions
|
855
|
+
reader = Authorization::Reader::DSLReader.new
|
856
|
+
reader.parse %{
|
857
|
+
authorization do
|
858
|
+
role :test_role do
|
859
|
+
has_permission_on :test_models, :to => :read do
|
860
|
+
if_attribute :test_attrs_with_attr => contains { user }
|
861
|
+
end
|
862
|
+
end
|
863
|
+
end
|
864
|
+
}
|
865
|
+
Authorization::Engine.instance(reader)
|
866
|
+
|
867
|
+
test_model_1 = TestModel.create!
|
868
|
+
test_model_2 = TestModel.create!
|
869
|
+
test_model_1.test_attrs_with_attr.create!
|
870
|
+
test_model_1.test_attrs.create!(:attr => 2)
|
871
|
+
test_model_2.test_attrs_with_attr.create!
|
872
|
+
test_model_2.test_attrs.create!(:attr => 2)
|
873
|
+
|
874
|
+
#assert_equal 1, test_model_1.test_attrs_with_attr.length
|
875
|
+
user = MockUser.new(:test_role,
|
876
|
+
:id => test_model_1.test_attrs.first.id)
|
877
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
878
|
+
user = MockUser.new(:test_role,
|
879
|
+
:id => test_model_1.test_attrs.last.id)
|
880
|
+
assert_equal 0, TestModel.with_permissions_to(:read, :user => user).length
|
881
|
+
|
882
|
+
TestModel.delete_all
|
883
|
+
TestAttr.delete_all
|
884
|
+
end
|
885
|
+
|
886
|
+
# TODO fails in Rails 3 because TestModel.scoped.joins(:test_attr_throughs_with_attr)
|
887
|
+
# does not work
|
888
|
+
if Rails.version < "3"
|
889
|
+
def test_with_contains_through_conditions
|
890
|
+
reader = Authorization::Reader::DSLReader.new
|
891
|
+
reader.parse %{
|
892
|
+
authorization do
|
893
|
+
role :test_role do
|
894
|
+
has_permission_on :test_models, :to => :read do
|
895
|
+
if_attribute :test_attr_throughs_with_attr => contains { user }
|
896
|
+
end
|
897
|
+
end
|
898
|
+
end
|
899
|
+
}
|
900
|
+
Authorization::Engine.instance(reader)
|
901
|
+
|
902
|
+
test_model_1 = TestModel.create!
|
903
|
+
test_model_2 = TestModel.create!
|
904
|
+
test_model_1.test_attrs.create!(:attr => 1).test_attr_throughs.create!
|
905
|
+
test_model_1.test_attrs.create!(:attr => 2).test_attr_throughs.create!
|
906
|
+
test_model_2.test_attrs.create!(:attr => 1).test_attr_throughs.create!
|
907
|
+
test_model_2.test_attrs.create!(:attr => 2).test_attr_throughs.create!
|
908
|
+
|
909
|
+
#assert_equal 1, test_model_1.test_attrs_with_attr.length
|
910
|
+
user = MockUser.new(:test_role,
|
911
|
+
:id => test_model_1.test_attr_throughs.first.id)
|
912
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
913
|
+
user = MockUser.new(:test_role,
|
914
|
+
:id => test_model_1.test_attr_throughs.last.id)
|
915
|
+
assert_equal 0, TestModel.with_permissions_to(:read, :user => user).length
|
916
|
+
|
917
|
+
TestModel.delete_all
|
918
|
+
TestAttrThrough.delete_all
|
919
|
+
TestAttr.delete_all
|
920
|
+
end
|
921
|
+
end
|
922
|
+
|
923
|
+
if Rails.version < "3"
|
924
|
+
def test_with_contains_habtm
|
925
|
+
reader = Authorization::Reader::DSLReader.new
|
926
|
+
reader.parse %{
|
927
|
+
authorization do
|
928
|
+
role :test_role do
|
929
|
+
has_permission_on :test_models, :to => :read do
|
930
|
+
if_attribute :test_attr_throughs_habtm => contains { user.test_attr_through_id }
|
931
|
+
end
|
932
|
+
end
|
933
|
+
end
|
934
|
+
}
|
935
|
+
Authorization::Engine.instance(reader)
|
936
|
+
|
937
|
+
# TODO habtm currently not working in Rails 3
|
938
|
+
test_model_1 = TestModel.create!
|
939
|
+
test_model_2 = TestModel.create!
|
940
|
+
test_attr_through_1 = TestAttrThrough.create!
|
941
|
+
test_attr_through_2 = TestAttrThrough.create!
|
942
|
+
TestAttr.create! :test_model_id => test_model_1.id, :test_attr_through_id => test_attr_through_1.id
|
943
|
+
TestAttr.create! :test_model_id => test_model_2.id, :test_attr_through_id => test_attr_through_2.id
|
944
|
+
|
945
|
+
user = MockUser.new(:test_role,
|
946
|
+
:test_attr_through_id => test_model_1.test_attr_throughs_habtm.first.id)
|
947
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
948
|
+
assert_equal test_model_1, TestModel.with_permissions_to(:read, :user => user)[0]
|
949
|
+
|
950
|
+
TestModel.delete_all
|
951
|
+
TestAttrThrough.delete_all
|
952
|
+
TestAttr.delete_all
|
953
|
+
end
|
954
|
+
end
|
955
|
+
|
956
|
+
# :primary_key not available in Rails prior to 2.2
|
957
|
+
if Rails.version > "2.2"
|
958
|
+
def test_with_contains_through_primary_key
|
959
|
+
reader = Authorization::Reader::DSLReader.new
|
960
|
+
reader.parse %{
|
961
|
+
authorization do
|
962
|
+
role :test_role do
|
963
|
+
has_permission_on :test_models, :to => :read do
|
964
|
+
if_attribute :test_attr_throughs_with_primary_id => contains { user }
|
965
|
+
end
|
966
|
+
end
|
967
|
+
end
|
968
|
+
}
|
969
|
+
Authorization::Engine.instance(reader)
|
970
|
+
|
971
|
+
test_attr_through_1 = TestAttrThrough.create!
|
972
|
+
test_item = NWayJoinItem.create!
|
973
|
+
test_model_1 = TestModel.create!(:test_attr_through_id => test_attr_through_1.id)
|
974
|
+
test_attr_1 = TestAttr.create!(:test_attr_through_id => test_attr_through_1.id,
|
975
|
+
:n_way_join_item_id => test_item.id)
|
976
|
+
|
977
|
+
user = MockUser.new(:test_role,
|
978
|
+
:id => test_attr_through_1.id)
|
979
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
980
|
+
|
981
|
+
TestModel.delete_all
|
982
|
+
TestAttrThrough.delete_all
|
983
|
+
TestAttr.delete_all
|
984
|
+
end
|
985
|
+
end
|
986
|
+
|
987
|
+
def test_with_intersects_with
|
988
|
+
reader = Authorization::Reader::DSLReader.new
|
989
|
+
reader.parse %{
|
990
|
+
authorization do
|
991
|
+
role :test_role do
|
992
|
+
has_permission_on :test_models, :to => :read do
|
993
|
+
if_attribute :test_attrs => intersects_with { user.test_attrs }
|
994
|
+
end
|
995
|
+
end
|
996
|
+
end
|
997
|
+
}
|
998
|
+
Authorization::Engine.instance(reader)
|
999
|
+
|
1000
|
+
test_model_1 = TestModel.create!
|
1001
|
+
test_model_2 = TestModel.create!
|
1002
|
+
test_model_1.test_attrs.create!
|
1003
|
+
test_model_1.test_attrs.create!
|
1004
|
+
test_model_1.test_attrs.create!
|
1005
|
+
test_model_2.test_attrs.create!
|
1006
|
+
|
1007
|
+
user = MockUser.new(:test_role,
|
1008
|
+
:test_attrs => [test_model_1.test_attrs.first, TestAttr.create!])
|
1009
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
1010
|
+
|
1011
|
+
user = MockUser.new(:test_role,
|
1012
|
+
:test_attrs => [TestAttr.create!])
|
1013
|
+
assert_equal 0, TestModel.with_permissions_to(:read, :user => user).length
|
1014
|
+
|
1015
|
+
TestModel.delete_all
|
1016
|
+
TestAttr.delete_all
|
1017
|
+
end
|
1018
|
+
|
1019
|
+
def test_with_is_and_has_one
|
1020
|
+
reader = Authorization::Reader::DSLReader.new
|
1021
|
+
reader.parse %{
|
1022
|
+
authorization do :test_attr_has_one
|
1023
|
+
role :test_role do
|
1024
|
+
has_permission_on :test_models, :to => :read do
|
1025
|
+
if_attribute :test_attr_has_one => is { user.test_attr }
|
1026
|
+
end
|
1027
|
+
end
|
1028
|
+
end
|
1029
|
+
}
|
1030
|
+
Authorization::Engine.instance(reader)
|
1031
|
+
|
1032
|
+
test_model_1 = TestModel.create!
|
1033
|
+
test_attr_1 = test_model_1.test_attrs.create!
|
1034
|
+
TestModel.create!.test_attrs.create!
|
1035
|
+
|
1036
|
+
user = MockUser.new(:test_role, :test_attr => test_attr_1)
|
1037
|
+
assert_equal 1, TestModel.with_permissions_to(:read,
|
1038
|
+
:context => :test_models, :user => user).length
|
1039
|
+
|
1040
|
+
TestModel.delete_all
|
1041
|
+
TestAttr.delete_all
|
1042
|
+
end
|
1043
|
+
|
1044
|
+
# TODO fails in Rails 3 because TestModel.scoped.joins(:test_attr_throughs_with_attr)
|
1045
|
+
# does not work
|
1046
|
+
if Rails.version < "3"
|
1047
|
+
def test_with_is_and_has_one_through_conditions
|
1048
|
+
reader = Authorization::Reader::DSLReader.new
|
1049
|
+
reader.parse %{
|
1050
|
+
authorization do
|
1051
|
+
role :test_role do
|
1052
|
+
has_permission_on :test_models, :to => :read do
|
1053
|
+
if_attribute :test_attr_throughs_with_attr_and_has_one => is { user }
|
1054
|
+
end
|
1055
|
+
end
|
1056
|
+
end
|
1057
|
+
}
|
1058
|
+
Authorization::Engine.instance(reader)
|
1059
|
+
|
1060
|
+
test_model_1 = TestModel.create!
|
1061
|
+
test_model_2 = TestModel.create!
|
1062
|
+
test_model_1.test_attrs.create!(:attr => 1).test_attr_throughs.create!
|
1063
|
+
test_model_1.test_attrs.create!(:attr => 2).test_attr_throughs.create!
|
1064
|
+
test_model_2.test_attrs.create!(:attr => 1).test_attr_throughs.create!
|
1065
|
+
test_model_2.test_attrs.create!(:attr => 2).test_attr_throughs.create!
|
1066
|
+
|
1067
|
+
#assert_equal 1, test_model_1.test_attrs_with_attr.length
|
1068
|
+
user = MockUser.new(:test_role,
|
1069
|
+
:id => test_model_1.test_attr_throughs.first.id)
|
1070
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
1071
|
+
user = MockUser.new(:test_role,
|
1072
|
+
:id => test_model_1.test_attr_throughs.last.id)
|
1073
|
+
assert_equal 0, TestModel.with_permissions_to(:read, :user => user).length
|
1074
|
+
|
1075
|
+
TestModel.delete_all
|
1076
|
+
TestAttr.delete_all
|
1077
|
+
TestAttrThrough.delete_all
|
1078
|
+
end
|
1079
|
+
end
|
1080
|
+
|
1081
|
+
def test_with_is_in
|
1082
|
+
reader = Authorization::Reader::DSLReader.new
|
1083
|
+
reader.parse %{
|
1084
|
+
authorization do
|
1085
|
+
role :test_role do
|
1086
|
+
has_permission_on :test_attrs, :to => :read do
|
1087
|
+
if_attribute :test_model => is_in { [user.test_model, user.test_model_2] }
|
1088
|
+
end
|
1089
|
+
end
|
1090
|
+
end
|
1091
|
+
}
|
1092
|
+
Authorization::Engine.instance(reader)
|
1093
|
+
|
1094
|
+
test_model_1 = TestModel.create!
|
1095
|
+
test_model_2 = TestModel.create!
|
1096
|
+
test_model_1.test_attrs.create!
|
1097
|
+
TestModel.create!.test_attrs.create!
|
1098
|
+
|
1099
|
+
user = MockUser.new(:test_role, :test_model => test_model_1,
|
1100
|
+
:test_model_2 => test_model_2)
|
1101
|
+
assert_equal 1, TestAttr.with_permissions_to(:read,
|
1102
|
+
:context => :test_attrs, :user => user).length
|
1103
|
+
|
1104
|
+
TestModel.delete_all
|
1105
|
+
TestAttr.delete_all
|
1106
|
+
end
|
1107
|
+
|
1108
|
+
def test_with_not_is_in
|
1109
|
+
reader = Authorization::Reader::DSLReader.new
|
1110
|
+
reader.parse %{
|
1111
|
+
authorization do
|
1112
|
+
role :test_role do
|
1113
|
+
has_permission_on :test_attrs, :to => :read do
|
1114
|
+
if_attribute :test_model => is_not_in { [user.test_model, user.test_model_2] }
|
1115
|
+
end
|
1116
|
+
end
|
1117
|
+
end
|
1118
|
+
}
|
1119
|
+
Authorization::Engine.instance(reader)
|
1120
|
+
|
1121
|
+
test_model_1 = TestModel.create!
|
1122
|
+
test_model_2 = TestModel.create!
|
1123
|
+
test_model_1.test_attrs.create!
|
1124
|
+
TestModel.create!.test_attrs.create!
|
1125
|
+
|
1126
|
+
user = MockUser.new(:test_role, :test_model => test_model_1,
|
1127
|
+
:test_model_2 => test_model_2)
|
1128
|
+
assert_equal 1, TestAttr.with_permissions_to(:read,
|
1129
|
+
:context => :test_attrs, :user => user).length
|
1130
|
+
|
1131
|
+
TestModel.delete_all
|
1132
|
+
TestAttr.delete_all
|
1133
|
+
end
|
1134
|
+
|
1135
|
+
def test_with_if_permitted_to
|
1136
|
+
reader = Authorization::Reader::DSLReader.new
|
1137
|
+
reader.parse %{
|
1138
|
+
authorization do
|
1139
|
+
role :test_role do
|
1140
|
+
has_permission_on :test_models, :to => :read do
|
1141
|
+
if_attribute :test_attrs => contains { user }
|
1142
|
+
end
|
1143
|
+
has_permission_on :test_attrs, :to => :read do
|
1144
|
+
if_permitted_to :read, :test_model
|
1145
|
+
end
|
1146
|
+
end
|
1147
|
+
end
|
1148
|
+
}
|
1149
|
+
Authorization::Engine.instance(reader)
|
1150
|
+
|
1151
|
+
test_model_1 = TestModel.create!
|
1152
|
+
test_attr_1 = test_model_1.test_attrs.create!
|
1153
|
+
|
1154
|
+
user = MockUser.new(:test_role, :id => test_attr_1.id)
|
1155
|
+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
|
1156
|
+
TestModel.delete_all
|
1157
|
+
TestAttr.delete_all
|
1158
|
+
end
|
1159
|
+
|
1160
|
+
def test_with_if_permitted_to_with_no_child_permissions
|
1161
|
+
reader = Authorization::Reader::DSLReader.new
|
1162
|
+
reader.parse %{
|
1163
|
+
authorization do
|
1164
|
+
role :another_role do
|
1165
|
+
has_permission_on :test_models, :to => :read do
|
1166
|
+
if_attribute :test_attrs => contains { user }
|
1167
|
+
end
|
1168
|
+
end
|
1169
|
+
role :additional_if_attribute do
|
1170
|
+
has_permission_on :test_attrs, :to => :read do
|
1171
|
+
if_permitted_to :read, :test_model
|
1172
|
+
if_attribute :test_model => {:test_attrs => contains { user }}
|
1173
|
+
end
|
1174
|
+
end
|
1175
|
+
role :only_permitted_to do
|
1176
|
+
has_permission_on :test_attrs, :to => :read do
|
1177
|
+
if_permitted_to :read, :test_model
|
1178
|
+
end
|
1179
|
+
end
|
1180
|
+
end
|
1181
|
+
}
|
1182
|
+
Authorization::Engine.instance(reader)
|
1183
|
+
|
1184
|
+
test_model_1 = TestModel.create!
|
1185
|
+
test_attr_1 = test_model_1.test_attrs.create!
|
1186
|
+
|
1187
|
+
user = MockUser.new(:only_permitted_to, :another_role, :id => test_attr_1.id)
|
1188
|
+
also_allowed_user = MockUser.new(:additional_if_attribute, :id => test_attr_1.id)
|
1189
|
+
non_allowed_user = MockUser.new(:only_permitted_to, :id => test_attr_1.id)
|
1190
|
+
|
1191
|
+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
|
1192
|
+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => also_allowed_user).length
|
1193
|
+
assert_raise Authorization::NotAuthorized do
|
1194
|
+
TestAttr.with_permissions_to(:read, :user => non_allowed_user).find(:all)
|
1195
|
+
end
|
1196
|
+
|
1197
|
+
TestModel.delete_all
|
1198
|
+
TestAttr.delete_all
|
1199
|
+
end
|
1200
|
+
|
1201
|
+
def test_with_if_permitted_to_with_context_from_model
|
1202
|
+
reader = Authorization::Reader::DSLReader.new
|
1203
|
+
reader.parse %{
|
1204
|
+
authorization do
|
1205
|
+
role :test_role do
|
1206
|
+
has_permission_on :test_models, :to => :read do
|
1207
|
+
if_attribute :test_another_attrs => contains { user }
|
1208
|
+
end
|
1209
|
+
has_permission_on :test_attrs, :to => :read do
|
1210
|
+
if_permitted_to :read, :test_another_model
|
1211
|
+
end
|
1212
|
+
end
|
1213
|
+
end
|
1214
|
+
}
|
1215
|
+
Authorization::Engine.instance(reader)
|
1216
|
+
|
1217
|
+
test_model_1 = TestModel.create!
|
1218
|
+
test_attr_1 = test_model_1.test_another_attrs.create!
|
1219
|
+
|
1220
|
+
user = MockUser.new(:test_role, :id => test_attr_1.id)
|
1221
|
+
non_allowed_user = MockUser.new(:test_role, :id => 111)
|
1222
|
+
|
1223
|
+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
|
1224
|
+
assert_equal 0, TestAttr.with_permissions_to(:read, :user => non_allowed_user).length
|
1225
|
+
TestModel.delete_all
|
1226
|
+
TestAttr.delete_all
|
1227
|
+
end
|
1228
|
+
|
1229
|
+
def test_with_has_many_if_permitted_to
|
1230
|
+
reader = Authorization::Reader::DSLReader.new
|
1231
|
+
reader.parse %{
|
1232
|
+
authorization do
|
1233
|
+
role :test_role do
|
1234
|
+
has_permission_on :test_models, :to => :read do
|
1235
|
+
if_permitted_to :read, :test_attrs
|
1236
|
+
end
|
1237
|
+
has_permission_on :test_attrs, :to => :read do
|
1238
|
+
if_attribute :attr => is { user.id }
|
1239
|
+
end
|
1240
|
+
end
|
1241
|
+
end
|
1242
|
+
}
|
1243
|
+
Authorization::Engine.instance(reader)
|
1244
|
+
|
1245
|
+
test_model_1 = TestModel.create!
|
1246
|
+
test_attr_1 = test_model_1.test_attrs.create!(:attr => 111)
|
1247
|
+
|
1248
|
+
user = MockUser.new(:test_role, :id => test_attr_1.attr)
|
1249
|
+
non_allowed_user = MockUser.new(:test_role, :id => 333)
|
1250
|
+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
|
1251
|
+
assert_equal 0, TestModel.with_permissions_to(:read, :user => non_allowed_user).length
|
1252
|
+
TestModel.delete_all
|
1253
|
+
TestAttr.delete_all
|
1254
|
+
end
|
1255
|
+
|
1256
|
+
def test_with_deep_has_many_if_permitted_to
|
1257
|
+
reader = Authorization::Reader::DSLReader.new
|
1258
|
+
reader.parse %{
|
1259
|
+
authorization do
|
1260
|
+
role :test_role do
|
1261
|
+
has_permission_on :branches, :to => :read do
|
1262
|
+
if_attribute :name => "A Branch"
|
1263
|
+
end
|
1264
|
+
has_permission_on :companies, :to => :read do
|
1265
|
+
if_permitted_to :read, :test_attrs => :branch
|
1266
|
+
end
|
1267
|
+
end
|
1268
|
+
end
|
1269
|
+
}
|
1270
|
+
Authorization::Engine.instance(reader)
|
1271
|
+
|
1272
|
+
readable_company = Company.create!
|
1273
|
+
readable_company.test_attrs.create!(:branch => Branch.create!(:name => "A Branch"))
|
1274
|
+
|
1275
|
+
forbidden_company = Company.create!
|
1276
|
+
forbidden_company.test_attrs.create!(:branch => Branch.create!(:name => "Different Branch"))
|
1277
|
+
|
1278
|
+
user = MockUser.new(:test_role)
|
1279
|
+
assert_equal 1, Company.with_permissions_to(:read, :user => user).length
|
1280
|
+
Company.delete_all
|
1281
|
+
Branch.delete_all
|
1282
|
+
TestAttr.delete_all
|
1283
|
+
end
|
1284
|
+
|
1285
|
+
def test_with_if_permitted_to_and_empty_obligations
|
1286
|
+
reader = Authorization::Reader::DSLReader.new
|
1287
|
+
reader.parse %{
|
1288
|
+
authorization do
|
1289
|
+
role :test_role do
|
1290
|
+
has_permission_on :test_models, :to => :read
|
1291
|
+
has_permission_on :test_attrs, :to => :read do
|
1292
|
+
if_permitted_to :read, :test_model
|
1293
|
+
end
|
1294
|
+
end
|
1295
|
+
end
|
1296
|
+
}
|
1297
|
+
Authorization::Engine.instance(reader)
|
1298
|
+
|
1299
|
+
test_model_1 = TestModel.create!
|
1300
|
+
test_attr_1 = test_model_1.test_attrs.create!
|
1301
|
+
|
1302
|
+
user = MockUser.new(:test_role)
|
1303
|
+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
|
1304
|
+
TestModel.delete_all
|
1305
|
+
TestAttr.delete_all
|
1306
|
+
end
|
1307
|
+
|
1308
|
+
def test_with_if_permitted_to_nil
|
1309
|
+
reader = Authorization::Reader::DSLReader.new
|
1310
|
+
reader.parse %{
|
1311
|
+
authorization do
|
1312
|
+
role :test_role do
|
1313
|
+
has_permission_on :test_models, :to => :read do
|
1314
|
+
if_attribute :test_attrs => contains { user }
|
1315
|
+
end
|
1316
|
+
has_permission_on :test_attrs, :to => :read do
|
1317
|
+
if_permitted_to :read, :test_model
|
1318
|
+
end
|
1319
|
+
end
|
1320
|
+
end
|
1321
|
+
}
|
1322
|
+
Authorization::Engine.instance(reader)
|
1323
|
+
|
1324
|
+
test_attr_1 = TestAttr.create!
|
1325
|
+
|
1326
|
+
user = MockUser.new(:test_role, :id => test_attr_1.id)
|
1327
|
+
assert_equal 0, TestAttr.with_permissions_to(:read, :user => user).length
|
1328
|
+
TestAttr.delete_all
|
1329
|
+
end
|
1330
|
+
|
1331
|
+
def test_with_if_permitted_to_self
|
1332
|
+
reader = Authorization::Reader::DSLReader.new
|
1333
|
+
reader.parse %{
|
1334
|
+
authorization do
|
1335
|
+
role :test_role do
|
1336
|
+
has_permission_on :test_models, :to => :read do
|
1337
|
+
if_attribute :test_attrs => contains { user }
|
1338
|
+
end
|
1339
|
+
has_permission_on :test_models, :to => :update do
|
1340
|
+
if_permitted_to :read
|
1341
|
+
end
|
1342
|
+
end
|
1343
|
+
end
|
1344
|
+
}
|
1345
|
+
Authorization::Engine.instance(reader)
|
1346
|
+
|
1347
|
+
test_model_1 = TestModel.create!
|
1348
|
+
test_attr_1 = test_model_1.test_attrs.create!
|
1349
|
+
test_attr_2 = TestAttr.create!
|
1350
|
+
|
1351
|
+
user = MockUser.new(:test_role, :id => test_attr_1.id)
|
1352
|
+
assert_equal 1, TestModel.with_permissions_to(:update, :user => user).length
|
1353
|
+
TestAttr.delete_all
|
1354
|
+
TestModel.delete_all
|
1355
|
+
end
|
1356
|
+
|
1357
|
+
def test_with_has_many_and_reoccuring_tables
|
1358
|
+
reader = Authorization::Reader::DSLReader.new
|
1359
|
+
reader.parse %{
|
1360
|
+
authorization do
|
1361
|
+
role :test_role do
|
1362
|
+
has_permission_on :test_attrs, :to => :read do
|
1363
|
+
if_attribute :test_another_model => { :content => 'test_1_2' },
|
1364
|
+
:test_model => { :content => 'test_1_1' }
|
1365
|
+
end
|
1366
|
+
end
|
1367
|
+
end
|
1368
|
+
}
|
1369
|
+
Authorization::Engine.instance(reader)
|
1370
|
+
|
1371
|
+
test_attr_1 = TestAttr.create!(
|
1372
|
+
:test_model => TestModel.create!(:content => 'test_1_1'),
|
1373
|
+
:test_another_model => TestModel.create!(:content => 'test_1_2')
|
1374
|
+
)
|
1375
|
+
test_attr_2 = TestAttr.create!(
|
1376
|
+
:test_model => TestModel.create!(:content => 'test_2_1'),
|
1377
|
+
:test_another_model => TestModel.create!(:content => 'test_2_2')
|
1378
|
+
)
|
1379
|
+
|
1380
|
+
user = MockUser.new(:test_role)
|
1381
|
+
assert_equal 1, TestAttr.with_permissions_to(:read, :user => user).length
|
1382
|
+
TestModel.delete_all
|
1383
|
+
TestAttr.delete_all
|
1384
|
+
end
|
1385
|
+
|
1386
|
+
def test_with_ored_rules_and_reoccuring_tables
|
1387
|
+
reader = Authorization::Reader::DSLReader.new
|
1388
|
+
reader.parse %{
|
1389
|
+
authorization do
|
1390
|
+
role :test_role do
|
1391
|
+
has_permission_on :test_attrs, :to => :read do
|
1392
|
+
if_attribute :test_another_model => { :content => 'test_1_2' },
|
1393
|
+
:test_model => { :content => 'test_1_1' }
|
1394
|
+
end
|
1395
|
+
has_permission_on :test_attrs, :to => :read do
|
1396
|
+
if_attribute :test_another_model => { :content => 'test_2_2' },
|
1397
|
+
:test_model => { :test_attrs => contains {user.test_attr} }
|
1398
|
+
end
|
1399
|
+
end
|
1400
|
+
end
|
1401
|
+
}
|
1402
|
+
Authorization::Engine.instance(reader)
|
1403
|
+
|
1404
|
+
test_attr_1 = TestAttr.create!(
|
1405
|
+
:test_model => TestModel.create!(:content => 'test_1_1'),
|
1406
|
+
:test_another_model => TestModel.create!(:content => 'test_1_2')
|
1407
|
+
)
|
1408
|
+
test_attr_2 = TestAttr.create!(
|
1409
|
+
:test_model => TestModel.create!(:content => 'test_2_1'),
|
1410
|
+
:test_another_model => TestModel.create!(:content => 'test_2_2')
|
1411
|
+
)
|
1412
|
+
test_attr_2.test_model.test_attrs.create!
|
1413
|
+
|
1414
|
+
user = MockUser.new(:test_role, :test_attr => test_attr_2.test_model.test_attrs.last)
|
1415
|
+
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
|
1416
|
+
TestModel.delete_all
|
1417
|
+
TestAttr.delete_all
|
1418
|
+
end
|
1419
|
+
|
1420
|
+
def test_with_many_ored_rules_and_reoccuring_tables
|
1421
|
+
reader = Authorization::Reader::DSLReader.new
|
1422
|
+
reader.parse %{
|
1423
|
+
authorization do
|
1424
|
+
role :test_role do
|
1425
|
+
has_permission_on :test_attrs, :to => :read do
|
1426
|
+
if_attribute :branch => { :company => { :country => {
|
1427
|
+
:test_models => contains { user.test_model }
|
1428
|
+
}} }
|
1429
|
+
if_attribute :company => { :country => {
|
1430
|
+
:test_models => contains { user.test_model }
|
1431
|
+
}}
|
1432
|
+
end
|
1433
|
+
end
|
1434
|
+
end
|
1435
|
+
}
|
1436
|
+
Authorization::Engine.instance(reader)
|
1437
|
+
|
1438
|
+
country = Country.create!(:name => 'country_1')
|
1439
|
+
country.test_models.create!
|
1440
|
+
test_attr_1 = TestAttr.create!(
|
1441
|
+
:branch => Branch.create!(:name => 'branch_1',
|
1442
|
+
:company => Company.create!(:name => 'company_1',
|
1443
|
+
:country => country))
|
1444
|
+
)
|
1445
|
+
test_attr_2 = TestAttr.create!(
|
1446
|
+
:company => Company.create!(:name => 'company_2',
|
1447
|
+
:country => country)
|
1448
|
+
)
|
1449
|
+
|
1450
|
+
user = MockUser.new(:test_role, :test_model => country.test_models.first)
|
1451
|
+
|
1452
|
+
assert_equal 2, TestAttr.with_permissions_to(:read, :user => user).length
|
1453
|
+
TestModel.delete_all
|
1454
|
+
TestAttr.delete_all
|
1455
|
+
end
|
1456
|
+
end
|
1457
|
+
|
1458
|
+
class ModelTest < Test::Unit::TestCase
|
1459
|
+
def test_permit_with_has_one_raises_no_name_error
|
1460
|
+
reader = Authorization::Reader::DSLReader.new
|
1461
|
+
reader.parse %{
|
1462
|
+
authorization do :test_attr_has_one
|
1463
|
+
role :test_role do
|
1464
|
+
has_permission_on :test_attrs, :to => :update do
|
1465
|
+
if_attribute :id => is { user.test_attr.id }
|
1466
|
+
end
|
1467
|
+
end
|
1468
|
+
end
|
1469
|
+
}
|
1470
|
+
instance = Authorization::Engine.instance(reader)
|
1471
|
+
|
1472
|
+
test_model = TestModel.create!
|
1473
|
+
test_attr = test_model.create_test_attr_has_one
|
1474
|
+
assert !test_attr.new_record?
|
1475
|
+
|
1476
|
+
user = MockUser.new(:test_role, :test_attr => test_attr)
|
1477
|
+
|
1478
|
+
assert_nothing_raised do
|
1479
|
+
assert instance.permit?(:update, :user => user, :object => test_model.test_attr_has_one)
|
1480
|
+
end
|
1481
|
+
|
1482
|
+
TestModel.delete_all
|
1483
|
+
TestAttr.delete_all
|
1484
|
+
end
|
1485
|
+
|
1486
|
+
def test_model_security_with_include_attributes_no_read
|
1487
|
+
reader = Authorization::Reader::DSLReader.new
|
1488
|
+
reader.parse %{
|
1489
|
+
authorization do
|
1490
|
+
role :test_role do
|
1491
|
+
has_permission_on :test_model_security_model_with_include_attributes_no_reads, :to => [:write_attr_2]
|
1492
|
+
end
|
1493
|
+
end
|
1494
|
+
}
|
1495
|
+
Authorization::Engine.instance(reader)
|
1496
|
+
|
1497
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1498
|
+
object = without_access_control do
|
1499
|
+
TestModelSecurityModelWithIncludeAttributesNoRead.create
|
1500
|
+
end
|
1501
|
+
|
1502
|
+
# Should be able to read all
|
1503
|
+
assert_nothing_raised { object.attr_1 }
|
1504
|
+
assert_nothing_raised { object.attr_2 }
|
1505
|
+
assert_nothing_raised { object.attr_3 }
|
1506
|
+
assert_nothing_raised { object.attr_4 }
|
1507
|
+
|
1508
|
+
# Permissions on write
|
1509
|
+
assert_raise(Authorization::NotAuthorized) { object.update_attributes(:attr_1 => 2) }
|
1510
|
+
assert_nothing_raised { object.update_attributes(:attr_2 => 2) }
|
1511
|
+
object.reload
|
1512
|
+
assert_equal 2, object.attr_2
|
1513
|
+
object.destroy
|
1514
|
+
|
1515
|
+
assert_raise ActiveRecord::RecordNotFound do
|
1516
|
+
TestModelSecurityModelWithIncludeAttributesDefault.find(object.id)
|
1517
|
+
end
|
1518
|
+
end
|
1519
|
+
|
1520
|
+
def test_model_security_with_include_attributes_default
|
1521
|
+
reader = Authorization::Reader::DSLReader.new
|
1522
|
+
reader.parse %{
|
1523
|
+
authorization do
|
1524
|
+
role :test_role do
|
1525
|
+
has_permission_on :test_model_security_model_with_include_attributes_defaults, :to => [:read_attr_2, :write_attr_2]
|
1526
|
+
end
|
1527
|
+
end
|
1528
|
+
}
|
1529
|
+
Authorization::Engine.instance(reader)
|
1530
|
+
|
1531
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1532
|
+
object = without_access_control do
|
1533
|
+
TestModelSecurityModelWithIncludeAttributesDefault.create
|
1534
|
+
end
|
1535
|
+
|
1536
|
+
assert_raise(Authorization::NotAuthorized) { object.update_attributes(:attr_1 => 2) }
|
1537
|
+
assert_nothing_raised { object.update_attributes(:attr_2 => 2) }
|
1538
|
+
|
1539
|
+
object.reload
|
1540
|
+
assert_raise(Authorization::NotAuthorized) { object.attr_1 }
|
1541
|
+
assert_equal 2, object.attr_2
|
1542
|
+
object.destroy
|
1543
|
+
|
1544
|
+
assert_equal 'read', TestModelSecurityModelWithIncludeAttributesDefault.read_all_privilege
|
1545
|
+
assert_equal 'write', TestModelSecurityModelWithIncludeAttributesDefault.write_all_privilege
|
1546
|
+
|
1547
|
+
assert_raise ActiveRecord::RecordNotFound do
|
1548
|
+
TestModelSecurityModelWithIncludeAttributesDefault.find(object.id)
|
1549
|
+
end
|
1550
|
+
end
|
1551
|
+
|
1552
|
+
def test_model_security_with_include_attributes_default_read_all
|
1553
|
+
reader = Authorization::Reader::DSLReader.new
|
1554
|
+
reader.parse %{
|
1555
|
+
authorization do
|
1556
|
+
role :test_role do
|
1557
|
+
has_permission_on :test_model_security_model_with_include_attributes_defaults, :to => [:read]
|
1558
|
+
end
|
1559
|
+
end
|
1560
|
+
}
|
1561
|
+
Authorization::Engine.instance(reader)
|
1562
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1563
|
+
object = without_access_control do
|
1564
|
+
TestModelSecurityModelWithIncludeAttributesDefault.create
|
1565
|
+
end
|
1566
|
+
|
1567
|
+
assert_nothing_raised { object.attr_1 }
|
1568
|
+
assert_nothing_raised { object.attr_2 }
|
1569
|
+
|
1570
|
+
assert_raise(Authorization::NotAuthorized) { object.update_attributes(:attr_1 => 2) }
|
1571
|
+
assert_raise(Authorization::NotAuthorized) { object.update_attributes(:attr_2 => 2) }
|
1572
|
+
end
|
1573
|
+
|
1574
|
+
def test_model_security_with_include_attributes_default_write_all
|
1575
|
+
reader = Authorization::Reader::DSLReader.new
|
1576
|
+
reader.parse %{
|
1577
|
+
authorization do
|
1578
|
+
role :test_role do
|
1579
|
+
has_permission_on :test_model_security_model_with_include_attributes_defaults, :to => [:write]
|
1580
|
+
end
|
1581
|
+
end
|
1582
|
+
}
|
1583
|
+
Authorization::Engine.instance(reader)
|
1584
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1585
|
+
object = without_access_control do
|
1586
|
+
TestModelSecurityModelWithIncludeAttributesDefault.create
|
1587
|
+
end
|
1588
|
+
|
1589
|
+
assert_raise(Authorization::NotAuthorized) { object.attr_1 }
|
1590
|
+
assert_raise(Authorization::NotAuthorized) { object.attr_2 }
|
1591
|
+
|
1592
|
+
assert_nothing_raised { object.update_attributes(:attr_1 => 2) }
|
1593
|
+
assert_nothing_raised { object.update_attributes(:attr_2 => 2) }
|
1594
|
+
end
|
1595
|
+
|
1596
|
+
def test_model_security_with_include_attributes_override_read_all
|
1597
|
+
object = without_access_control do
|
1598
|
+
TestModelSecurityModelWithIncludeAttributesReadOverride.create
|
1599
|
+
end
|
1600
|
+
|
1601
|
+
reader = Authorization::Reader::DSLReader.new
|
1602
|
+
reader.parse %{
|
1603
|
+
authorization do
|
1604
|
+
role :test_role do
|
1605
|
+
has_permission_on :test_model_security_model_with_include_attributes_read_overrides, :to => [:read, :write]
|
1606
|
+
end
|
1607
|
+
end
|
1608
|
+
}
|
1609
|
+
|
1610
|
+
Authorization::Engine.instance(reader)
|
1611
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1612
|
+
|
1613
|
+
assert_raise(Authorization::NotAuthorized) { object.attr_1 }
|
1614
|
+
assert_raise(Authorization::NotAuthorized) { object.attr_2 }
|
1615
|
+
assert_nothing_raised { object.update_attributes(:attr_1 => 2) }
|
1616
|
+
assert_nothing_raised { object.update_attributes(:attr_2 => 2) }
|
1617
|
+
|
1618
|
+
reader = Authorization::Reader::DSLReader.new
|
1619
|
+
reader.parse %{
|
1620
|
+
authorization do
|
1621
|
+
role :test_role do
|
1622
|
+
has_permission_on :test_model_security_model_with_include_attributes_read_overrides, :to => [:da_read_all, :write]
|
1623
|
+
end
|
1624
|
+
end
|
1625
|
+
}
|
1626
|
+
|
1627
|
+
Authorization::Engine.instance(reader)
|
1628
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1629
|
+
|
1630
|
+
assert_nothing_raised { object.attr_1 }
|
1631
|
+
assert_nothing_raised { object.attr_2 }
|
1632
|
+
assert_nothing_raised { object.update_attributes(:attr_1 => 2) }
|
1633
|
+
assert_nothing_raised { object.update_attributes(:attr_2 => 2) }
|
1634
|
+
end
|
1635
|
+
|
1636
|
+
def test_model_security_with_include_attributes_override_write_all
|
1637
|
+
object = without_access_control do
|
1638
|
+
TestModelSecurityModelWithIncludeAttributesWriteOverride.create
|
1639
|
+
end
|
1640
|
+
|
1641
|
+
reader = Authorization::Reader::DSLReader.new
|
1642
|
+
reader.parse %{
|
1643
|
+
authorization do
|
1644
|
+
role :test_role do
|
1645
|
+
has_permission_on :test_model_security_model_with_include_attributes_write_overrides, :to => [:read, :write]
|
1646
|
+
end
|
1647
|
+
end
|
1648
|
+
}
|
1649
|
+
|
1650
|
+
Authorization::Engine.instance(reader)
|
1651
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1652
|
+
|
1653
|
+
assert_nothing_raised { object.attr_1 }
|
1654
|
+
assert_nothing_raised { object.attr_2 }
|
1655
|
+
assert_raise(Authorization::NotAuthorized) { object.update_attributes(:attr_1 => 2) }
|
1656
|
+
assert_raise(Authorization::NotAuthorized) { object.update_attributes(:attr_2 => 2) }
|
1657
|
+
|
1658
|
+
reader = Authorization::Reader::DSLReader.new
|
1659
|
+
reader.parse %{
|
1660
|
+
authorization do
|
1661
|
+
role :test_role do
|
1662
|
+
has_permission_on :test_model_security_model_with_include_attributes_write_overrides, :to => [:da_write_all, :read]
|
1663
|
+
end
|
1664
|
+
end
|
1665
|
+
}
|
1666
|
+
|
1667
|
+
Authorization::Engine.instance(reader)
|
1668
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1669
|
+
|
1670
|
+
assert_nothing_raised { object.attr_1 }
|
1671
|
+
assert_nothing_raised { object.attr_2 }
|
1672
|
+
assert_nothing_raised { object.update_attributes(:attr_1 => 2) }
|
1673
|
+
assert_nothing_raised { object.update_attributes(:attr_2 => 2) }
|
1674
|
+
end
|
1675
|
+
|
1676
|
+
def test_readable_attributes_defaults
|
1677
|
+
object = without_access_control do
|
1678
|
+
TestModelSecurityModelWithIncludeAttributesDefault.create
|
1679
|
+
end
|
1680
|
+
|
1681
|
+
reader = Authorization::Reader::DSLReader.new
|
1682
|
+
reader.parse %{
|
1683
|
+
authorization do
|
1684
|
+
role :test_role do
|
1685
|
+
has_permission_on :test_model_security_model_with_include_attributes_defaults, :to => [:manage]
|
1686
|
+
end
|
1687
|
+
end
|
1688
|
+
}
|
1689
|
+
|
1690
|
+
Authorization::Engine.instance(reader)
|
1691
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1692
|
+
|
1693
|
+
# Should only have access to ID
|
1694
|
+
assert_equal({"id" => object.id}, object.readable_attributes, "readable_attributes, No Read Access Given, No White List")
|
1695
|
+
assert_equal(["id"], object.readable_columns, "readable_columns, No Read Access Given, No White List")
|
1696
|
+
assert_equal(["id"], object.showable_columns, "showable_columns, No Read Access Given, No White List")
|
1697
|
+
|
1698
|
+
reader = Authorization::Reader::DSLReader.new
|
1699
|
+
reader.parse %{
|
1700
|
+
authorization do
|
1701
|
+
role :test_role do
|
1702
|
+
has_permission_on :test_model_security_model_with_include_attributes_defaults, :to => [:read]
|
1703
|
+
end
|
1704
|
+
end
|
1705
|
+
}
|
1706
|
+
|
1707
|
+
Authorization::Engine.instance(reader)
|
1708
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1709
|
+
|
1710
|
+
# Should only have access to ID
|
1711
|
+
assert_equal({"id" => object.id, "attr_1"=>1, "attr_2"=>1, "attr_3"=>1, "attr_4"=>1}.sort, object.readable_attributes.sort, "All Access Given, No White List")
|
1712
|
+
assert_equal(["id", "attr_1", "attr_2", "attr_3", "attr_4"].sort, object.readable_columns.sort, "All Access Given, No White List")
|
1713
|
+
assert_equal(["id", "attr_1", "attr_2", "attr_3", "attr_4"].sort, object.showable_columns.sort, "showable_columns, All Access Given, No White List")
|
1714
|
+
end
|
1715
|
+
|
1716
|
+
def test_writable_attributes_defaults
|
1717
|
+
object = without_access_control do
|
1718
|
+
TestModelSecurityModelWithIncludeAttributesDefault.create
|
1719
|
+
end
|
1720
|
+
|
1721
|
+
reader = Authorization::Reader::DSLReader.new
|
1722
|
+
reader.parse %{
|
1723
|
+
authorization do
|
1724
|
+
role :test_role do
|
1725
|
+
has_permission_on :test_model_security_model_with_include_attributes_defaults, :to => [:manage]
|
1726
|
+
end
|
1727
|
+
end
|
1728
|
+
}
|
1729
|
+
|
1730
|
+
Authorization::Engine.instance(reader)
|
1731
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1732
|
+
|
1733
|
+
# Should only have access to ID
|
1734
|
+
assert_equal({"id" => object.id}, object.writable_attributes, "No Read Access Given, No White List")
|
1735
|
+
assert_equal(["id"], object.writable_columns, "No Read Access Given, No White List")
|
1736
|
+
|
1737
|
+
reader = Authorization::Reader::DSLReader.new
|
1738
|
+
reader.parse %{
|
1739
|
+
authorization do
|
1740
|
+
role :test_role do
|
1741
|
+
has_permission_on :test_model_security_model_with_include_attributes_defaults, :to => [:write]
|
1742
|
+
end
|
1743
|
+
end
|
1744
|
+
}
|
1745
|
+
|
1746
|
+
Authorization::Engine.instance(reader)
|
1747
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1748
|
+
|
1749
|
+
# Should only have access to ID
|
1750
|
+
assert_equal({"id" => object.id, "attr_1"=>1, "attr_2"=>1, "attr_3"=>1, "attr_4"=>1}.sort, object.writable_attributes.sort, "All Access Given, No White List")
|
1751
|
+
assert_equal(["id", "attr_1", "attr_2", "attr_3", "attr_4"].sort, object.writable_columns.sort, "All Access Given, No White List")
|
1752
|
+
end
|
1753
|
+
|
1754
|
+
def test_readable_writable_attributes_with_whitelist
|
1755
|
+
object = without_access_control do
|
1756
|
+
TestModelSecurityModelWithIncludeAttributesWhiteList.create
|
1757
|
+
end
|
1758
|
+
|
1759
|
+
reader = Authorization::Reader::DSLReader.new
|
1760
|
+
reader.parse %{authorization do; role :test_role do; has_permission_on :test_model_security_model_with_include_attributes_white_lists, :to => [:read]; end; end}
|
1761
|
+
Authorization::Engine.instance(reader)
|
1762
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1763
|
+
|
1764
|
+
# Should only have access to ID, and white list
|
1765
|
+
assert_equal({"attr_1"=>1, "id"=>object.id, "attr_2"=>1}.sort, object.readable_attributes.sort, "No Read Access Given, But there is a White List")
|
1766
|
+
assert_equal({"attr_1"=>1, "id"=>object.id, "attr_3"=>1}.sort, object.writable_attributes.sort, "No Write Access Given, But there is a White List")
|
1767
|
+
end
|
1768
|
+
|
1769
|
+
def test_readable_writable_attributes_with_all
|
1770
|
+
object = without_access_control do
|
1771
|
+
TestModelSecurityModelWithIncludeAttributesWhiteList.create
|
1772
|
+
end
|
1773
|
+
|
1774
|
+
reader = Authorization::Reader::DSLReader.new
|
1775
|
+
reader.parse %{authorization do; role :test_role do; has_permission_on :test_model_security_model_with_include_attributes_white_lists, :to => [:da_read_all, :da_write_all]; end; end}
|
1776
|
+
Authorization::Engine.instance(reader)
|
1777
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1778
|
+
|
1779
|
+
# Should only have access to ID, and white list
|
1780
|
+
assert_equal({"attr_1"=>1, "attr_2"=>1, "attr_3"=>1, "attr_4"=>1, "id"=>object.id}.sort, object.readable_attributes.sort, "Read All Given")
|
1781
|
+
assert_equal({"attr_1"=>1, "attr_2"=>1, "attr_3"=>1, "attr_4"=>1, "id"=>object.id}.sort, object.writable_attributes.sort, "Write All Given")
|
1782
|
+
end
|
1783
|
+
|
1784
|
+
def test_readable_writable_attributes_with_application_default_attributes
|
1785
|
+
object = without_access_control do
|
1786
|
+
TestModelSecurityModelWithIncludeAttributesApplicationDefaultAttributes.create
|
1787
|
+
end
|
1788
|
+
|
1789
|
+
reader = Authorization::Reader::DSLReader.new
|
1790
|
+
reader.parse %{authorization do; role :test_role do; has_permission_on :test_model_security_model_with_include_attributes_application_default_attributes, :to => [:manage]; end; end}
|
1791
|
+
Authorization::Engine.instance(reader)
|
1792
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1793
|
+
|
1794
|
+
# Should not be able to read/write anything that is not on the whitelist
|
1795
|
+
assert_raise(Authorization::NotAuthorized) { object.attr_2 }
|
1796
|
+
assert_raise(Authorization::NotAuthorized) { object.attr_3 }
|
1797
|
+
assert_raise(Authorization::NotAuthorized) { object.update_attributes(:attr_2 => 2) }
|
1798
|
+
assert_raise(Authorization::NotAuthorized) { object.update_attributes(:attr_3 => 2) }
|
1799
|
+
|
1800
|
+
# Should be able to read/write anything that is on the application_default_attributes
|
1801
|
+
assert_nothing_raised { object.update_attribute(:attr_4, 2) }
|
1802
|
+
assert_equal 2, object.attr_4
|
1803
|
+
|
1804
|
+
# Readable and Writable attributes should return whitelist, application defined attributes and primary key
|
1805
|
+
assert_equal({"attr_1"=>1, "attr_4" => 2, "id"=>object.id}.sort, object.readable_attributes.sort, "readable_attributes, White List Given, Application Default Attributes Given")
|
1806
|
+
assert_equal({"attr_1"=>1, "attr_4" => 2, "id"=>object.id}.sort, object.writable_attributes.sort, "writable_attributes, White List Given, Application Default Attributes Given")
|
1807
|
+
|
1808
|
+
# showable_attributes should not return application_default_attributes
|
1809
|
+
assert_equal({"attr_1"=>1, "id"=>object.id}.sort, object.showable_attributes.sort, "showable_attributes, White List Given, Application Default Attributes Given")
|
1810
|
+
end
|
1811
|
+
|
1812
|
+
def test_permitted_to_alias_chain
|
1813
|
+
object = without_access_control do
|
1814
|
+
TestModelSecurityModelWithIncludeAttributesApplicationDefaultAttributes.create
|
1815
|
+
end
|
1816
|
+
|
1817
|
+
reader = Authorization::Reader::DSLReader.new
|
1818
|
+
reader.parse %{authorization do; role :test_role do; has_permission_on :test_model_security_model_with_include_attributes_application_default_attributes, :to => [:manage, :read_attr_2, :write_attr_3]; end; end}
|
1819
|
+
Authorization::Engine.instance(reader)
|
1820
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1821
|
+
|
1822
|
+
assert object.permitted_to?(:read_attr_1)
|
1823
|
+
assert object.permitted_to?(:write_attr_1)
|
1824
|
+
|
1825
|
+
assert object.permitted_to?(:read_attr_2)
|
1826
|
+
assert !object.permitted_to?(:write_attr_2)
|
1827
|
+
|
1828
|
+
assert !object.permitted_to?(:read_attr_3)
|
1829
|
+
assert object.permitted_to?(:write_attr_3)
|
1830
|
+
|
1831
|
+
assert !object.permitted_to?(:write_attr_4)
|
1832
|
+
assert !object.permitted_to?(:read_attr_4)
|
1833
|
+
end
|
1834
|
+
|
1835
|
+
def test_model_security_write_allowed
|
1836
|
+
reader = Authorization::Reader::DSLReader.new
|
1837
|
+
reader.parse %{
|
1838
|
+
authorization do
|
1839
|
+
role :test_role do
|
1840
|
+
has_permission_on :test_model_security_models do
|
1841
|
+
to :read, :create, :update, :delete
|
1842
|
+
if_attribute :attr => is { 1 }
|
1843
|
+
end
|
1844
|
+
end
|
1845
|
+
end
|
1846
|
+
}
|
1847
|
+
Authorization::Engine.instance(reader)
|
1848
|
+
|
1849
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1850
|
+
assert(object = TestModelSecurityModel.create)
|
1851
|
+
|
1852
|
+
assert_nothing_raised { object.update_attributes(:attr_2 => 2) }
|
1853
|
+
object.reload
|
1854
|
+
assert_equal 2, object.attr_2
|
1855
|
+
object.destroy
|
1856
|
+
assert_raise ActiveRecord::RecordNotFound do
|
1857
|
+
TestModelSecurityModel.find(object.id)
|
1858
|
+
end
|
1859
|
+
end
|
1860
|
+
|
1861
|
+
def test_model_security_write_not_allowed_no_privilege
|
1862
|
+
reader = Authorization::Reader::DSLReader.new
|
1863
|
+
reader.parse %{
|
1864
|
+
authorization do
|
1865
|
+
role :test_role do
|
1866
|
+
has_permission_on :test_model_security_models do
|
1867
|
+
to :read, :create, :update, :delete
|
1868
|
+
if_attribute :attr => is { 1 }
|
1869
|
+
end
|
1870
|
+
end
|
1871
|
+
role :test_role_restricted do
|
1872
|
+
end
|
1873
|
+
end
|
1874
|
+
}
|
1875
|
+
Authorization::Engine.instance(reader)
|
1876
|
+
|
1877
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1878
|
+
assert(object = TestModelSecurityModel.create)
|
1879
|
+
|
1880
|
+
Authorization.current_user = MockUser.new(:test_role_restricted)
|
1881
|
+
assert_raise Authorization::NotAuthorized do
|
1882
|
+
object.update_attributes(:attr_2 => 2)
|
1883
|
+
end
|
1884
|
+
end
|
1885
|
+
|
1886
|
+
def test_model_security_write_not_allowed_wrong_attribute_value
|
1887
|
+
reader = Authorization::Reader::DSLReader.new
|
1888
|
+
reader.parse %{
|
1889
|
+
authorization do
|
1890
|
+
role :test_role_unrestricted do
|
1891
|
+
has_permission_on :test_model_security_models do
|
1892
|
+
to :read, :create, :update, :delete
|
1893
|
+
end
|
1894
|
+
end
|
1895
|
+
role :test_role do
|
1896
|
+
has_permission_on :test_model_security_models do
|
1897
|
+
to :read, :create, :update, :delete
|
1898
|
+
if_attribute :attr => is { 1 }
|
1899
|
+
end
|
1900
|
+
end
|
1901
|
+
end
|
1902
|
+
}
|
1903
|
+
Authorization::Engine.instance(reader)
|
1904
|
+
|
1905
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1906
|
+
assert(object = TestModelSecurityModel.create)
|
1907
|
+
assert_raise Authorization::AttributeAuthorizationError do
|
1908
|
+
TestModelSecurityModel.create :attr => 2
|
1909
|
+
end
|
1910
|
+
object = TestModelSecurityModel.create
|
1911
|
+
assert_raise Authorization::AttributeAuthorizationError do
|
1912
|
+
object.update_attributes(:attr => 2)
|
1913
|
+
end
|
1914
|
+
object.reload
|
1915
|
+
|
1916
|
+
assert_nothing_raised do
|
1917
|
+
object.update_attributes(:attr_2 => 1)
|
1918
|
+
end
|
1919
|
+
assert_raise Authorization::AttributeAuthorizationError do
|
1920
|
+
object.update_attributes(:attr => 2)
|
1921
|
+
end
|
1922
|
+
end
|
1923
|
+
|
1924
|
+
def test_model_security_with_and_without_find_restrictions
|
1925
|
+
reader = Authorization::Reader::DSLReader.new
|
1926
|
+
reader.parse %{
|
1927
|
+
authorization do
|
1928
|
+
role :test_role_unrestricted do
|
1929
|
+
has_permission_on :test_model_security_models do
|
1930
|
+
to :read, :create, :update, :delete
|
1931
|
+
end
|
1932
|
+
end
|
1933
|
+
role :test_role do
|
1934
|
+
has_permission_on :test_model_security_models do
|
1935
|
+
to :read, :create, :update, :delete
|
1936
|
+
if_attribute :attr => is { 1 }
|
1937
|
+
end
|
1938
|
+
end
|
1939
|
+
end
|
1940
|
+
}
|
1941
|
+
Authorization::Engine.instance(reader)
|
1942
|
+
|
1943
|
+
Authorization.current_user = MockUser.new(:test_role_unrestricted)
|
1944
|
+
object = TestModelSecurityModel.create :attr => 2
|
1945
|
+
object_with_find = TestModelSecurityModelWithFind.create :attr => 2
|
1946
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1947
|
+
assert_nothing_raised do
|
1948
|
+
object.class.find(object.id)
|
1949
|
+
end
|
1950
|
+
assert_raise Authorization::AttributeAuthorizationError do
|
1951
|
+
object_with_find.class.find(object_with_find.id)
|
1952
|
+
end
|
1953
|
+
end
|
1954
|
+
|
1955
|
+
def test_model_security_delete_unallowed
|
1956
|
+
reader = Authorization::Reader::DSLReader.new
|
1957
|
+
reader.parse %{
|
1958
|
+
authorization do
|
1959
|
+
role :test_role_unrestricted do
|
1960
|
+
has_permission_on :test_model_security_models do
|
1961
|
+
to :read, :create, :update, :delete
|
1962
|
+
end
|
1963
|
+
end
|
1964
|
+
role :test_role do
|
1965
|
+
has_permission_on :test_model_security_models do
|
1966
|
+
to :read, :create, :update, :delete
|
1967
|
+
if_attribute :attr => is { 1 }
|
1968
|
+
end
|
1969
|
+
end
|
1970
|
+
end
|
1971
|
+
}
|
1972
|
+
Authorization::Engine.instance(reader)
|
1973
|
+
|
1974
|
+
Authorization.current_user = MockUser.new(:test_role_unrestricted)
|
1975
|
+
object = TestModelSecurityModel.create :attr => 2
|
1976
|
+
Authorization.current_user = MockUser.new(:test_role)
|
1977
|
+
|
1978
|
+
assert_raise Authorization::AttributeAuthorizationError do
|
1979
|
+
object.destroy
|
1980
|
+
end
|
1981
|
+
end
|
1982
|
+
|
1983
|
+
def test_model_security_changing_critical_attribute_unallowed
|
1984
|
+
reader = Authorization::Reader::DSLReader.new
|
1985
|
+
reader.parse %{
|
1986
|
+
authorization do
|
1987
|
+
role :test_role_unrestricted do
|
1988
|
+
has_permission_on :test_model_security_models do
|
1989
|
+
to :read, :create, :update, :delete
|
1990
|
+
end
|
1991
|
+
end
|
1992
|
+
role :test_role do
|
1993
|
+
has_permission_on :test_model_security_models do
|
1994
|
+
to :read, :create, :update, :delete
|
1995
|
+
if_attribute :attr => is { 1 }
|
1996
|
+
end
|
1997
|
+
end
|
1998
|
+
end
|
1999
|
+
}
|
2000
|
+
Authorization::Engine.instance(reader)
|
2001
|
+
|
2002
|
+
Authorization.current_user = MockUser.new(:test_role_unrestricted)
|
2003
|
+
object = TestModelSecurityModel.create :attr => 2
|
2004
|
+
Authorization.current_user = MockUser.new(:test_role)
|
2005
|
+
|
2006
|
+
# TODO before not checked yet
|
2007
|
+
#assert_raise Authorization::AuthorizationError do
|
2008
|
+
# object.update_attributes(:attr => 1)
|
2009
|
+
#end
|
2010
|
+
end
|
2011
|
+
|
2012
|
+
def test_model_security_no_role_unallowed
|
2013
|
+
reader = Authorization::Reader::DSLReader.new
|
2014
|
+
reader.parse %{
|
2015
|
+
authorization do
|
2016
|
+
end
|
2017
|
+
}
|
2018
|
+
Authorization::Engine.instance(reader)
|
2019
|
+
|
2020
|
+
Authorization.current_user = MockUser.new(:test_role_2)
|
2021
|
+
assert_raise Authorization::NotAuthorized do
|
2022
|
+
TestModelSecurityModel.create
|
2023
|
+
end
|
2024
|
+
end
|
2025
|
+
|
2026
|
+
def test_model_security_with_assoc
|
2027
|
+
reader = Authorization::Reader::DSLReader.new
|
2028
|
+
reader.parse %{
|
2029
|
+
authorization do
|
2030
|
+
role :test_role do
|
2031
|
+
has_permission_on :test_model_security_models do
|
2032
|
+
to :create, :update, :delete
|
2033
|
+
if_attribute :test_attrs => contains { user }
|
2034
|
+
end
|
2035
|
+
end
|
2036
|
+
end
|
2037
|
+
}
|
2038
|
+
Authorization::Engine.instance(reader)
|
2039
|
+
|
2040
|
+
test_attr = TestAttr.create
|
2041
|
+
test_attr.role_symbols << :test_role
|
2042
|
+
Authorization.current_user = test_attr
|
2043
|
+
assert(object = TestModelSecurityModel.create(:test_attrs => [test_attr]))
|
2044
|
+
assert_nothing_raised do
|
2045
|
+
object.update_attributes(:attr_2 => 2)
|
2046
|
+
end
|
2047
|
+
without_access_control do
|
2048
|
+
object.reload
|
2049
|
+
end
|
2050
|
+
assert_equal 2, object.attr_2
|
2051
|
+
object.destroy
|
2052
|
+
assert_raise ActiveRecord::RecordNotFound do
|
2053
|
+
TestModelSecurityModel.find(object.id)
|
2054
|
+
end
|
2055
|
+
end
|
2056
|
+
|
2057
|
+
def test_model_security_with_update_attrbributes
|
2058
|
+
reader = Authorization::Reader::DSLReader.new
|
2059
|
+
reader.parse %{
|
2060
|
+
authorization do
|
2061
|
+
role :test_role do
|
2062
|
+
has_permission_on :test_model_security_models, :to => :update do
|
2063
|
+
if_attribute :test_attrs => { :branch => is { user.branch }}
|
2064
|
+
end
|
2065
|
+
end
|
2066
|
+
end
|
2067
|
+
}
|
2068
|
+
Authorization::Engine.instance(reader)
|
2069
|
+
|
2070
|
+
params = {
|
2071
|
+
:model_data => { :attr => 11 }
|
2072
|
+
}
|
2073
|
+
|
2074
|
+
test_attr = TestAttr.create!(:branch => Branch.create!)
|
2075
|
+
test_model = without_access_control do
|
2076
|
+
TestModelSecurityModel.create!(:test_attrs => [test_attr])
|
2077
|
+
end
|
2078
|
+
|
2079
|
+
with_user MockUser.new(:test_role, :branch => test_attr.branch) do
|
2080
|
+
assert_nothing_raised do
|
2081
|
+
test_model.update_attributes(params[:model_data])
|
2082
|
+
end
|
2083
|
+
end
|
2084
|
+
without_access_control do
|
2085
|
+
assert_equal params[:model_data][:attr], test_model.reload.attr
|
2086
|
+
end
|
2087
|
+
|
2088
|
+
TestAttr.delete_all
|
2089
|
+
TestModelSecurityModel.delete_all
|
2090
|
+
Branch.delete_all
|
2091
|
+
end
|
2092
|
+
|
2093
|
+
def test_using_access_control
|
2094
|
+
assert !TestModel.using_access_control?
|
2095
|
+
assert TestModelSecurityModel.using_access_control?
|
2096
|
+
end
|
2097
|
+
|
2098
|
+
def test_authorization_permit_association_proxy
|
2099
|
+
reader = Authorization::Reader::DSLReader.new
|
2100
|
+
reader.parse %{
|
2101
|
+
authorization do
|
2102
|
+
role :test_role do
|
2103
|
+
has_permission_on :test_attrs, :to => :read do
|
2104
|
+
if_attribute :test_model => {:content => "content" }
|
2105
|
+
end
|
2106
|
+
end
|
2107
|
+
end
|
2108
|
+
}
|
2109
|
+
engine = Authorization::Engine.instance(reader)
|
2110
|
+
|
2111
|
+
test_model = TestModel.create(:content => "content")
|
2112
|
+
assert engine.permit?(:read, :object => test_model.test_attrs,
|
2113
|
+
:user => MockUser.new(:test_role))
|
2114
|
+
assert !engine.permit?(:read, :object => TestAttr.new,
|
2115
|
+
:user => MockUser.new(:test_role))
|
2116
|
+
TestModel.delete_all
|
2117
|
+
end
|
2118
|
+
|
2119
|
+
def test_multiple_roles_with_has_many_through
|
2120
|
+
reader = Authorization::Reader::DSLReader.new
|
2121
|
+
reader.parse %{
|
2122
|
+
authorization do
|
2123
|
+
role :test_role_1 do
|
2124
|
+
has_permission_on :test_models, :to => :read do
|
2125
|
+
if_attribute :test_attr_throughs => contains {user.test_attr_through_id},
|
2126
|
+
:content => 'test_1'
|
2127
|
+
end
|
2128
|
+
end
|
2129
|
+
|
2130
|
+
role :test_role_2 do
|
2131
|
+
has_permission_on :test_models, :to => :read do
|
2132
|
+
if_attribute :test_attr_throughs_2 => contains {user.test_attr_through_2_id},
|
2133
|
+
:content => 'test_2'
|
2134
|
+
end
|
2135
|
+
end
|
2136
|
+
end
|
2137
|
+
}
|
2138
|
+
Authorization::Engine.instance(reader)
|
2139
|
+
|
2140
|
+
test_model_1 = TestModel.create! :content => 'test_1'
|
2141
|
+
test_model_2 = TestModel.create! :content => 'test_2'
|
2142
|
+
test_model_1.test_attrs.create!.test_attr_throughs.create!
|
2143
|
+
test_model_2.test_attrs.create!.test_attr_throughs.create!
|
2144
|
+
|
2145
|
+
user = MockUser.new(:test_role_1, :test_role_2,
|
2146
|
+
:test_attr_through_id => test_model_1.test_attr_throughs.first.id,
|
2147
|
+
:test_attr_through_2_id => test_model_2.test_attr_throughs.first.id)
|
2148
|
+
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
|
2149
|
+
TestModel.delete_all
|
2150
|
+
TestAttr.delete_all
|
2151
|
+
TestAttrThrough.delete_all
|
2152
|
+
end
|
2153
|
+
|
2154
|
+
def test_model_permitted_to
|
2155
|
+
reader = Authorization::Reader::DSLReader.new
|
2156
|
+
reader.parse %{
|
2157
|
+
authorization do
|
2158
|
+
role :test_role do
|
2159
|
+
has_permission_on :companies, :to => :read do
|
2160
|
+
if_attribute :name => "company_1"
|
2161
|
+
end
|
2162
|
+
end
|
2163
|
+
end
|
2164
|
+
}
|
2165
|
+
Authorization::Engine.instance(reader)
|
2166
|
+
|
2167
|
+
user = MockUser.new(:test_role)
|
2168
|
+
allowed_read_company = Company.new(:name => 'company_1')
|
2169
|
+
prohibited_company = Company.new(:name => 'company_2')
|
2170
|
+
|
2171
|
+
assert allowed_read_company.permitted_to?(:read, :user => user)
|
2172
|
+
assert !allowed_read_company.permitted_to?(:update, :user => user)
|
2173
|
+
assert !prohibited_company.permitted_to?(:read, :user => user)
|
2174
|
+
|
2175
|
+
executed_block = false
|
2176
|
+
allowed_read_company.permitted_to?(:read, :user => user) do
|
2177
|
+
executed_block = true
|
2178
|
+
end
|
2179
|
+
assert executed_block
|
2180
|
+
|
2181
|
+
executed_block = false
|
2182
|
+
prohibited_company.permitted_to?(:read, :user => user) do
|
2183
|
+
executed_block = true
|
2184
|
+
end
|
2185
|
+
assert !executed_block
|
2186
|
+
|
2187
|
+
assert_nothing_raised do
|
2188
|
+
allowed_read_company.permitted_to!(:read, :user => user)
|
2189
|
+
end
|
2190
|
+
assert_raise Authorization::NotAuthorized do
|
2191
|
+
prohibited_company.permitted_to!(:update, :user => user)
|
2192
|
+
end
|
2193
|
+
assert_raise Authorization::AttributeAuthorizationError do
|
2194
|
+
prohibited_company.permitted_to!(:read, :user => user)
|
2195
|
+
end
|
2196
|
+
end
|
2197
|
+
|
2198
|
+
def test_model_permitted_to_with_modified_context
|
2199
|
+
reader = Authorization::Reader::DSLReader.new
|
2200
|
+
reader.parse %{
|
2201
|
+
authorization do
|
2202
|
+
role :test_role do
|
2203
|
+
has_permission_on :companies, :to => :read
|
2204
|
+
end
|
2205
|
+
end
|
2206
|
+
}
|
2207
|
+
Authorization::Engine.instance(reader)
|
2208
|
+
|
2209
|
+
user = MockUser.new(:test_role)
|
2210
|
+
allowed_read_company = SmallCompany.new(:name => 'small_company_1')
|
2211
|
+
|
2212
|
+
assert allowed_read_company.permitted_to?(:read, :user => user)
|
2213
|
+
assert !allowed_read_company.permitted_to?(:update, :user => user)
|
2214
|
+
end
|
2215
|
+
end
|
2216
|
+
|