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\Registry\Endpoint; |
18
|
|
|
|
19
|
|
|
class RolesDefinitionExtension extends AbstractDefinitionExtension |
20
|
|
|
{ |
21
|
|
|
private $authorizationChecker; |
22
|
|
|
|
23
|
22 |
|
public function __construct(AuthorizationCheckerInterface $authorizationChecker) |
24
|
|
|
{ |
25
|
22 |
|
$this->authorizationChecker = $authorizationChecker; |
26
|
22 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritDoc} |
30
|
|
|
*/ |
31
|
22 |
|
public function buildConfig(ArrayNodeDefinition $root): void |
32
|
|
|
{ |
33
|
|
|
$root |
34
|
22 |
|
->info('List of roles for queries and mutations') |
35
|
22 |
|
->prototype('scalar') |
36
|
22 |
|
->end(); |
37
|
22 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
22 |
|
public function configureEndpoint(Endpoint $endpoint): void |
43
|
|
|
{ |
44
|
22 |
|
$endpoint->setQueries($this->secureDefinitions($endpoint->allQueries(), $endpoint)); |
45
|
22 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ExecutableDefinitionInterface[] $definitions |
49
|
|
|
* @param Endpoint $endpoint |
50
|
|
|
* |
51
|
|
|
* @return ExecutableDefinitionInterface[] |
52
|
|
|
*/ |
53
|
22 |
|
private function secureDefinitions(array $definitions, Endpoint $endpoint): array |
|
|
|
|
54
|
|
|
{ |
55
|
22 |
|
$secureDefinitions = []; |
56
|
22 |
|
foreach ($definitions as $definition) { |
57
|
22 |
|
if ($roles = $definition->getRoles()) { |
|
|
|
|
58
|
|
|
if ($this->authorizationChecker->isGranted($roles)) { |
59
|
|
|
$secureDefinitions[] = $definition; |
60
|
|
|
} |
61
|
|
|
} else { |
62
|
22 |
|
$secureDefinitions[] = $definition; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
22 |
|
return $secureDefinitions; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.