Class: Policy::Base
- Inherits:
-
Object
- Object
- Policy::Base
- Extended by:
- Role
- Includes:
- PolicyDefaults
- Defined in:
- lib/pundit_roles/policy/base.rb
Overview
Base policy class to be extended by all other policies, authorizes users based on roles they fall into, return a uniquely merged hash of permitted attributes and associations of each role the @user has.
Defined Under Namespace
Classes: Scope
Constant Summary
Constants included from PolicyDefaults
PolicyDefaults::RESTRICTED_CREATE_ASSOCIATIONS, PolicyDefaults::RESTRICTED_CREATE_ATTRIBUTES, PolicyDefaults::RESTRICTED_SAVE_ASSOCIATIONS, PolicyDefaults::RESTRICTED_SAVE_ATTRIBUTES, PolicyDefaults::RESTRICTED_SHOW_ASSOCIATIONS, PolicyDefaults::RESTRICTED_SHOW_ATTRIBUTES, PolicyDefaults::RESTRICTED_UPDATE_ASSOCIATIONS, PolicyDefaults::RESTRICTED_UPDATE_ATTRIBUTES
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
the object we’re checking permissions of.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#user ⇒ Object
readonly
the user that initiated the action.
Attributes included from Role
Instance Method Summary collapse
-
#initialize(user, resource) ⇒ Base
constructor
A new instance of Base.
-
#resolve_query(query) ⇒ Object
Retrieves the permitted roles for the current query, checks if user is one or more of these roles and return a hash of attributes and associations that the user has access to.
Methods included from Role
Methods included from PolicyDefaults
#create?, #destroy?, #index?, #show?, #update?
Constructor Details
#initialize(user, resource) ⇒ Base
Returns a new instance of Base.
17 18 19 20 |
# File 'lib/pundit_roles/policy/base.rb', line 17 def initialize(user, resource) @user = user @resource = resource end |
Instance Attribute Details
#record ⇒ Object (readonly)
the object we’re checking permissions of
12 13 14 |
# File 'lib/pundit_roles/policy/base.rb', line 12 def record @record end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
16 17 18 |
# File 'lib/pundit_roles/policy/base.rb', line 16 def resource @resource end |
#user ⇒ Object (readonly)
the user that initiated the action
12 13 14 |
# File 'lib/pundit_roles/policy/base.rb', line 12 def user @user end |
Instance Method Details
#resolve_query(query) ⇒ Object
Retrieves the permitted roles for the current query, checks if user is one or more of these roles and return a hash of attributes and associations that the user has access to.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/pundit_roles/policy/base.rb', line 26 def resolve_query(query) permitted_roles = public_send(query) if permitted_roles.is_a? TrueClass or permitted_roles.is_a? FalseClass return permitted_roles end = self.class. # Always checks if user is a guest, and return the appropriate permission if true # the guest role cannot be merged with other roles if guest? return handle_guest_user(permitted_roles, ) end current_roles = determine_current_roles(permitted_roles, ) unless current_roles.present? return false end if current_roles.length == 1 return current_roles.values[0].merge({roles: [current_roles.keys[0]]}) end return unique_merge(current_roles) end |