diff --git a/lib/declarative_authorization/helper.rb b/lib/declarative_authorization/helper.rb index cf06ada2..00bb9472 100644 --- a/lib/declarative_authorization/helper.rb +++ b/lib/declarative_authorization/helper.rb @@ -60,5 +60,9 @@ def has_role_with_hierarchy?(*roles, &block) def has_any_role?(*roles,&block) controller.has_any_role?(*roles,&block) end + + def has_any_role_with_hierarchy?(*roles, &block) + controller.has_any_role_with_hierarchy?(*roles, &block) + end end end \ No newline at end of file diff --git a/lib/declarative_authorization/in_controller.rb b/lib/declarative_authorization/in_controller.rb index 67c686fa..beba8830 100644 --- a/lib/declarative_authorization/in_controller.rb +++ b/lib/declarative_authorization/in_controller.rb @@ -107,6 +107,15 @@ def has_role_with_hierarchy?(*roles, &block) result end + # As has_any_role? except checks all roles included in the role hierarchy + def has_any_role_with_hierarchy?(*roles, &block) + user_roles = authorization_engine.roles_with_hierarchy_for(current_user) + result = roles.any? do |role| + user_roles.include?(role) + end + yield if result and block_given? + result + end protected def filter_access_filter # :nodoc: diff --git a/test/helper_test.rb b/test/helper_test.rb index 5ced67e7..e01905e1 100644 --- a/test/helper_test.rb +++ b/test/helper_test.rb @@ -200,8 +200,48 @@ def test_has_role_with_hierarchy block_evaled = true end assert !block_evaled - end - + def test_has_any_role_with_hierarchy + reader = Authorization::Reader::DSLReader.new + reader.parse %{ + authorization do + role :test_role do + has_permission_on :mocks, :to => :show + end + role :other_role do + has_permission_on :another_mocks, :to => :show + end + + role :root do + includes :test_role + end + end + } + + user = MockUser.new(:root) + request!(user, :action, reader) + + assert has_any_role_with_hierarchy?(:test_role) + assert !has_any_role_with_hierarchy?(:other_role) + assert has_any_role_with_hierarchy?(:test_role,:other_role) + + block_evaled = false + has_any_role_with_hierarchy?(:test_role) do + block_evaled = true + end + assert block_evaled + + block_evaled = false + has_any_role_with_hierarchy?(:test_role2) do + block_evaled = true + end + assert !block_evaled + + block_evaled = false + has_any_role_with_hierarchy?(:test_role,:test_role2) do + block_evaled = true + end + assert block_evaled + end end \ No newline at end of file