Class: GraphqlPundit::AuthorizationExtension

Inherits:
GraphQL::Schema::FieldExtension
  • Object
show all
Includes:
Common
Defined in:
lib/graphql_pundit/authorization_extension.rb

Overview

Authorization methods to be included in the used Field class

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#callable?, #model?, #object?

Constructor Details

#initialize(field:, options:) ⇒ AuthorizationExtension

Returns a new instance of AuthorizationExtension.



10
11
12
13
# File 'lib/graphql_pundit/authorization_extension.rb', line 10

def initialize(field:, options:)
  super
  @current_user = options[:current_user] || :current_user
end

Instance Attribute Details

#authorizeObject (readonly)

Returns the value of attribute authorize.



8
9
10
# File 'lib/graphql_pundit/authorization_extension.rb', line 8

def authorize
  @authorize
end

#policyObject (readonly)

Returns the value of attribute policy.



8
9
10
# File 'lib/graphql_pundit/authorization_extension.rb', line 8

def policy
  @policy
end

#raise_unauthorizedObject (readonly)

Returns the value of attribute raise_unauthorized.



8
9
10
# File 'lib/graphql_pundit/authorization_extension.rb', line 8

def raise_unauthorized
  @raise_unauthorized
end

#recordObject (readonly)

Returns the value of attribute record.



8
9
10
# File 'lib/graphql_pundit/authorization_extension.rb', line 8

def record
  @record
end

Instance Method Details

#applyObject



15
16
17
18
19
20
21
# File 'lib/graphql_pundit/authorization_extension.rb', line 15

def apply
  @raise_unauthorized = field.raise_unauthorized
  @authorize = field.authorize
  @record = field.record
  @policy = field.policy
  @method_sym = field.method_sym
end

#resolve(object:, arguments:, context:, **_rest) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/graphql_pundit/authorization_extension.rb', line 23

def resolve(object:, arguments:, context:, **_rest)
  # yield the current time as `memo`
  raise ::Pundit::NotAuthorizedError unless do_authorize(object, arguments, context)

  yield(object, arguments)
rescue ::Pundit::NotAuthorizedError
  raise GraphQL::ExecutionError, "You're not authorized to do this" if @raise_unauthorized
end