Completed
Push — master ( 3c98fd...23d0f8 )
by Kévin
24s queued 12s
created

SecurityPostDenormalizeStage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 20 5
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[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
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\GraphQl\Resolver\Stage;
15
16
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
17
use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
18
use GraphQL\Error\Error;
19
use GraphQL\Type\Definition\ResolveInfo;
20
21
/**
22
 * Security post denormalize stage of GraphQL resolvers.
23
 *
24
 * @experimental
25
 *
26
 * @author Vincent Chalamon <[email protected]>
27
 */
28
final class SecurityPostDenormalizeStage implements SecurityPostDenormalizeStageInterface
29
{
30
    private $resourceMetadataFactory;
31
    private $resourceAccessChecker;
32
33
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, ResourceAccessCheckerInterface $resourceAccessChecker)
34
    {
35
        $this->resourceMetadataFactory = $resourceMetadataFactory;
36
        $this->resourceAccessChecker = $resourceAccessChecker;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function __invoke(string $resourceClass, string $operationName, array $context): void
43
    {
44
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
45
46
        $isGranted = $resourceMetadata->getGraphqlAttribute($operationName, 'security_post_denormalize', null, true);
47
        if (null === $isGranted) {
48
            // Backward compatibility
49
            $isGranted = $resourceMetadata->getGraphqlAttribute($operationName, 'access_control', null, true);
50
            if (null !== $isGranted) {
51
                @trigger_error('Attribute "access_control" is deprecated since API Platform 2.5, prefer using "security" attribute instead', E_USER_DEPRECATED);
52
            }
53
        }
54
55
        if (null === $isGranted || $this->resourceAccessChecker->isGranted($resourceClass, (string) $isGranted, $context['extra_variables'])) {
56
            return;
57
        }
58
59
        /** @var ResolveInfo $info */
60
        $info = $context['info'];
61
        throw Error::createLocatedError($resourceMetadata->getGraphqlAttribute($operationName, 'security_post_denormalize_message', 'Access Denied.'), $info->fieldNodes, $info->path);
62
    }
63
}
64