Merge pull request #660 from fl00r/master

Segmentation fault on aliasing
This commit is contained in:
Ryan Bates 2012-07-02 13:36:44 -07:00
commit cad4db2d7b
2 changed files with 10 additions and 0 deletions

View File

@ -172,10 +172,16 @@ module CanCan
# This way one can use params[:action] in the controller to determine the permission. # This way one can use params[:action] in the controller to determine the permission.
def alias_action(*args) def alias_action(*args)
target = args.pop[:to] target = args.pop[:to]
validate_target(target)
aliased_actions[target] ||= [] aliased_actions[target] ||= []
aliased_actions[target] += args aliased_actions[target] += args
end end
# User shouldn't specify targets with names of real actions or it will cause Seg fault
def validate_target(target)
raise Error, "You can't specify target (#{target}) as alias because it is real action name" if aliased_actions.values.flatten.include? target
end
# Returns a hash of aliased actions. The key is the target and the value is an array of actions aliasing the key. # Returns a hash of aliased actions. The key is the target and the value is an array of actions aliasing the key.
def aliased_actions def aliased_actions
@aliased_actions ||= default_alias_actions @aliased_actions ||= default_alias_actions

View File

@ -87,6 +87,10 @@ describe CanCan::Ability do
@ability.can?(:increment, 123).should be_true @ability.can?(:increment, 123).should be_true
end end
it "should raise an Error if alias target is an exist action" do
lambda{ @ability.alias_action :show, :to => :show }.should raise_error(CanCan::Error, "You can't specify target (show) as alias because it is real action name")
end
it "should always call block with arguments when passing no arguments to can" do it "should always call block with arguments when passing no arguments to can" do
@ability.can do |action, object_class, object| @ability.can do |action, object_class, object|
action.should == :foo action.should == :foo