Skip to content

Commit

Permalink
Updates handling of association proxies and tests for Rails 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
stffn committed Jan 10, 2012
1 parent e932697 commit 28c30d0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
10 changes: 9 additions & 1 deletion lib/declarative_authorization/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def self.default_role
def self.default_role= (role)
@@default_role = role.to_sym
end

def self.is_a_association_proxy? (object)
if Rails.version < "3.2"
object.respond_to?(:proxy_reflection)
else
object.respond_to?(:proxy_association)
end
end

# Authorization::Engine implements the reference monitor. It may be used
# for querying the permission and retrieving obligations under which
Expand Down Expand Up @@ -155,7 +163,7 @@ def permit! (privilege, options = {})
#
# Example: permit!( :edit, :object => user.posts )
#
if options[:object].respond_to?( :proxy_reflection ) && options[:object].respond_to?( :new )
if Authorization.is_a_association_proxy?(options[:object]) && options[:object].respond_to?(:new)
options[:object] = options[:object].new
end

Expand Down
2 changes: 1 addition & 1 deletion lib/declarative_authorization/in_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def options_for_permit (object_or_sym = nil, options = {}, bang = true)
context = object = nil
if object_or_sym.nil?
context = self.class.decl_auth_context
elsif !object_or_sym.respond_to?(:proxy_reflection) and object_or_sym.is_a?(Symbol)
elsif !Authorization.is_a_association_proxy?(object_or_sym) and object_or_sym.is_a?(Symbol)
context = object_or_sym
else
object = object_or_sym
Expand Down
14 changes: 9 additions & 5 deletions lib/declarative_authorization/obligation_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,13 @@ def add_obligation_join_for( path )
# Returns the model associated with the given path.
def model_for (path)
reflection = reflection_for(path)

if reflection.respond_to?(:proxy_reflection)
reflection.proxy_reflection.klass

if Authorization.is_a_association_proxy?(reflection)
if Rails.version < "3.2"
reflection.proxy_reflection.klass
else
reflection.proxy_association.reflection.klass
end
elsif reflection.respond_to?(:klass)
reflection.klass
else
Expand All @@ -167,7 +171,7 @@ def map_reflection_for( path )

reflection = path.empty? ? top_level_model : begin
parent = reflection_for( path[0..-2] )
if !parent.respond_to?(:proxy_reflection) and parent.respond_to?(:klass)
if !Authorization.is_a_association_proxy?(parent) and parent.respond_to?(:klass)
parent.klass.reflect_on_association( path.last )
else
parent.reflect_on_association( path.last )
Expand All @@ -182,7 +186,7 @@ def map_reflection_for( path )

# Claim alias for join table
# TODO change how this is checked
if !reflection.respond_to?(:proxy_reflection) and !reflection.respond_to?(:proxy_scope) and reflection.is_a?(ActiveRecord::Reflection::ThroughReflection)
if !Authorization.is_a_association_proxy?(reflection) and !reflection.respond_to?(:proxy_scope) and reflection.is_a?(ActiveRecord::Reflection::ThroughReflection)
join_table_path = path[0..-2] + [reflection.options[:through]]
reflection_for(join_table_path, true)
end
Expand Down
18 changes: 17 additions & 1 deletion test/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ class TestModelSecurityModel < ActiveRecord::Base
using_access_control
end
class TestModelSecurityModelWithFind < ActiveRecord::Base
set_table_name "test_model_security_models"
if Rails.version < "3.2"
set_table_name "test_model_security_models"
else
self.table_name = "test_model_security_models"
end
has_many :test_attrs
belongs_to :test_attr
using_access_control :include_read => true,
Expand Down Expand Up @@ -209,6 +213,9 @@ def test_with_nested_has_many_through
end
}
Authorization::Engine.instance(reader)
TestModel.delete_all
TestAttrThrough.delete_all
TestAttr.delete_all

allowed_model = TestModel.create!
allowed_model.test_attrs.create!(:attr => 1).test_attr_throughs.create!
Expand Down Expand Up @@ -383,6 +390,7 @@ def test_with_not_is
end
}
Authorization::Engine.instance(reader)
TestModel.delete_all

test_model_1 = TestModel.create!
TestModel.create!
Expand Down Expand Up @@ -911,6 +919,9 @@ def test_with_contains_through_primary_key
end
}
Authorization::Engine.instance(reader)
TestModel.delete_all
TestAttrThrough.delete_all
TestAttr.delete_all

test_attr_through_1 = TestAttrThrough.create!
test_item = NWayJoinItem.create!
Expand Down Expand Up @@ -1061,6 +1072,8 @@ def test_with_not_is_in
end
}
Authorization::Engine.instance(reader)
TestModel.delete_all
TestAttr.delete_all

test_model_1 = TestModel.create!
test_model_2 = TestModel.create!
Expand Down Expand Up @@ -1803,6 +1816,9 @@ def test_multiple_roles_with_has_many_through
end
}
Authorization::Engine.instance(reader)
TestModel.delete_all
TestAttr.delete_all
TestAttrThrough.delete_all

test_model_1 = TestModel.create! :content => 'test_1'
test_model_2 = TestModel.create! :content => 'test_2'
Expand Down

0 comments on commit 28c30d0

Please sign in to comment.