Skip to content

Commit

Permalink
added more info to AccessDenied exception
Browse files Browse the repository at this point in the history
  • Loading branch information
dnagir committed Jan 6, 2012
1 parent bb5df11 commit 950c6db
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/allowy.rb
Expand Up @@ -5,7 +5,16 @@
require "allowy/controller_extensions"

module Allowy
class UndefinedActionError < StandardError; end
class UndefinedAccessControlError < StandardError; end
class AccessDenied < StandardError; end
class UndefinedActionError < StandardError; end

class AccessDenied < StandardError
attr_reader :action, :subject

def initialize(message, action, subject)
@message = message
@action = action
@subject = subject
end
end
end
2 changes: 1 addition & 1 deletion lib/allowy/access_control.rb
Expand Up @@ -21,7 +21,7 @@ def cannot?(*args)
end

def authorize!(*args)
raise AccessDenied unless can?(*args)
raise AccessDenied.new("Not authorized", args.first, args[1]) unless can?(*args)
end
end

Expand Down
7 changes: 6 additions & 1 deletion spec/access_control_spec.rb
Expand Up @@ -30,8 +30,13 @@ module Allowy

describe "#authorize!" do
it "shuold raise error" do
expect { subject.authorize! :read, 'deny' }.to raise_error AccessDenied
expect { subject.authorize! :read, 'deny' }.to raise_error AccessDenied do |err|
err.message.should_not be_blank
err.action.should == :read
err.subject.should == 'deny'
end
end

it "should not raise error" do
expect { subject.authorize! :read, 'allow' }.not_to raise_error AccessDenied
end
Expand Down

0 comments on commit 950c6db

Please sign in to comment.