Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include extra args in AccessDenied exceptions #173

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
Develop

1.10.1 (January 27th, 2015)

* Complete cancancan#172 - Include extra args in AccessDenied exception.

1.10.0 (August 28, 2014)

* Complete cancancan#115 - Specify authorization action for parent resources.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate the two features in two different PR.


1.10.1 (January 13th, 2015)

Expand Down
2 changes: 1 addition & 1 deletion lib/cancan/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def authorize!(action, subject, *args)
end
if cannot?(action, subject, *args)
message ||= unauthorized_message(action, subject)
raise AccessDenied.new(message, action, subject)
raise AccessDenied.new(message, action, subject, args)
end
subject
end
Expand Down
6 changes: 5 additions & 1 deletion lib/cancan/controller_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ def adapter
end

def authorization_action
parent? ? :show : @params[:action].to_sym
parent? ? parent_authorization_action : @params[:action].to_sym
end

def parent_authorization_action
@options[:parent_action] || :show
end

def id_param
Expand Down
5 changes: 3 additions & 2 deletions lib/cancan/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ class AuthorizationNotPerformed < Error; end
# See ControllerAdditions#authorized! for more information on rescuing from this exception
# and customizing the message using I18n.
class AccessDenied < Error
attr_reader :action, :subject
attr_reader :action, :subject, :extra_args
attr_writer :default_message

def initialize(message = nil, action = nil, subject = nil)
def initialize(message = nil, action = nil, subject = nil, extra_args = nil)
@message = message
@action = action
@subject = subject
@extra_args = extra_args
@default_message = I18n.t(:"unauthorized.default", :default => "You are not authorized to access this page.")
end

Expand Down
1 change: 1 addition & 0 deletions spec/cancan/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ class Container < Hash; end
expect(e.message).to eq("Access denied!")
expect(e.action).to eq(:read)
expect(e.subject).to eq(:foo)
expect(e.extra_args.first).to eq(1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add some more tests: check what is the content of extra_args in different cases (also when there is no extra_args)

else
fail "Expected CanCan::AccessDenied exception to be raised"
end
Expand Down
8 changes: 8 additions & 0 deletions spec/cancan/controller_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ class HiddenModel < ::Model; end
expect { resource.authorize_resource }.to raise_error(CanCan::AccessDenied)
end

it "authorizes with :custom_action for parent collection action" do
controller.instance_variable_set(:@category, :some_category)
allow(controller).to receive(:authorize!).with(:custom_action, :some_category) { raise CanCan::AccessDenied }

resource = CanCan::ControllerResource.new(controller, :category, :parent => true, :parent_action => :custom_action )
expect { resource.authorize_resource }.to raise_error(CanCan::AccessDenied)
end

it "has the specified nested resource_class when using / for namespace" do
module Admin
class Dashboard; end
Expand Down