1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/******************************************************************************* |
4
|
|
|
* This file is part of the GraphQL Bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) YnloUltratech <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
******************************************************************************/ |
11
|
|
|
|
12
|
|
|
namespace Ynlo\GraphQLBundle\Definition\Extension; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
15
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\ExecutableDefinitionInterface; |
17
|
|
|
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface; |
18
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
19
|
|
|
|
20
|
|
|
class RolesDefinitionExtension extends AbstractDefinitionExtension |
21
|
|
|
{ |
22
|
|
|
private $authorizationChecker; |
23
|
|
|
|
24
|
|
|
public function __construct(AuthorizationCheckerInterface $authorizationChecker) |
25
|
|
|
{ |
26
|
|
|
$this->authorizationChecker = $authorizationChecker; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritDoc} |
31
|
|
|
*/ |
32
|
|
|
public function buildConfig(ArrayNodeDefinition $root): void |
33
|
|
|
{ |
34
|
|
|
$root |
35
|
|
|
->info('List of roles for queries and mutations') |
36
|
|
|
->prototype('scalar') |
37
|
|
|
->end(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
|
|
public function configureEndpoint(Endpoint $endpoint): void |
44
|
|
|
{ |
45
|
|
|
$endpoint->setQueries($this->secureDefinitions($endpoint->allQueries(), $endpoint)); |
46
|
|
|
$endpoint->setMutations($this->secureDefinitions($endpoint->allMutations(), $endpoint)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param ExecutableDefinitionInterface[] $definitions |
51
|
|
|
* @param Endpoint $endpoint |
52
|
|
|
* |
53
|
|
|
* @return ExecutableDefinitionInterface[] |
54
|
|
|
*/ |
55
|
|
|
private function secureDefinitions(array $definitions, Endpoint $endpoint): array |
56
|
|
|
{ |
57
|
|
|
$secureDefinitions = []; |
58
|
|
|
foreach ($definitions as $definition) { |
59
|
|
|
if (($roles = $definition->getRoles()) && !$this->authorizationChecker->isGranted($roles)) { |
|
|
|
|
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$secureDefinitions[] = $definition; |
64
|
|
|
|
65
|
|
|
/** @var FieldsAwareDefinitionInterface $type */ |
66
|
|
|
$type = $endpoint->getType($definition->getType()); |
67
|
|
|
if ($fields = $type->getFields()) { |
68
|
|
|
foreach ($fields as $fieldDefinition) { |
69
|
|
|
if (($roles = $fieldDefinition->getRoles()) && !$this->authorizationChecker->isGranted($roles)) { |
70
|
|
|
$type->removeField($fieldDefinition->getName()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $secureDefinitions; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|