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\ExpressionLanguage; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface; |
15
|
|
|
|
16
|
|
|
class AuthorizationExpressionProvider implements ExpressionFunctionProviderInterface |
17
|
|
|
{ |
18
|
27 |
|
public function getFunctions() |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
27 |
|
new ExpressionFunction( |
22
|
27 |
|
'hasRole', |
23
|
|
|
function ($role) { |
24
|
2 |
|
return sprintf('$container->get(\'security.authorization_checker\')->isGranted(%s)', $role); |
25
|
|
|
} |
26
|
27 |
|
), |
27
|
|
|
|
28
|
27 |
|
new ExpressionFunction( |
29
|
27 |
|
'hasAnyRole', |
30
|
|
|
function ($roles) { |
31
|
1 |
|
$code = sprintf('array_reduce(%s, function ($isGranted, $role) use ($container) { return $isGranted || $container->get(\'security.authorization_checker\')->isGranted($role); }, false)', $roles); |
32
|
|
|
|
33
|
1 |
|
return $code; |
34
|
|
|
} |
35
|
27 |
|
), |
36
|
|
|
|
37
|
27 |
|
new ExpressionFunction( |
38
|
27 |
|
'isAnonymous', |
39
|
|
|
function () { |
40
|
1 |
|
return '$container->get(\'security.authorization_checker\')->isGranted(\'IS_AUTHENTICATED_ANONYMOUSLY\')'; |
41
|
|
|
} |
42
|
27 |
|
), |
43
|
|
|
|
44
|
27 |
|
new ExpressionFunction( |
45
|
27 |
|
'isRememberMe', |
46
|
|
|
function () { |
47
|
1 |
|
return '$container->get(\'security.authorization_checker\')->isGranted(\'IS_AUTHENTICATED_REMEMBERED\')'; |
48
|
|
|
} |
49
|
27 |
|
), |
50
|
|
|
|
51
|
27 |
|
new ExpressionFunction( |
52
|
27 |
|
'isFullyAuthenticated', |
53
|
|
|
function () { |
54
|
2 |
|
return '$container->get(\'security.authorization_checker\')->isGranted(\'IS_AUTHENTICATED_FULLY\')'; |
55
|
|
|
} |
56
|
27 |
|
), |
57
|
|
|
|
58
|
27 |
|
new ExpressionFunction( |
59
|
27 |
|
'isAuthenticated', |
60
|
|
|
function () { |
61
|
1 |
|
return '$container->get(\'security.authorization_checker\')->isGranted(\'IS_AUTHENTICATED_REMEMBERED\') || $container->get(\'security.authorization_checker\')->isGranted(\'IS_AUTHENTICATED_FULLY\')'; |
62
|
|
|
} |
63
|
27 |
|
), |
64
|
|
|
|
65
|
27 |
|
new ExpressionFunction( |
66
|
27 |
|
'hasPermission', |
67
|
|
|
function ($object, $permission) { |
68
|
1 |
|
$code = sprintf('$container->get(\'security.authorization_checker\')->isGranted(%s, %s)', $permission, $object); |
69
|
|
|
|
70
|
1 |
|
return $code; |
71
|
|
|
} |
72
|
27 |
|
), |
73
|
|
|
|
74
|
27 |
|
new ExpressionFunction( |
75
|
27 |
|
'hasAnyPermission', |
76
|
1 |
|
function ($object, $permissions) { |
77
|
1 |
|
$code = sprintf('array_reduce(%s, function ($isGranted, $permission) use ($container, $object) { return $isGranted || $container->get(\'security.authorization_checker\')->isGranted($permission, %s); }, false)', $permissions, $object); |
78
|
|
|
|
79
|
1 |
|
return $code; |
80
|
|
|
} |
81
|
27 |
|
), |
82
|
27 |
|
]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|