Passed
Pull Request — master (#7)
by Yonel Ceruto
06:50
created

RolesDefinitionExtension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 57
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A buildConfig() 0 6 1
C secureDefinitions() 0 22 8
A configureEndpoint() 0 4 1
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)) {
0 ignored issues
show
Bug introduced by
The method getRoles() does not exist on Ynlo\GraphQLBundle\Defin...ableDefinitionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Ynlo\GraphQLBundle\Defin...ableDefinitionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
            if (($roles = $definition->/** @scrutinizer ignore-call */ getRoles()) && !$this->authorizationChecker->isGranted($roles)) {
Loading history...
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