changing the interface for ControllerResource load/authorize so they can be intertwined

This commit is contained in:
Ryan Bates
2011-05-19 16:38:33 -04:00
parent e24d5d146b
commit a29e31606b
5 changed files with 74 additions and 114 deletions
+3 -3
View File
@@ -12,7 +12,7 @@ module CanCan
# end
#
def load_and_authorize_resource(*args)
cancan_resource_class.add_before_filter(self, :load_and_authorize_resource, *args)
cancan_resource_class.add_before_filter(self, {:load => true, :authorize => true}, *args)
end
# Sets up a before filter which loads the model resource into an instance variable.
@@ -114,7 +114,7 @@ module CanCan
#
def load_resource(*args)
raise ImplementationRemoved, "The load_resource method has been removed, use load_and_authorize_resource instead."
cancan_resource_class.add_before_filter(self, :load_resource, *args)
cancan_resource_class.add_before_filter(self, {:load => true}, *args)
end
# Sets up a before filter which authorizes the resource using the instance variable.
@@ -171,7 +171,7 @@ module CanCan
#
def authorize_resource(*args)
raise ImplementationRemoved, "The authorize_resource method has been removed, use load_and_authorize_resource instead."
cancan_resource_class.add_before_filter(self, :authorize_resource, *args)
cancan_resource_class.add_before_filter(self, {:authorize => true}, *args)
end
# Skip both the loading and authorization behavior of CanCan for this given controller. This is primarily
+19 -23
View File
@@ -2,12 +2,12 @@ module CanCan
# Handle the load and authorization controller logic so we don't clutter up all controllers with non-interface methods.
# This class is used internally, so you do not need to call methods directly on it.
class ControllerResource # :nodoc:
def self.add_before_filter(controller_class, method, *args)
options = args.extract_options!
def self.add_before_filter(controller_class, behavior, *args)
options = args.extract_options!.merge(behavior)
resource_name = args.first
before_filter_method = options.delete(:prepend) ? :prepend_before_filter : :before_filter
controller_class.send(before_filter_method, options.slice(:only, :except)) do |controller|
controller.class.cancan_resource_class.new(controller, resource_name, options.except(:only, :except)).send(method)
controller.class.cancan_resource_class.new(controller, resource_name, options.except(:only, :except)).process
end
end
@@ -18,28 +18,24 @@ module CanCan
@name = args.first
end
def load_and_authorize_resource
load_resource
authorize_resource
end
def load_resource
if load_instance?
self.resource_instance ||= load_resource_instance
elsif load_collection?
self.collection_instance ||= load_collection
current_ability.fully_authorized! @params[:action], @params[:controller]
def process
if @options[:load]
if load_instance?
self.resource_instance ||= load_resource_instance
elsif load_collection?
self.collection_instance ||= load_collection
current_ability.fully_authorized! @params[:action], @params[:controller]
end
end
end
def authorize_resource
if resource_instance
if @params[name] && (authorization_action == :create || authorization_action == :update)
@params[name].each do |key, value|
@controller.authorize!(authorization_action, resource_instance, key.to_sym)
if @options[:authorize]
if resource_instance
if @params[name] && (authorization_action == :create || authorization_action == :update)
@params[name].each do |key, value|
@controller.authorize!(authorization_action, resource_instance, key.to_sym)
end
else
@controller.authorize!(authorization_action, resource_instance)
end
else
@controller.authorize!(authorization_action, resource_instance)
end
end
end