@@ -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
0 commit comments