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\DefinitionInterface; |
17
|
|
|
use Ynlo\GraphQLBundle\Definition\ExecutableDefinitionInterface; |
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
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param ExecutableDefinitionInterface[] $definitions |
50
|
|
|
* @param Endpoint $endpoint |
51
|
|
|
* |
52
|
|
|
* @return ExecutableDefinitionInterface[] |
53
|
|
|
*/ |
54
|
|
|
private function secureDefinitions(array $definitions, Endpoint $endpoint): array |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$secureDefinitions = []; |
57
|
|
|
foreach ($definitions as $definition) { |
58
|
|
|
if ($roles = $definition->getRoles()) { |
|
|
|
|
59
|
|
|
if ($this->authorizationChecker->isGranted($roles)) { |
60
|
|
|
$secureDefinitions[] = $definition; |
61
|
|
|
} |
62
|
|
|
} else { |
63
|
|
|
$secureDefinitions[] = $definition; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $secureDefinitions; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.