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); |
|
|
|
|
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
|
|
|
|