Exception: CanCan::AccessDenied
- Defined in:
- lib/cancan/exceptions.rb
Overview
This error is raised when a user isn’t allowed to access a given controller action. This usually happens within a call to ControllerAdditions#authorize! 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. # => "Not authorized!"
exception.action # => :read
exception.subject # => Article
If the message is not specified (or is nil) it will default to “You are not authorized to access this page.” This default can be overridden by setting default_message.
exception. = "Default error message"
exception. # => "Default error message"
See ControllerAdditions#authorize! for more information on rescuing from this exception and customizing the message using I18n.
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#default_message ⇒ Object
writeonly
Sets the attribute default_message.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
Instance Method Summary collapse
-
#initialize(message = nil, action = nil, subject = nil, conditions = nil) ⇒ AccessDenied
constructor
A new instance of AccessDenied.
- #inspect ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(message = nil, action = nil, subject = nil, conditions = nil) ⇒ AccessDenied
Returns a new instance of AccessDenied.
50 51 52 53 54 55 56 |
# File 'lib/cancan/exceptions.rb', line 50 def initialize( = nil, action = nil, subject = nil, conditions = nil) @message = @action = action @subject = subject @conditions = conditions @default_message = I18n.t(:"unauthorized.default", default: 'You are not authorized to access this page.') end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
47 48 49 |
# File 'lib/cancan/exceptions.rb', line 47 def action @action end |
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
47 48 49 |
# File 'lib/cancan/exceptions.rb', line 47 def conditions @conditions end |
#default_message=(value) ⇒ Object (writeonly)
Sets the attribute default_message
48 49 50 |
# File 'lib/cancan/exceptions.rb', line 48 def (value) @default_message = value end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
47 48 49 |
# File 'lib/cancan/exceptions.rb', line 47 def subject @subject end |
Instance Method Details
#inspect ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/cancan/exceptions.rb', line 62 def inspect details = %i[action subject conditions message].map do |attribute| value = instance_variable_get "@#{attribute}" "#{attribute}: #{value.inspect}" if value.present? end.compact.join(', ') "#<#{self.class.name} #{details}>" end |
#to_s ⇒ Object
58 59 60 |
# File 'lib/cancan/exceptions.rb', line 58 def to_s @message || @default_message end |