Passed
Push — improve-filter-message ( b945de...00229f )
by Han Hui
04:37
created

ResourceAccessCheckerTrait::canAccess()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 6
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;
15
16
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
17
use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
18
use GraphQL\Error\Error;
19
use GraphQL\Type\Definition\ResolveInfo;
20
21
/**
22
 * Checks if the current logged in user can access to this resource.
23
 *
24
 * @experimental
25
 *
26
 * @author Kévin Dunglas <[email protected]>
27
 */
28
trait ResourceAccessCheckerTrait
29
{
30
    /**
31
     * @throws Error
32
     */
33
    public function canAccess(?ResourceAccessCheckerInterface $resourceAccessChecker, ResourceMetadata $resourceMetadata, string $resourceClass, ResolveInfo $info, $extraVariables = [], string $operationName = null): void
34
    {
35
        if (null === $resourceAccessChecker) {
36
            return;
37
        }
38
39
        $isGranted = $resourceMetadata->getGraphqlAttribute($operationName ?? '', 'access_control', null, true);
40
        if (null === $isGranted || $resourceAccessChecker->isGranted($resourceClass, $isGranted, $extraVariables)) {
41
            return;
42
        }
43
44
        throw Error::createLocatedError($resourceMetadata->getGraphqlAttribute($operationName ?? '', 'access_control_message', 'Access Denied.'), $info->fieldNodes, $info->path);
45
    }
46
}
47