This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 783
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removing unauthorized! in favor of authorize! and including more info…
…rmation in AccessDenied exception - closes #40
- Loading branch information
Showing
12 changed files
with
152 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,6 @@ | ||
module CanCan | ||
# A general CanCan exception | ||
class Error < StandardError; end | ||
|
||
# This error is raised when a user isn't allowed to access a given | ||
# controller action. See ControllerAdditions#unauthorized! for details. | ||
class AccessDenied < Error; end | ||
end | ||
|
||
require 'cancan/ability' | ||
require 'cancan/controller_resource' | ||
require 'cancan/resource_authorization' | ||
require 'cancan/controller_additions' | ||
require 'cancan/active_record_additions' | ||
require 'cancan/exceptions' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module CanCan | ||
# A general CanCan exception | ||
class Error < StandardError; end | ||
|
||
# Raised when removed code is called, an alternative solution is provided in message. | ||
class ImplementationRemoved < Error; end | ||
|
||
# This error is raised when a user isn't allowed to access a given controller action. | ||
# This usually happens within a call to ControllerAdditions#authorized! but can be | ||
# raised manually. | ||
# | ||
# raise CanCan::AccessDenied.new("Not authorized!", :read, Article) | ||
# | ||
# The passed message, action, and subject are optional and can later be retrieved when | ||
# rescuing from the exception. | ||
# | ||
# exception.message # => "Not authorized!" | ||
# exception.action # => :read | ||
# exception.subject # => Article | ||
# | ||
# If the message is not specified (or is nil) it will default to "You are anot authorized | ||
# to access this page." This default can be overridden by setting default_message. | ||
# | ||
# exception.default_message = "Default error message" | ||
# exception.message # => "Default error message" | ||
# | ||
# See ControllerAdditions#authorized! for more information on rescuing from this exception. | ||
class AccessDenied < Error | ||
attr_reader :action, :subject | ||
attr_writer :default_message | ||
|
||
def initialize(message = nil, action = nil, subject = nil) | ||
@message = message | ||
@action = action | ||
@subject = subject | ||
@default_message = "You are not authorized to access this page." | ||
end | ||
|
||
def to_s | ||
@message || @default_message | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
Spec::Matchers.define :be_able_to do |*args| | ||
match do |model| | ||
model.can?(*args) | ||
match do |ability| | ||
ability.can?(*args) | ||
end | ||
|
||
failure_message_for_should do |model| | ||
failure_message_for_should do |ability| | ||
"expected to be able to #{args.map(&:inspect).join(" ")}" | ||
end | ||
|
||
failure_message_for_should_not do |model| | ||
failure_message_for_should_not do |ability| | ||
"expected not to be able to #{args.map(&:inspect).join(" ")}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require "spec_helper" | ||
|
||
describe CanCan::AccessDenied do | ||
describe "with action and subject" do | ||
before(:each) do | ||
@exception = CanCan::AccessDenied.new(nil, :some_action, :some_subject) | ||
end | ||
|
||
it "should have action and subject accessors" do | ||
@exception.action.should == :some_action | ||
@exception.subject.should == :some_subject | ||
end | ||
|
||
it "should have a changable default message" do | ||
@exception.message.should == "You are not authorized to access this page." | ||
@exception.default_message = "Unauthorized!" | ||
@exception.message.should == "Unauthorized!" | ||
end | ||
end | ||
|
||
describe "with only a message" do | ||
before(:each) do | ||
@exception = CanCan::AccessDenied.new("Access denied!") | ||
end | ||
|
||
it "should have nil action and subject" do | ||
@exception.action.should be_nil | ||
@exception.subject.should be_nil | ||
end | ||
|
||
it "should have passed message" do | ||
@exception.message.should == "Access denied!" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters