Passed
Pull Request — 2.1 (#1729)
by Antoine
04:12 queued 01:10
created

ResourceAccessCheckerTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 19
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A canAccess() 0 12 4
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
/*
15
 * This file is part of the API Platform project.
16
 *
17
 * (c) Kévin Dunglas <[email protected]>
18
 *
19
 * For the full copyright and license information, please view the LICENSE
20
 * file that was distributed with this source code.
21
 */
22
23
namespace ApiPlatform\Core\GraphQl\Resolver;
24
25
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
26
use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
27
use GraphQL\Error\Error;
28
use GraphQL\Type\Definition\ResolveInfo;
29
30
/**
31
 * Checks if the current logged in user can access to this resource.
32
 *
33
 * @experimental
34
 *
35
 * @author Kévin Dunglas <[email protected]>
36
 */
37
trait ResourceAccessCheckerTrait
38
{
39
    /**
40
     * @param object $object
41
     *
42
     * @throws Error
43
     */
44
    public function canAccess(ResourceAccessCheckerInterface $resourceAccessChecker = null, ResourceMetadata $resourceMetadata, string $resourceClass, ResolveInfo $info, $object = null, string $operationName = null)
45
    {
46
        if (null === $resourceAccessChecker) {
47
            return;
48
        }
49
50
        $isGranted = $resourceMetadata->getGraphqlAttribute($operationName, 'access_control', null, true);
0 ignored issues
show
Bug introduced by
It seems like $operationName can also be of type null; however, parameter $operationName of ApiPlatform\Core\Metadat...::getGraphqlAttribute() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

50
        $isGranted = $resourceMetadata->getGraphqlAttribute(/** @scrutinizer ignore-type */ $operationName, 'access_control', null, true);
Loading history...
51
        if (null === $isGranted || $resourceAccessChecker->isGranted($resourceClass, $isGranted, ['object' => $object])) {
52
            return;
53
        }
54
55
        throw Error::createLocatedError('Access Denied.', $info->fieldNodes, $info->path);
56
    }
57
}
58