Skip to content

Commit 8976405

Browse files
committed
Fix stffn#193.
1 parent 9503abb commit 8976405

2 files changed

Lines changed: 45 additions & 39 deletions

File tree

lib/declarative_authorization/authorization.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,14 @@ def validate? (attr_validator, object = nil, hash = nil)
512512
object ||= attr_validator.object
513513
return false unless object
514514

515+
if Authorization.is_a_association_proxy?(object)
516+
return false unless object.length > 0
517+
object.each do |member|
518+
return true if validate?(attr_validator, member, hash)
519+
end
520+
return false
521+
end
522+
515523
(hash || @conditions_hash).all? do |attr, value|
516524
attr_value = object_attribute_value(object, attr)
517525
if value.is_a?(Hash)
@@ -625,14 +633,7 @@ def to_long_s (hash = nil)
625633
protected
626634
def object_attribute_value (object, attr)
627635
begin
628-
if object.respond_to?(:proxy_association)
629-
# first = object.first
630-
# object.delete(first)
631-
# first.send(attr)
632-
object.first.send(attr)
633-
else
634-
object.send(attr)
635-
end
636+
object.send(attr)
636637
rescue ArgumentError, NoMethodError => e
637638
raise AuthorizationUsageError, "Error occurred while validating attribute ##{attr} on #{object.inspect}: #{e}.\n" +
638639
"Please check your authorization rules and ensure the attribute is correctly spelled and \n" +

test/model_test.rb

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,37 +1864,42 @@ def test_authorization_permit_association_proxy
18641864
TestModel.delete_all
18651865
end
18661866

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
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+
test_model_3 = TestModel.create!
1891+
test_branch_3 = Branch.create!(:test_model => test_model_3)
1892+
1893+
assert engine.permit?(:read, :object => test_branch,
1894+
:user => MockUser.new(:test_role))
1895+
assert !engine.permit?(:read, :object => test_branch_2,
1896+
:user => MockUser.new(:test_role))
1897+
assert !engine.permit?(:read, :object => test_branch_3,
1898+
:user => MockUser.new(:test_role))
1899+
TestModel.delete_all
1900+
Branch.delete_all
1901+
TestAttr.delete_all
1902+
end
18981903

18991904
def test_multiple_roles_with_has_many_through
19001905
reader = Authorization::Reader::DSLReader.new

0 commit comments

Comments
 (0)