@@ -17,14 +17,27 @@ class TestModel < ActiveRecord::Base
17
17
has_many :test_attrs
18
18
has_many :test_another_attrs , :class_name => "TestAttr" , :foreign_key => :test_another_model_id
19
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
20
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"
21
+ has_many :branches
22
+
23
+ # :conditions is deprecated in Rails 4.1
24
+ if Rails . version >= '4'
25
+ has_many :test_attrs_with_attr , lambda { where ( :attr => 1 ) } , :class_name => "TestAttr"
26
+ has_many :test_attr_throughs_with_attr , lambda { where ( "test_attrs.attr = 1" ) } , :through => :test_attrs ,
27
+ :class_name => "TestAttrThrough" , :source => :test_attr_throughs
28
+
29
+ has_one :test_attr_throughs_with_attr_and_has_one , lambda { where ( "test_attrs.attr = 1" ) } , :through => :test_attrs ,
30
+ :class_name => "TestAttrThrough" , :source => :test_attr_throughs
31
+ else
32
+ has_many :test_attrs_with_attr , :class_name => "TestAttr" , :conditions => { :attr => 1 }
33
+ has_many :test_attr_throughs_with_attr , :through => :test_attrs ,
34
+ :class_name => "TestAttrThrough" , :source => :test_attr_throughs ,
35
+ :conditions => "test_attrs.attr = 1"
36
+
37
+ has_one :test_attr_throughs_with_attr_and_has_one , :through => :test_attrs ,
38
+ :class_name => "TestAttrThrough" , :source => :test_attr_throughs ,
39
+ :conditions => "test_attrs.attr = 1"
40
+ end
28
41
29
42
if Rails . version < '4'
30
43
attr_accessible :content , :test_attr_through_id , :country_id
@@ -38,8 +51,10 @@ class TestModel < ActiveRecord::Base
38
51
39
52
if Rails . version < "3"
40
53
named_scope :with_content , :conditions => "test_models.content IS NOT NULL"
41
- else
54
+ elsif Rails . version < "4"
42
55
scope :with_content , :conditions => "test_models.content IS NOT NULL"
56
+ else
57
+ scope :with_content , lambda { where ( "test_models.content IS NOT NULL" ) }
43
58
end
44
59
45
60
# Primary key test
@@ -121,9 +136,10 @@ class TestModelSecurityModelWithFind < ActiveRecord::Base
121
136
class Branch < ActiveRecord ::Base
122
137
has_many :test_attrs
123
138
belongs_to :company
139
+ belongs_to :test_model
124
140
125
141
if Rails . version < '4'
126
- attr_accessible :name , :company
142
+ attr_accessible :name , :company , :test_model
127
143
end
128
144
end
129
145
class Company < ActiveRecord ::Base
@@ -170,7 +186,12 @@ def test_multiple_deep_ored_belongs_to
170
186
:test_another_model_id => test_model_2 . id
171
187
172
188
user = MockUser . new ( :test_role , :id => test_attr_1 )
173
- assert_equal 1 , TestAttr . with_permissions_to ( :read , :user => user ) . length
189
+ if Rails . version >= '4'
190
+ assert_equal 1 , TestAttr . with_permissions_to ( :read , :user => user ) . references ( :test_attrs , :test_attrs_test_models , :test_attrs_test_models_2 ) . length
191
+ else
192
+ assert_equal 1 , TestAttr . with_permissions_to ( :read , :user => user ) . length
193
+ end
194
+
174
195
TestAttr . delete_all
175
196
TestModel . delete_all
176
197
end
@@ -802,7 +823,11 @@ def test_with_contains
802
823
user = MockUser . new ( :test_role ,
803
824
:id => test_model_1 . test_attrs . first . id )
804
825
assert_equal 1 , TestModel . with_permissions_to ( :read , :user => user ) . length
805
- assert_equal 1 , TestModel . with_permissions_to ( :read , :user => user ) . find ( :all , :conditions => { :id => test_model_1 . id } ) . length
826
+ if Rails . version < '3'
827
+ assert_equal 1 , TestModel . with_permissions_to ( :read , :user => user ) . find ( :all , :conditions => { :id => test_model_1 . id } ) . length
828
+ else
829
+ assert_equal 1 , TestModel . with_permissions_to ( :read , :user => user ) . where ( :id => test_model_1 . id ) . length
830
+ end
806
831
807
832
TestModel . delete_all
808
833
TestAttr . delete_all
@@ -1185,7 +1210,11 @@ def test_with_anded_if_permitted_to
1185
1210
assert Authorization ::Engine . instance . permit? ( :read , :object => test_model_1 . test_attrs . first , :user => user_with_both_roles )
1186
1211
assert Authorization ::Engine . instance . permit? ( :read , :object => test_model_for_second_role . test_attrs . first , :user => user_with_both_roles )
1187
1212
#p Authorization::Engine.instance.obligations(:read, :user => user_with_both_roles, :context => :test_attrs)
1188
- assert_equal 2 , TestAttr . with_permissions_to ( :read , :user => user_with_both_roles ) . length
1213
+ if Rails . version >= '4'
1214
+ assert_equal 2 , TestAttr . with_permissions_to ( :read , :user => user_with_both_roles ) . references ( :test_attrs , :test_models ) . length
1215
+ else
1216
+ assert_equal 1 , TestAttr . with_permissions_to ( :read , :user => user ) . length
1217
+ end
1189
1218
1190
1219
TestModel . delete_all
1191
1220
TestAttr . delete_all
@@ -1446,7 +1475,11 @@ def test_with_ored_rules_and_reoccuring_tables
1446
1475
test_attr_2 . test_model . test_attrs . create!
1447
1476
1448
1477
user = MockUser . new ( :test_role , :test_attr => test_attr_2 . test_model . test_attrs . last )
1449
- assert_equal 2 , TestAttr . with_permissions_to ( :read , :user => user ) . length
1478
+ if Rails . version >= '4'
1479
+ assert_equal 2 , TestAttr . with_permissions_to ( :read , :user => user ) . references ( :test_attrs , :test_models , :test_models_test_attrs , :test_attrs_test_models ) . length
1480
+ else
1481
+ assert_equal 2 , TestAttr . with_permissions_to ( :read , :user => user ) . length
1482
+ end
1450
1483
TestModel . delete_all
1451
1484
TestAttr . delete_all
1452
1485
end
@@ -1483,7 +1516,11 @@ def test_with_many_ored_rules_and_reoccuring_tables
1483
1516
1484
1517
user = MockUser . new ( :test_role , :test_model => country . test_models . first )
1485
1518
1486
- assert_equal 2 , TestAttr . with_permissions_to ( :read , :user => user ) . length
1519
+ if Rails . version >= '4'
1520
+ assert_equal 2 , TestAttr . with_permissions_to ( :read , :user => user ) . references ( :test_attrs , :test_models , :test_models_countries ) . length
1521
+ else
1522
+ assert_equal 2 , TestAttr . with_permissions_to ( :read , :user => user ) . length
1523
+ end
1487
1524
TestModel . delete_all
1488
1525
TestAttr . delete_all
1489
1526
end
@@ -1822,12 +1859,43 @@ def test_authorization_permit_association_proxy
1822
1859
test_model = TestModel . create ( :content => "content" )
1823
1860
assert engine . permit? ( :read , :object => test_model . test_attrs ,
1824
1861
:user => MockUser . new ( :test_role ) )
1825
- assert test_model . test_attrs . empty?
1826
1862
assert !engine . permit? ( :read , :object => TestAttr . new ,
1827
1863
:user => MockUser . new ( :test_role ) )
1828
1864
TestModel . delete_all
1829
1865
end
1830
1866
1867
+ # def test_authorization_permit_nested_association_proxy
1868
+ # reader = Authorization::Reader::DSLReader.new
1869
+ # reader.parse %{
1870
+ # authorization do
1871
+ # role :test_role do
1872
+ # has_permission_on :branches, :to => :read do
1873
+ # if_attribute :test_model => { :test_attrs => {:attr => 1 } }
1874
+ # end
1875
+ # end
1876
+ # end
1877
+ # }
1878
+ # engine = Authorization::Engine.instance(reader)
1879
+
1880
+ # test_model = TestModel.create!
1881
+ # test_model.test_attrs.create!(:attr => 0)
1882
+ # test_attr = test_model.test_attrs.create!(:attr => 1)
1883
+ # test_model.test_attrs.create!(:attr => 3)
1884
+ # test_branch = Branch.create!(:test_model => test_model)
1885
+
1886
+ # test_model_2 = TestModel.create!
1887
+ # test_attr_2 = test_model_2.test_attrs.create!(:attr => 2)
1888
+ # test_branch_2 = Branch.create!(:test_model => test_model_2)
1889
+
1890
+ # assert engine.permit?(:read, :object => test_branch,
1891
+ # :user => MockUser.new(:test_role))
1892
+ # assert !engine.permit?(:read, :object => test_branch_2,
1893
+ # :user => MockUser.new(:test_role))
1894
+ # TestModel.delete_all
1895
+ # Branch.delete_all
1896
+ # TestAttr.delete_all
1897
+ # end
1898
+
1831
1899
def test_multiple_roles_with_has_many_through
1832
1900
reader = Authorization ::Reader ::DSLReader . new
1833
1901
reader . parse %{
@@ -1860,7 +1928,11 @@ def test_multiple_roles_with_has_many_through
1860
1928
user = MockUser . new ( :test_role_1 , :test_role_2 ,
1861
1929
:test_attr_through_id => test_model_1 . test_attr_throughs . first . id ,
1862
1930
:test_attr_through_2_id => test_model_2 . test_attr_throughs . first . id )
1863
- assert_equal 2 , TestModel . with_permissions_to ( :read , :user => user ) . length
1931
+ if Rails . version >= '4'
1932
+ assert_equal 2 , TestModel . with_permissions_to ( :read , :user => user ) . references ( :test_models , :test_attr_throughs ) . length
1933
+ else
1934
+ assert_equal 2 , TestModel . with_permissions_to ( :read , :user => user ) . length
1935
+ end
1864
1936
TestModel . delete_all
1865
1937
TestAttr . delete_all
1866
1938
TestAttrThrough . delete_all
0 commit comments