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