1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Definition\ConfigProcessor; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
6
|
|
|
use Overblog\GraphQLBundle\Definition\LazyConfig; |
7
|
|
|
use Overblog\GraphQLBundle\Error\UserWarning; |
8
|
|
|
use Overblog\GraphQLBundle\Resolver\AccessResolver; |
9
|
|
|
|
10
|
|
|
final class AclConfigProcessor implements ConfigProcessorInterface |
11
|
|
|
{ |
12
|
|
|
/** @var AccessResolver */ |
13
|
|
|
private $accessResolver; |
14
|
|
|
|
15
|
|
|
/** @var callable */ |
16
|
|
|
private $defaultResolver; |
17
|
|
|
|
18
|
88 |
|
public function __construct(AccessResolver $accessResolver, callable $defaultResolver) |
19
|
|
|
{ |
20
|
88 |
|
$this->accessResolver = $accessResolver; |
21
|
88 |
|
$this->defaultResolver = $defaultResolver; |
22
|
88 |
|
} |
23
|
|
|
|
24
|
|
|
public static function acl(array $fields, AccessResolver $accessResolver, callable $defaultResolver) |
25
|
|
|
{ |
26
|
78 |
|
$deniedAccess = static function () { |
27
|
1 |
|
throw new UserWarning('Access denied to this field.'); |
28
|
78 |
|
}; |
29
|
78 |
|
foreach ($fields as &$field) { |
30
|
78 |
|
if (isset($field['access']) && true !== $field['access']) { |
31
|
11 |
|
$accessChecker = $field['access']; |
32
|
11 |
|
if (false === $accessChecker) { |
33
|
10 |
|
$field['resolve'] = $deniedAccess; |
34
|
11 |
|
} elseif (is_callable($accessChecker)) { |
35
|
78 |
|
$field['resolve'] = function ($value, $args, $context, ResolveInfo $info) use ($field, $accessChecker, $accessResolver, $defaultResolver) { |
36
|
10 |
|
$resolverCallback = self::findFieldResolver($field, $info, $defaultResolver); |
37
|
10 |
|
$isMutation = 'mutation' === $info->operation->operation && $info->parentType === $info->schema->getMutationType(); |
38
|
|
|
|
39
|
10 |
|
return $accessResolver->resolve($accessChecker, $resolverCallback, [$value, $args, $context, $info], $isMutation); |
40
|
78 |
|
}; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
78 |
|
return $fields; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function process(LazyConfig $lazyConfig) |
|
|
|
|
49
|
|
|
{ |
50
|
88 |
|
$lazyConfig->addPostLoader(function ($config) { |
51
|
88 |
|
if (isset($config['fields']) && is_callable($config['fields'])) { |
52
|
78 |
|
$config['fields'] = function () use ($config) { |
53
|
78 |
|
$fields = $config['fields'](); |
54
|
|
|
|
55
|
78 |
|
return static::acl($fields, $this->accessResolver, $this->defaultResolver); |
56
|
|
|
}; |
57
|
|
|
} |
58
|
|
|
|
59
|
88 |
|
return $config; |
60
|
88 |
|
}); |
61
|
|
|
|
62
|
88 |
|
return $lazyConfig; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $field |
67
|
|
|
* @param ResolveInfo $info |
68
|
|
|
* @param callable $defaultResolver |
69
|
|
|
* |
70
|
|
|
* @return callable |
71
|
|
|
*/ |
72
|
10 |
|
private static function findFieldResolver(array $field, ResolveInfo $info, callable $defaultResolver) |
73
|
|
|
{ |
74
|
10 |
|
if (isset($field['resolve'])) { |
75
|
7 |
|
$resolver = $field['resolve']; |
76
|
6 |
|
} elseif (isset($info->parentType->config['resolveField'])) { |
77
|
1 |
|
$resolver = $info->parentType->config['resolveField']; |
78
|
|
|
} else { |
79
|
5 |
|
$resolver = $defaultResolver; |
80
|
|
|
} |
81
|
|
|
|
82
|
10 |
|
return $resolver; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.