Module: Pragma::Operation::Authorization::InstanceMethods
- Defined in:
- lib/pragma/operation/authorization.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#authorize(authorizable) ⇒ Boolean
Authorizes this operation on the provided resource or policy.
-
#authorize!(authorizable) ⇒ Object
Authorizes this operation on the provided resource or policy.
-
#authorize_collection(collection) ⇒ Pragma::Decorator::Base|Enumerable
Scopes the provided collection.
-
#build_policy(resource) ⇒ Pragma::Policy::Base
Builds the policy for the current user and the given resource, using the previously defined policy class.
Instance Method Details
#authorize(authorizable) ⇒ Boolean
Authorizes this operation on the provided resource or policy.
If no policy was defined, simply returns true.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/pragma/operation/authorization.rb', line 63 def () return true unless self.class.policy_klass policy = if self.class.policy_klass && .is_a?(self.class.policy_klass) else build_policy() end params.each_pair do |name, value| next unless policy.resource.respond_to?("#{name}=") policy.resource.send("#{name}=", value) end policy.send("#{self.class.operation_name}?") end |
#authorize!(authorizable) ⇒ Object
Authorizes this operation on the provided resource or policy. If the user is not authorized to perform the operation, responds with 403 Forbidden and an error body and halts the execution.
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/pragma/operation/authorization.rb', line 85 def () return if () respond_with!( status: :forbidden, resource: { error_type: :forbidden, error_message: 'You are not authorized to perform this operation.' } ) end |
#authorize_collection(collection) ⇒ Pragma::Decorator::Base|Enumerable
Scopes the provided collection.
If no policy class is defined, simply returns the collection.
104 105 106 107 108 109 110 111 |
# File 'lib/pragma/operation/authorization.rb', line 104 def (collection) return collection unless self.class.policy_klass self.class.policy_klass.accessible_by( user: current_user, scope: collection ) end |
#build_policy(resource) ⇒ Pragma::Policy::Base
Builds the policy for the current user and the given resource, using the previously defined policy class.
52 53 54 |
# File 'lib/pragma/operation/authorization.rb', line 52 def build_policy(resource) self.class.build_policy(user: current_user, resource: resource) end |