uhees-declarative_authorization 0.3.1 → 0.3.2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +6 -0
- data/README.rdoc +24 -7
- data/app/controllers/authorization_rules_controller.rb +29 -5
- data/app/helpers/authorization_rules_helper.rb +7 -3
- data/app/views/authorization_rules/_change.erb +12 -3
- data/app/views/authorization_rules/_show_graph.erb +2 -2
- data/app/views/authorization_rules/_suggestions.erb +38 -14
- data/app/views/authorization_rules/change.html.erb +37 -9
- data/lib/declarative_authorization/authorization.rb +10 -7
- data/lib/declarative_authorization/development_support/analyzer.rb +2 -2
- data/lib/declarative_authorization/development_support/change_supporter.rb +46 -4
- data/lib/declarative_authorization/in_controller.rb +255 -3
- data/lib/declarative_authorization/in_model.rb +11 -2
- data/lib/declarative_authorization/maintenance.rb +2 -8
- data/lib/declarative_authorization/obligation_scope.rb +18 -7
- data/lib/declarative_authorization/reader.rb +4 -1
- data/test/authorization_test.rb +6 -2
- data/test/controller_filter_resource_access_test.rb +394 -0
- data/test/controller_test.rb +19 -8
- data/test/maintenance_test.rb +5 -0
- data/test/model_test.rb +137 -0
- data/test/schema.sql +1 -0
- data/test/test_helper.rb +18 -3
- metadata +5 -4
- data/app/views/authorization_rules/_suggestion.erb +0 -9
data/test/controller_test.rb
CHANGED
@@ -2,12 +2,11 @@ require File.join(File.dirname(__FILE__), 'test_helper.rb')
|
|
2
2
|
|
3
3
|
|
4
4
|
class LoadMockObject < MockDataObject
|
5
|
-
def self.
|
6
|
-
|
5
|
+
def self.name
|
6
|
+
"LoadMockObject"
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
|
11
10
|
##################
|
12
11
|
class SpecificMocksController < MocksController
|
13
12
|
filter_access_to :test_action, :require => :test, :context => :permissions
|
@@ -26,7 +25,6 @@ end
|
|
26
25
|
class BasicControllerTest < ActionController::TestCase
|
27
26
|
tests SpecificMocksController
|
28
27
|
|
29
|
-
|
30
28
|
def test_filter_access_to_receiving_an_explicit_array
|
31
29
|
reader = Authorization::Reader::DSLReader.new
|
32
30
|
|
@@ -129,9 +127,6 @@ class BasicControllerTest < ActionController::TestCase
|
|
129
127
|
}
|
130
128
|
request!(MockUser.new(:test_role), "new", reader)
|
131
129
|
assert @controller.authorized?
|
132
|
-
|
133
|
-
request!(MockUser.new(:test_role), "edit_2", reader)
|
134
|
-
assert !@controller.authorized?
|
135
130
|
end
|
136
131
|
|
137
132
|
def test_existing_instance_var_remains_unchanged
|
@@ -243,6 +238,23 @@ class LoadObjectControllerTest < ActionController::TestCase
|
|
243
238
|
assert @controller.authorized?
|
244
239
|
assert @controller.instance_variable_defined?(:@load_mock_object)
|
245
240
|
end
|
241
|
+
|
242
|
+
def test_filter_access_object_load_without_param
|
243
|
+
reader = Authorization::Reader::DSLReader.new
|
244
|
+
reader.parse %{
|
245
|
+
authorization do
|
246
|
+
role :test_role do
|
247
|
+
has_permission_on :load_mock_objects, :to => [:show, :edit] do
|
248
|
+
if_attribute :id => is {"1"}
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
}
|
253
|
+
|
254
|
+
assert_raise RuntimeError, "No id param supplied" do
|
255
|
+
request!(MockUser.new(:test_role), "show", reader)
|
256
|
+
end
|
257
|
+
end
|
246
258
|
|
247
259
|
def test_filter_access_with_object_load_custom
|
248
260
|
reader = Authorization::Reader::DSLReader.new
|
@@ -415,4 +427,3 @@ class NamespacedControllerTest < ActionController::TestCase
|
|
415
427
|
assert @controller.authorized?
|
416
428
|
end
|
417
429
|
end
|
418
|
-
|
data/test/maintenance_test.rb
CHANGED
@@ -2,6 +2,7 @@ require File.join(File.dirname(__FILE__), 'test_helper.rb')
|
|
2
2
|
require File.join(File.dirname(__FILE__), %w{.. lib declarative_authorization maintenance})
|
3
3
|
|
4
4
|
class MaintenanceTest < Test::Unit::TestCase
|
5
|
+
include Authorization::TestHelper
|
5
6
|
|
6
7
|
def test_usages_by_controllers
|
7
8
|
usage_test_controller = Class.new(ActionController::Base)
|
@@ -25,6 +26,10 @@ class MaintenanceTest < Test::Unit::TestCase
|
|
25
26
|
assert !engine.permit?(:test_2, :context => :permissions,
|
26
27
|
:user => MockUser.new(:test_role))
|
27
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
|
28
33
|
assert engine.permit?(:test_2, :context => :permissions,
|
29
34
|
:user => MockUser.new(:test_role))
|
30
35
|
end
|
data/test/model_test.rb
CHANGED
@@ -28,6 +28,8 @@ class TestModel < ActiveRecord::Base
|
|
28
28
|
has_and_belongs_to_many :test_attr_throughs_habtm, :join_table => :test_attrs,
|
29
29
|
:class_name => "TestAttrThrough"
|
30
30
|
|
31
|
+
named_scope :with_content, :conditions => "test_models.content IS NOT NULL"
|
32
|
+
|
31
33
|
# Primary key test
|
32
34
|
# take this out for Rails prior to 2.2
|
33
35
|
if ([Rails::VERSION::MAJOR, Rails::VERSION::MINOR] <=> [2, 2]) > -1
|
@@ -84,6 +86,11 @@ class Company < ActiveRecord::Base
|
|
84
86
|
has_many :branches
|
85
87
|
belongs_to :country
|
86
88
|
end
|
89
|
+
class SmallCompany < Company
|
90
|
+
def self.decl_auth_context
|
91
|
+
:companies
|
92
|
+
end
|
93
|
+
end
|
87
94
|
class Country < ActiveRecord::Base
|
88
95
|
has_many :test_models
|
89
96
|
has_many :companies
|
@@ -168,6 +175,74 @@ class ModelTest < Test::Unit::TestCase
|
|
168
175
|
TestModel.delete_all
|
169
176
|
end
|
170
177
|
|
178
|
+
def test_named_scope_on_proxy
|
179
|
+
reader = Authorization::Reader::DSLReader.new
|
180
|
+
reader.parse %{
|
181
|
+
authorization do
|
182
|
+
role :test_role do
|
183
|
+
has_permission_on :test_attrs, :to => :read do
|
184
|
+
if_attribute :id => is { user.test_attr_value }
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
}
|
189
|
+
Authorization::Engine.instance(reader)
|
190
|
+
|
191
|
+
test_model_1 = TestModel.create!
|
192
|
+
test_attr_1 = test_model_1.test_attrs.create!
|
193
|
+
test_model_1.test_attrs.create!
|
194
|
+
TestAttr.create!
|
195
|
+
|
196
|
+
user = MockUser.new(:test_role, :test_attr_value => test_attr_1.id)
|
197
|
+
assert_equal 1, test_model_1.test_attrs.with_permissions_to(:read, :user => user).length
|
198
|
+
TestModel.delete_all
|
199
|
+
TestAttr.delete_all
|
200
|
+
end
|
201
|
+
|
202
|
+
def test_named_scope_on_named_scope
|
203
|
+
reader = Authorization::Reader::DSLReader.new
|
204
|
+
reader.parse %{
|
205
|
+
authorization do
|
206
|
+
role :test_role do
|
207
|
+
has_permission_on :test_models, :to => :read do
|
208
|
+
if_attribute :country_id => 1
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
}
|
213
|
+
Authorization::Engine.instance(reader)
|
214
|
+
|
215
|
+
TestModel.create!(:country_id => 1, :content => "Content")
|
216
|
+
TestModel.create!(:country_id => 1)
|
217
|
+
TestModel.create!(:country_id => 2, :content => "Content")
|
218
|
+
|
219
|
+
user = MockUser.new(:test_role)
|
220
|
+
assert_equal 2, TestModel.with_permissions_to(:read, :user => user).length
|
221
|
+
assert_equal 1, TestModel.with_content.with_permissions_to(:read, :user => user).length
|
222
|
+
TestModel.delete_all
|
223
|
+
end
|
224
|
+
|
225
|
+
def test_named_scope_with_modified_context
|
226
|
+
reader = Authorization::Reader::DSLReader.new
|
227
|
+
reader.parse %{
|
228
|
+
authorization do
|
229
|
+
role :test_role do
|
230
|
+
has_permission_on :companies, :to => :read do
|
231
|
+
if_attribute :id => is { user.test_company_id }
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
}
|
236
|
+
Authorization::Engine.instance(reader)
|
237
|
+
|
238
|
+
test_company = SmallCompany.create!
|
239
|
+
|
240
|
+
user = MockUser.new(:test_role, :test_company_id => test_company.id)
|
241
|
+
assert_equal 1, SmallCompany.with_permissions_to(:read,
|
242
|
+
:user => user).length
|
243
|
+
SmallCompany.delete_all
|
244
|
+
end
|
245
|
+
|
171
246
|
def test_named_scope_with_is_nil
|
172
247
|
reader = Authorization::Reader::DSLReader.new
|
173
248
|
reader.parse %{
|
@@ -1168,4 +1243,66 @@ class ModelTest < Test::Unit::TestCase
|
|
1168
1243
|
TestModel.delete_all
|
1169
1244
|
TestAttr.delete_all
|
1170
1245
|
end
|
1246
|
+
|
1247
|
+
def test_model_permitted_to
|
1248
|
+
reader = Authorization::Reader::DSLReader.new
|
1249
|
+
reader.parse %{
|
1250
|
+
authorization do
|
1251
|
+
role :test_role do
|
1252
|
+
has_permission_on :companies, :to => :read do
|
1253
|
+
if_attribute :name => "company_1"
|
1254
|
+
end
|
1255
|
+
end
|
1256
|
+
end
|
1257
|
+
}
|
1258
|
+
Authorization::Engine.instance(reader)
|
1259
|
+
|
1260
|
+
user = MockUser.new(:test_role)
|
1261
|
+
allowed_read_company = Company.new(:name => 'company_1')
|
1262
|
+
prohibited_company = Company.new(:name => 'company_2')
|
1263
|
+
|
1264
|
+
assert allowed_read_company.permitted_to?(:read, :user => user)
|
1265
|
+
assert !allowed_read_company.permitted_to?(:update, :user => user)
|
1266
|
+
assert !prohibited_company.permitted_to?(:read, :user => user)
|
1267
|
+
|
1268
|
+
executed_block = false
|
1269
|
+
allowed_read_company.permitted_to?(:read, :user => user) do
|
1270
|
+
executed_block = true
|
1271
|
+
end
|
1272
|
+
assert executed_block
|
1273
|
+
|
1274
|
+
executed_block = false
|
1275
|
+
prohibited_company.permitted_to?(:read, :user => user) do
|
1276
|
+
executed_block = true
|
1277
|
+
end
|
1278
|
+
assert !executed_block
|
1279
|
+
|
1280
|
+
assert_nothing_raised do
|
1281
|
+
allowed_read_company.permitted_to!(:read, :user => user)
|
1282
|
+
end
|
1283
|
+
assert_raise Authorization::NotAuthorized do
|
1284
|
+
prohibited_company.permitted_to!(:update, :user => user)
|
1285
|
+
end
|
1286
|
+
assert_raise Authorization::AttributeAuthorizationError do
|
1287
|
+
prohibited_company.permitted_to!(:read, :user => user)
|
1288
|
+
end
|
1289
|
+
end
|
1290
|
+
|
1291
|
+
def test_model_permitted_to_with_modified_context
|
1292
|
+
reader = Authorization::Reader::DSLReader.new
|
1293
|
+
reader.parse %{
|
1294
|
+
authorization do
|
1295
|
+
role :test_role do
|
1296
|
+
has_permission_on :companies, :to => :read
|
1297
|
+
end
|
1298
|
+
end
|
1299
|
+
}
|
1300
|
+
Authorization::Engine.instance(reader)
|
1301
|
+
|
1302
|
+
user = MockUser.new(:test_role)
|
1303
|
+
allowed_read_company = SmallCompany.new(:name => 'small_company_1')
|
1304
|
+
|
1305
|
+
assert allowed_read_company.permitted_to?(:read, :user => user)
|
1306
|
+
assert !allowed_read_company.permitted_to?(:update, :user => user)
|
1307
|
+
end
|
1171
1308
|
end
|
data/test/schema.sql
CHANGED
data/test/test_helper.rb
CHANGED
@@ -34,12 +34,21 @@ class MockDataObject
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
def descends_from_active_record?
|
37
|
+
def self.descends_from_active_record?
|
38
38
|
true
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
def self.table_name
|
42
|
-
|
42
|
+
name.tableize
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.name
|
46
|
+
"Mock"
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.find(*args)
|
50
|
+
raise "Couldn't find #{self.name} with id #{args[0].inspect}" unless args[0]
|
51
|
+
new :id => args[0]
|
43
52
|
end
|
44
53
|
end
|
45
54
|
|
@@ -70,6 +79,10 @@ class MocksController < ActionController::Base
|
|
70
79
|
end
|
71
80
|
end
|
72
81
|
end
|
82
|
+
|
83
|
+
def self.define_resource_actions
|
84
|
+
define_action_methods :index, :show, :edit, :update, :new, :create, :destroy
|
85
|
+
end
|
73
86
|
|
74
87
|
def logger (*args)
|
75
88
|
Class.new do
|
@@ -77,8 +90,10 @@ class MocksController < ActionController::Base
|
|
77
90
|
#p args
|
78
91
|
end
|
79
92
|
alias_method :info, :warn
|
93
|
+
alias_method :debug, :warn
|
80
94
|
def warn?; end
|
81
95
|
alias_method :info?, :warn?
|
96
|
+
alias_method :debug?, :warn?
|
82
97
|
end.new
|
83
98
|
end
|
84
99
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uhees-declarative_authorization
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.1
|
4
|
+
version: 0.3.2.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steffen Bartsch
|
@@ -28,8 +28,9 @@ executables: []
|
|
28
28
|
|
29
29
|
extensions: []
|
30
30
|
|
31
|
-
extra_rdoc_files:
|
32
|
-
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.rdoc
|
33
|
+
- CHANGELOG
|
33
34
|
files:
|
34
35
|
- CHANGELOG
|
35
36
|
- MIT-LICENSE
|
@@ -43,7 +44,6 @@ files:
|
|
43
44
|
- app/helpers/authorization_rules_helper.rb
|
44
45
|
- app/views/authorization_rules/_change.erb
|
45
46
|
- app/views/authorization_rules/_show_graph.erb
|
46
|
-
- app/views/authorization_rules/_suggestion.erb
|
47
47
|
- app/views/authorization_rules/_suggestions.erb
|
48
48
|
- app/views/authorization_rules/change.html.erb
|
49
49
|
- app/views/authorization_rules/graph.dot.erb
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- lib/declarative_authorization/rails_legacy.rb
|
66
66
|
- lib/declarative_authorization/reader.rb
|
67
67
|
- test/authorization_test.rb
|
68
|
+
- test/controller_filter_resource_access_test.rb
|
68
69
|
- test/controller_test.rb
|
69
70
|
- test/development_support
|
70
71
|
- test/dsl_reader_test.rb
|