Skip to content

Commit

Permalink
Merge pull request #46 from jraqula/master
Browse files Browse the repository at this point in the history
Allow an AccessDenied error to carry context about the rejection
  • Loading branch information
pokonski authored Apr 28, 2018
2 parents 420d0d4 + cd1327f commit 66c1341
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,26 @@ class ApplicationController < ActionController::Base
end
```

You can also extract the action and subject which raised the error,
if you want to handle authorization errors differently for some cases:
```ruby
rescue_from "AccessGranted::AccessDenied" do |exception|
status = case exception.action
when :read # invocation like `authorize! :read, @something`
403
else
404
end

body = case exception.subject
when Post # invocation like `authorize! @some_action, Post`
"failed to access a post"
else
"failed to access something else"
end
end
```

#### Checking permissions in controllers

To check if the user has a permission to perform an action, use the `can?` and `cannot?` methods.
Expand Down
8 changes: 7 additions & 1 deletion lib/access-granted/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@ class Error < StandardError; end

class DuplicatePermission < Error; end;
class DuplicateRole < Error; end;
class AccessDenied < Error; end;
class AccessDenied < Error
attr_reader :action, :subject
def initialize(action = nil, subject = nil)
@action = action
@subject = subject
end
end
end
2 changes: 1 addition & 1 deletion lib/access-granted/policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def cannot?(*args)

def authorize!(action, subject)
if cannot?(action, subject)
raise AccessDenied
raise AccessDenied.new(action, subject)
end
subject
end
Expand Down
6 changes: 5 additions & 1 deletion spec/controller_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@

describe "#authorize!" do
it "raises exception when authorization fails" do
expect { @controller.authorize!(:read, String) }.to raise_error(AccessGranted::AccessDenied)
expect { @controller.authorize!(:read, String) }.to raise_error do |err|
expect(err).to be_a(AccessGranted::AccessDenied)
expect(err.action).to eq(:read)
expect(err.subject).to eq(String)
end
end

it "returns subject if authorization succeeds" do
Expand Down
6 changes: 5 additions & 1 deletion spec/policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ def configure
end

it "raises AccessDenied if action is not allowed" do
expect { klass.new(@member).authorize!(:create, Integer) }.to raise_error AccessGranted::AccessDenied
expect { klass.new(@member).authorize!(:create, Integer) }.to raise_error do |err|
expect(err).to be_a(AccessGranted::AccessDenied)
expect(err.action).to eq(:create)
expect(err.subject).to eq(Integer)
end
end

it "returns the subject if allowed" do
Expand Down

0 comments on commit 66c1341

Please sign in to comment.