Skip to content

Commit 1f9c2e6

Browse files
author
Jeroen van Dijk
committed
Implement a :namespace option for filter_access_to in order to handle namespaced controllers.
Conflicts: declarative_authorization.gemspec.src test/controller_test.rb Merge uhees and stffn
1 parent 7225757 commit 1f9c2e6

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

lib/declarative_authorization/in_controller.rb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ module ClassMethods
222222
# Privilege required; defaults to action_name
223223
# [:+context+]
224224
# The privilege's context, defaults to controller_name, pluralized.
225+
# [:+namespace+]
226+
# Prefix the default controller context with.
227+
# * +true+: the model namespace(s) separated with underscores,
228+
# * +Symbol+ or +String+: the given symbol or string
229+
# * else: no prefix
230+
# Example:
231+
# filter_access_to :show, :namespace => true
232+
# filter_access_to :delete, :namespace => :foo
225233
# [:+attribute_check+]
226234
# Enables the check of attributes defined in the authorization rules.
227235
# Defaults to false. If enabled, filter_access_to will use a context
@@ -250,6 +258,7 @@ def filter_access_to (*args, &filter_block)
250258
options = {
251259
:require => nil,
252260
:context => nil,
261+
:namespace => nil,
253262
:attribute_check => false,
254263
:model => nil,
255264
:load_method => nil
@@ -268,6 +277,7 @@ def filter_access_to (*args, &filter_block)
268277
end
269278
filter_access_permissions <<
270279
ControllerPermission.new(actions, privilege, context,
280+
options[:namespace],
271281
options[:attribute_check],
272282
options[:model],
273283
options[:load_method],
@@ -498,19 +508,31 @@ def actions_from_option (option) # :nodoc:
498508
end
499509

500510
class ControllerPermission # :nodoc:
501-
attr_reader :actions, :privilege, :context, :attribute_check
502-
def initialize (actions, privilege, context, attribute_check = false,
511+
attr_reader :actions, :privilege, :context, :namespace, :attribute_check
512+
def initialize (actions, privilege, context, namespace, attribute_check = false,
503513
load_object_model = nil, load_object_method = nil,
504514
filter_block = nil)
505515
@actions = actions.to_set
506516
@privilege = privilege
507517
@context = context
518+
@namespace = namespace
508519
@load_object_model = load_object_model
509520
@load_object_method = load_object_method
510521
@filter_block = filter_block
511522
@attribute_check = attribute_check
512523
end
513524

525+
def controller_context(contr)
526+
case @namespace
527+
when true
528+
"#{contr.class.name.gsub(/::/, "_").gsub(/Controller$/, "").underscore}".to_sym
529+
when String, Symbol
530+
"#{@namespace.to_s}_#{contr.class.controller_name}".to_sym
531+
else
532+
contr.class.controller_name.to_sym
533+
end
534+
end
535+
514536
def matches? (action_name)
515537
@actions.include?(action_name.to_sym)
516538
end
@@ -519,7 +541,7 @@ def permit! (contr)
519541
if @filter_block
520542
return contr.instance_eval(&@filter_block)
521543
end
522-
context = @context || contr.class.controller_name.to_sym
544+
context = @context || controller_context(contr)
523545
object = @attribute_check ? load_object(contr, context) : nil
524546
privilege = @privilege || :"#{contr.action_name}"
525547

test/controller_test.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33

44
class LoadMockObject < MockDataObject
5+
def self.find(*args)
6+
new :id => args[0]
7+
end
58
end
69

710
##################
@@ -367,3 +370,47 @@ def test_controller_hierarchy
367370
assert !@controller.authorized?
368371
end
369372
end
373+
374+
##################
375+
module Foo
376+
class CommonController < MocksController
377+
filter_access_to :all
378+
filter_access_to :new
379+
filter_access_to :show, :namespace => :bar
380+
filter_access_to :delete, :namespace => true
381+
382+
define_action_methods :new, :show, :delete
383+
end
384+
end
385+
class NamespacedControllerTest < ActionController::TestCase
386+
tests Foo::CommonController
387+
def test_namespaced_controller
388+
reader = Authorization::Reader::DSLReader.new
389+
reader.parse %{
390+
authorization do
391+
role :test_role1 do
392+
has_permission_on :common, :to => [:new, :show, :delete]
393+
end
394+
role :test_role2 do
395+
has_permission_on :common, :to => [:new]
396+
has_permission_on :bar_common, :to => [:show]
397+
has_permission_on :foo_common, :to => [:delete]
398+
end
399+
end
400+
}
401+
request!(MockUser.new(:test_role1), "new", reader)
402+
assert @controller.authorized?
403+
request!(MockUser.new(:test_role1), "show", reader)
404+
assert !@controller.authorized?
405+
request!(MockUser.new(:test_role1), "delete", reader)
406+
assert !@controller.authorized?
407+
408+
request!(MockUser.new(:test_role2), "new", reader)
409+
assert @controller.authorized?
410+
request!(MockUser.new(:test_role2), "show", reader)
411+
assert @controller.authorized?
412+
request!(MockUser.new(:test_role2), "delete", reader)
413+
assert @controller.authorized?
414+
end
415+
end
416+

0 commit comments

Comments
 (0)