Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding a before filter for loading and authorizing a resource

  • Loading branch information...
commit 1034c817634ec286e7ee313a8ca42e8ec2060a2f 1 parent 6c6a57b
@ryanb authored
View
2  lib/cancan/ability.rb
@@ -9,7 +9,7 @@ def self.included(base)
base.alias_action :edit, :to => :update
end
- def can?(original_action, target)
+ def can?(original_action, target) # TODO this could use some refactoring
(self.class.can_history || []).reverse.each do |can_action, can_target, can_block|
possible_actions_for(original_action).each do |action|
if (can_action == :manage || can_action == action) && (can_target == :all || can_target == target || target.kind_of?(can_target))
View
17 lib/cancan/controller_additions.rb
@@ -15,6 +15,23 @@ def current_ability
def can?(*args)
(@current_ability ||= current_ability).can?(*args)
end
+
+ def load_resource # TODO this could use some refactoring
+ if params[:id]
+ instance_variable_set("@#{params[:controller].singularize}", params[:controller].singularize.camelcase.constantize.find(params[:id]))
+ elsif params[params[:controller].singularize.to_sym]
+ instance_variable_set("@#{params[:controller].singularize}", params[:controller].singularize.camelcase.constantize.new(params[params[:controller].singularize.to_sym]))
+ end
+ end
+
+ def authorize_resource # TODO this could use some refactoring
+ unauthorized! unless can?(params[:action].to_sym, instance_variable_get("@#{params[:controller].singularize}") || params[:controller].singularize.camelcase.constantize)
+ end
+
+ def load_and_authorize_resource
+ load_resource
+ authorize_resource
+ end
end
end
View
43 spec/cancan/controller_additions_spec.rb
@@ -29,4 +29,47 @@ class Ability
@controller.current_ability.should be_kind_of(Ability)
@controller.can?(:foo, :bar).should be_false
end
+
+ it "should load the resource if params[:id] is specified" do
+ stub(@controller).params { {:controller => "abilities", :action => "show", :id => 123} }
+ stub(Ability).find(123) { :some_resource }
+ @controller.load_resource
+ @controller.instance_variable_get(:@ability).should == :some_resource
+ end
+
+ it "should build a new resource with hash if params[:id] is not specified" do
+ stub(@controller).params { {:controller => "abilities", :action => "create", :ability => {:foo => "bar"}} }
+ stub(Ability).new(:foo => "bar") { :some_resource }
+ @controller.load_resource
+ @controller.instance_variable_get(:@ability).should == :some_resource
+ end
+
+ it "should not build a resource of neither id nor attributes are specified" do
+ stub(@controller).params { {:controller => "abilities", :action => "index"} }
+ @controller.load_resource
+ @controller.instance_variable_get(:@ability).should be_nil
+ end
+
+ it "should perform authorization using controller action and loaded model" do
+ @controller.instance_variable_set(:@ability, :some_resource)
+ stub(@controller).params { {:controller => "abilities", :action => "show"} }
+ stub(@controller).can?(:show, :some_resource) { false }
+ lambda {
+ @controller.authorize_resource
+ }.should raise_error(CanCan::AccessDenied)
+ end
+
+ it "should perform authorization using controller action and non loaded model" do
+ stub(@controller).params { {:controller => "abilities", :action => "show"} }
+ stub(@controller).can?(:show, Ability) { false }
+ lambda {
+ @controller.authorize_resource
+ }.should raise_error(CanCan::AccessDenied)
+ end
+
+ it "should load and authorize resource in one call" do
+ mock(@controller).load_resource
+ stub(@controller).authorize_resource
+ @controller.load_and_authorize_resource
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.