Module: GraphQL::Pundit::Authorization

Included in:
Field
Defined in:
lib/graphql-pundit/authorization.rb

Overview

Authorization methods to be included in the used Field class

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



9
10
11
# File 'lib/graphql-pundit/authorization.rb', line 9

def self.prepended(base)
  base.include(GraphQL::Pundit::Common)
end

Instance Method Details

#authorize(*args, record: nil, policy: nil) ⇒ Object



28
29
30
31
32
# File 'lib/graphql-pundit/authorization.rb', line 28

def authorize(*args, record: nil, policy: nil)
  @authorize = args[0] || true
  @record = record if record
  @policy = policy if policy
end

#authorize!(*args, record: nil, policy: nil) ⇒ Object



34
35
36
37
# File 'lib/graphql-pundit/authorization.rb', line 34

def authorize!(*args, record: nil, policy: nil)
  @do_raise = true
  authorize(*args, record: record, policy: policy)
end

#initialize(*args, authorize: nil, record: nil, policy: nil, **kwargs, &block) ⇒ Object

rubocop:disable Metrics/ParameterLists



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/graphql-pundit/authorization.rb', line 14

def initialize(*args, authorize: nil,
                      record: nil,
                      policy: nil,
                      **kwargs, &block)
  # rubocop:enable Metrics/ParameterLists
  # authorize! is not a valid variable name
  authorize_bang = kwargs.delete(:authorize!)
  @record = record if record
  @policy = policy if policy
  @authorize = authorize_bang || authorize
  @do_raise = !!authorize_bang
  super(*args, **kwargs, &block)
end

#resolve_field(obj, args, ctx) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/graphql-pundit/authorization.rb', line 39

def resolve_field(obj, args, ctx)
  raise ::Pundit::NotAuthorizedError unless do_authorize(obj, args, ctx)
  super(obj, args, ctx)
rescue ::Pundit::NotAuthorizedError
  if @do_raise
    raise GraphQL::ExecutionError, "You're not authorized to do this"
  end
end