Class: Wallaby::PunditAuthorizationProvider
- Inherits:
-
ModelAuthorizationProvider
- Object
- ModelAuthorizationProvider
- Wallaby::PunditAuthorizationProvider
- Defined in:
- lib/authorizers/wallaby/pundit_authorization_provider.rb
Overview
Pundit base authorization provider.
Instance Attribute Summary
Attributes inherited from ModelAuthorizationProvider
Class Method Summary collapse
-
.available?(context) ⇒ true, false
Detect and see if Pundit 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) ⇒ Hash
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) ⇒ Array
Restrict user for mass assignment.
Methods inherited from ModelAuthorizationProvider
#initialize, #unauthorized?, #user
Constructor Details
This class inherits a constructor from Wallaby::ModelAuthorizationProvider
Class Method Details
.available?(context) ⇒ true, false
Detect and see if Pundit is in use.
10 11 12 |
# File 'lib/authorizers/wallaby/pundit_authorization_provider.rb', line 10 def self.available?(context) defined?(Pundit) && context.respond_to?(:pundit_user) end |
.options_from(context) ⇒ Hash
Get the information from context for ModelAuthorizationProvider#initialize
17 18 19 20 21 |
# File 'lib/authorizers/wallaby/pundit_authorization_provider.rb', line 17 def self.(context) { user: context.try(:pundit_user) || context.try(:wallaby_user) } end |
Instance Method Details
#accessible_for(_action, scope) ⇒ Object
Restrict user to access certain scope/query.
52 53 54 |
# File 'lib/authorizers/wallaby/pundit_authorization_provider.rb', line 52 def accessible_for(_action, scope) Pundit.policy_scope!(user, scope) end |
#attributes_for(action, subject) ⇒ Hash
Restrict user to assign certain values.
It will do a lookup in policy’s methods and pick the first available method:
-
‘attributes_for_#action`
-
‘attributes_for`
65 66 67 68 |
# File 'lib/authorizers/wallaby/pundit_authorization_provider.rb', line 65 def attributes_for(action, subject) policy = Pundit.policy!(user, subject) policy.try("attributes_for_#{action}") || policy.try('attributes_for') || {} end |
#authorize(action, subject) ⇒ Object
Check user’s permission for an action on given subject.
This method is mostly used in controller.
29 30 31 32 33 34 35 36 |
# File 'lib/authorizers/wallaby/pundit_authorization_provider.rb', line 29 def (action, subject) Pundit.(user, subject, normalize(action)) && subject rescue ::Pundit::NotAuthorizedError 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
43 44 45 46 |
# File 'lib/authorizers/wallaby/pundit_authorization_provider.rb', line 43 def (action, subject) policy = Pundit.policy!(user, subject) policy.try normalize(action) end |
#permit_params(action, subject) ⇒ Array
Restrict user for mass assignment.
It will do a lookup in policy’s methods and pick the first available method:
-
‘permitted_attributes_for_#{ action }`
-
‘permitted_attributes`
79 80 81 82 83 |
# File 'lib/authorizers/wallaby/pundit_authorization_provider.rb', line 79 def permit_params(action, subject) policy = Pundit.policy!(user, subject) # @see https://github.com/varvet/pundit/blob/master/lib/pundit.rb#L258 policy.try("permitted_attributes_for_#{action}") || policy.try('permitted_attributes') end |