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