1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\EventListener\GraphQL; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
14
|
|
|
use Ynlo\GraphQLBundle\Events\GraphQLEvents; |
15
|
|
|
use Ynlo\GraphQLBundle\Events\GraphQLFieldEvent; |
16
|
|
|
use Ynlo\GraphQLBundle\Events\GraphQLMutationEvent; |
17
|
|
|
use Ynlo\GraphQLBundle\Exception\Controlled\ForbiddenError; |
18
|
|
|
use Ynlo\GraphQLBundle\Security\Authorization\AccessControlChecker; |
19
|
|
|
use Ynlo\GraphQLBundle\Util\TypeUtil; |
20
|
|
|
|
21
|
|
|
class AccessControlListener implements EventSubscriberInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var AccessControlChecker |
25
|
|
|
*/ |
26
|
|
|
protected $accessControlChecker; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param AccessControlChecker $accessControlChecker |
30
|
|
|
*/ |
31
|
|
|
public function __construct(AccessControlChecker $accessControlChecker) |
32
|
|
|
{ |
33
|
|
|
$this->accessControlChecker = $accessControlChecker; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
*/ |
39
|
|
|
public static function getSubscribedEvents() |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
GraphQLEvents::PRE_READ_FIELD => 'preReadField', |
43
|
|
|
GraphQLEvents::MUTATION_SUBMITTED => 'onSubmitMutation', |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function onSubmitMutation(GraphQLMutationEvent $event) |
48
|
|
|
{ |
49
|
|
|
$operation = $event->getContext()->getDefinition(); |
50
|
|
|
if ($this->accessControlChecker->isControlled($operation) |
51
|
|
|
&& !$this->accessControlChecker->isGranted($operation, $event->getFormEvent()->getData()) |
52
|
|
|
) { |
53
|
|
|
$message = $this->accessControlChecker->getMessage($operation) ?? null; |
54
|
|
|
throw new ForbiddenError($message); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function preReadField(GraphQLFieldEvent $event) |
59
|
|
|
{ |
60
|
|
|
//check firstly if the user have rights to read the operation |
61
|
|
|
$node = $event->getContext()->getNode(); |
62
|
|
|
if ($node && $this->accessControlChecker->isControlled($node) |
63
|
|
|
&& !$this->accessControlChecker->isGranted($node, $event->getContext()->getRoot()) |
64
|
|
|
) { |
65
|
|
|
$event->stopPropagation(); |
66
|
|
|
$event->setValue(null); |
67
|
|
|
throw new ForbiddenError($this->accessControlChecker->getMessage($node)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
//check if user have rights to read the object |
71
|
|
|
if (\is_object($event->getContext()->getRoot())) { |
72
|
|
|
$concreteType = TypeUtil::resolveObjectType($event->getContext()->getEndpoint(), $event->getContext()->getRoot()); |
73
|
|
|
if ($concreteType) { |
74
|
|
|
$objectDefinition = $event->getContext()->getEndpoint()->getType($concreteType); |
75
|
|
|
if ($this->accessControlChecker->isControlled($objectDefinition) |
76
|
|
|
&& !$this->accessControlChecker->isGranted($objectDefinition, $event->getContext()->getRoot()) |
77
|
|
|
) { |
78
|
|
|
$event->stopPropagation(); |
79
|
|
|
$event->setValue(null); |
80
|
|
|
throw new ForbiddenError($this->accessControlChecker->getMessage($objectDefinition)); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
//check then if the user have rights to read the field |
86
|
|
|
$field = $event->getContext()->getDefinition(); |
87
|
|
|
if ($this->accessControlChecker->isControlled($field) |
88
|
|
|
&& !$this->accessControlChecker->isGranted($field, $event->getContext()->getRoot()) |
89
|
|
|
) { |
90
|
|
|
throw new ForbiddenError($this->accessControlChecker->getMessage($field)); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|