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