1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
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
|
|
|
namespace Overblog\GraphQLBundle\Resolver; |
13
|
|
|
|
14
|
|
|
use GraphQL\Executor\Promise\Adapter\SyncPromise; |
15
|
|
|
use GraphQL\Executor\Promise\Promise; |
16
|
|
|
use GraphQL\Executor\Promise\PromiseAdapter; |
17
|
|
|
use Overblog\GraphQLBundle\Error\UserError; |
18
|
|
|
use Overblog\GraphQLBundle\Error\UserWarning; |
19
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection; |
20
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\Edge; |
21
|
|
|
|
22
|
|
|
class AccessResolver |
23
|
|
|
{ |
24
|
|
|
/** @var PromiseAdapter */ |
25
|
|
|
private $promiseAdapter; |
26
|
|
|
|
27
|
|
|
public function __construct(PromiseAdapter $promiseAdapter) |
28
|
|
|
{ |
29
|
|
|
$this->promiseAdapter = $promiseAdapter; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function resolve(callable $accessChecker, callable $resolveCallback, array $resolveArgs = [], $isMutation = false) |
33
|
|
|
{ |
34
|
|
|
// operation is mutation and is mutation field |
35
|
|
|
if ($isMutation) { |
36
|
|
|
if (!$this->hasAccess($accessChecker, null, $resolveArgs)) { |
37
|
|
|
throw new UserError('Access denied to this field.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$result = call_user_func_array($resolveCallback, $resolveArgs); |
41
|
|
|
} else { |
42
|
|
|
$result = $this->filterResultUsingAccess($accessChecker, $resolveCallback, $resolveArgs); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $result; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function filterResultUsingAccess(callable $accessChecker, callable $resolveCallback, array $resolveArgs = []) |
49
|
|
|
{ |
50
|
|
|
$result = call_user_func_array($resolveCallback, $resolveArgs); |
51
|
|
|
if ($result instanceof Promise) { |
52
|
|
|
$result = $result->adoptedPromise; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($this->promiseAdapter->isThenable($result) || $result instanceof SyncPromise) { |
56
|
|
|
return $this->promiseAdapter->then( |
57
|
|
|
new Promise($result, $this->promiseAdapter), |
58
|
|
|
function ($result) use ($accessChecker, $resolveArgs) { |
59
|
|
|
return $this->processFilter($result, $accessChecker, $resolveArgs); |
60
|
|
|
} |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->processFilter($result, $accessChecker, $resolveArgs); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function processFilter($result, $accessChecker, $resolveArgs) |
68
|
|
|
{ |
69
|
|
|
if (is_array($result)) { |
70
|
|
|
$result = array_map( |
71
|
|
|
function ($object) use ($accessChecker, $resolveArgs) { |
72
|
|
|
return $this->hasAccess($accessChecker, $object, $resolveArgs) ? $object : null; |
73
|
|
|
}, |
74
|
|
|
$result |
75
|
|
|
); |
76
|
|
|
} elseif ($result instanceof Connection) { |
77
|
|
|
$result->edges = array_map( |
78
|
|
|
function (Edge $edge) use ($accessChecker, $resolveArgs) { |
79
|
|
|
$edge->node = $this->hasAccess($accessChecker, $edge->node, $resolveArgs) ? $edge->node : null; |
80
|
|
|
|
81
|
|
|
return $edge; |
82
|
|
|
}, |
83
|
|
|
$result->edges |
84
|
|
|
); |
85
|
|
|
} elseif (!$this->hasAccess($accessChecker, $result, $resolveArgs)) { |
86
|
|
|
throw new UserWarning('Access denied to this field.'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $result; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function hasAccess(callable $accessChecker, $object, array $resolveArgs = []) |
93
|
|
|
{ |
94
|
|
|
$resolveArgs[] = $object; |
95
|
|
|
$access = (bool) call_user_func_array($accessChecker, $resolveArgs); |
96
|
|
|
|
97
|
|
|
return $access; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|