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