Class: Wallaby::CancancanAuthorizationProvider
- Inherits:
-
ModelAuthorizationProvider
- Object
- ModelAuthorizationProvider
- Wallaby::CancancanAuthorizationProvider
- Defined in:
- lib/authorizers/wallaby/cancancan_authorization_provider.rb
Overview
This authorization provider DOES NOT use the existing current_ability helper. It has its own version of #ability instance.
CanCanCan base authorization provider.
Instance Attribute Summary collapse
-
#ability ⇒ Ability
The Ability instance for #user #user} or from the {#options.
Attributes inherited from ModelAuthorizationProvider
Class Method Summary collapse
-
.available?(context) ⇒ true, false
Detect and see if CanCanCan is in use.
-
.options_from(context) ⇒ Hash
Get the information from context for ModelAuthorizationProvider#initialize.
Instance Method Summary collapse
-
#accessible_for(action, scope) ⇒ Object
Restrict user to access certain scope/query.
-
#attributes_for(action, subject) ⇒ Object
Restrict user to assign certain values.
-
#authorize(action, subject) ⇒ Object
Check user’s permission for an action on given subject.
-
#authorized?(action, subject) ⇒ true, false
Check and see if user is allowed to perform an action on given subject.
-
#permit_params(action, subject) ⇒ nil
Simply return nil as CanCanCan doesn’t provide such a feature.
Methods inherited from ModelAuthorizationProvider
#initialize, #unauthorized?, #user
Constructor Details
This class inherits a constructor from Wallaby::ModelAuthorizationProvider
Instance Attribute Details
#ability ⇒ Ability
Returns the Ability instance for #user #user} or from the {#options.
32 33 34 35 36 |
# File 'lib/authorizers/wallaby/cancancan_authorization_provider.rb', line 32 def ability # NOTE: use current_ability's class to create the ability instance. # just in case that developer uses a different Ability class (e.g. UserAbility) @ability ||= [:ability] || Ability.new(user) end |
Class Method Details
.available?(context) ⇒ true, false
Detect and see if CanCanCan is in use.
13 14 15 |
# File 'lib/authorizers/wallaby/cancancan_authorization_provider.rb', line 13 def self.available?(context) defined?(CanCanCan) && context.respond_to?(:current_ability) end |
.options_from(context) ⇒ Hash
Get the information from context for ModelAuthorizationProvider#initialize
20 21 22 23 24 25 |
# File 'lib/authorizers/wallaby/cancancan_authorization_provider.rb', line 20 def self.(context) { ability: context.try(:current_ability), user: context.try(:wallaby_user) } end |
Instance Method Details
#accessible_for(action, scope) ⇒ Object
Restrict user to access certain scope/query.
66 67 68 |
# File 'lib/authorizers/wallaby/cancancan_authorization_provider.rb', line 66 def accessible_for(action, scope) scope.try(:accessible_by, ability, action) || scope end |
#attributes_for(action, subject) ⇒ Object
Restrict user to assign certain values.
75 |
# File 'lib/authorizers/wallaby/cancancan_authorization_provider.rb', line 75 delegate :attributes_for, to: :ability |
#authorize(action, subject) ⇒ Object
Check user’s permission for an action on given subject.
This method will be mostly used in controller.
44 45 46 47 48 49 50 51 |
# File 'lib/authorizers/wallaby/cancancan_authorization_provider.rb', line 44 def (action, subject) ability. action, subject rescue ::CanCan::AccessDenied Logger.error <<~MESSAGE #{Utils.inspect user} is forbidden to perform #{action} on #{Utils.inspect subject} MESSAGE raise Forbidden end |
#authorized?(action, subject) ⇒ true, false
Check and see if user is allowed to perform an action on given subject.
58 59 60 |
# File 'lib/authorizers/wallaby/cancancan_authorization_provider.rb', line 58 def (action, subject) ability.can? action, subject end |
#permit_params(action, subject) ⇒ nil
Simply return nil as CanCanCan doesn’t provide such a feature.
81 82 83 |
# File 'lib/authorizers/wallaby/cancancan_authorization_provider.rb', line 81 def permit_params(action, subject) # Do nothing end |