Permalink
Browse files

add attribute to CanCan::Unauthorized exceptions

  • Loading branch information...
1 parent aed9f26 commit 1c83429d1f888e3977c86f80ac0082261a5530b1 @jhawthorn committed Aug 12, 2012
Showing with 32 additions and 4 deletions.
  1. +1 −1 lib/cancan/ability.rb
  2. +3 −2 lib/cancan/exceptions.rb
  3. +13 −0 spec/cancan/ability_spec.rb
  4. +15 −1 spec/cancan/exceptions_spec.rb
View
@@ -204,7 +204,7 @@ def authorize!(action, subject, *args)
attribute = args.first
if cannot?(action, subject, *args)
message ||= unauthorized_message(action, subject)
- raise Unauthorized.new(message, action, subject)
+ raise Unauthorized.new(message, action, subject, attribute)
elsif sufficient_attribute_check?(action, subject, attribute) && sufficient_condition_check?(action, subject)
fully_authorized!(action, subject)
end
View
@@ -36,13 +36,14 @@ class InsufficientAuthorizationCheck < Error; end
# See ControllerAdditions#authorize! for more information on rescuing from this exception
# and customizing the message using I18n.
class Unauthorized < Error
- attr_reader :action, :subject
+ attr_reader :action, :subject, :attribute
attr_writer :default_message
- def initialize(message = nil, action = nil, subject = nil)
+ def initialize(message = nil, action = nil, subject = nil, attribute = nil)
@message = message
@action = action
@subject = subject
+ @attribute = attribute
@default_message = I18n.t(:"unauthorized.default", :default => "You are not authorized to access this page.")
end
@@ -408,6 +408,19 @@ class Container < Hash; end
e.message.should == "Access denied!"
e.action.should == :read
e.subject.should == :books
+ e.attribute.should be_nil
+ else
+ fail "Expected CanCan::Unauthorized exception to be raised"
+ end
+ end
+
+ it "raises CanCan::Unauthorized when calling authorize! on unauthorized attribute" do
+ begin
+ @ability.authorize! :read, :books, :title
+ rescue CanCan::Unauthorized => e
+ e.action.should == :read
+ e.subject.should == :books
+ e.attribute.should == :title
else
fail "Expected CanCan::Unauthorized exception to be raised"
end
@@ -1,6 +1,18 @@
require "spec_helper"
describe CanCan::Unauthorized do
+ describe "with action, subject, and attribute" do
+ before(:each) do
+ @exception = CanCan::Unauthorized.new(nil, :some_action, :some_subject, :some_attr)
+ end
+
+ it "has action, subject, and attribute accessors" do
+ @exception.action.should == :some_action
+ @exception.subject.should == :some_subject
+ @exception.attribute.should == :some_attr
+ end
+ end
+
describe "with action and subject" do
before(:each) do
@exception = CanCan::Unauthorized.new(nil, :some_action, :some_subject)
@@ -9,6 +21,7 @@
it "has action and subject accessors" do
@exception.action.should == :some_action
@exception.subject.should == :some_subject
+ @exception.attribute.should be_nil
end
it "has a changable default message" do
@@ -23,9 +36,10 @@
@exception = CanCan::Unauthorized.new("Access denied!")
end
- it "has nil action and subject" do
+ it "has nil action, subject, and attribute" do
@exception.action.should be_nil
@exception.subject.should be_nil
+ @exception.attribute.should be_nil
end
it "has passed message" do

0 comments on commit 1c83429

Please sign in to comment.