Completed
Pull Request — 2.1 (#1607)
by Grégoire
03:23
created

ResourceAccessCheckerTrait::canAccess()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 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
/*
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)
45
    {
46
        if (null === $resourceAccessChecker) {
47
            return;
48
        }
49
50
        $isGranted = $resourceMetadata->getGraphqlAttribute('query', 'access_control', null, true);
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