|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2010-2017 Fabien Potencier |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is furnished |
|
11
|
|
|
* to do so, subject to t * *he following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
14
|
|
|
* copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace Bankiru\Api\Rpc\Listener; |
|
27
|
|
|
|
|
28
|
|
|
use Bankiru\Api\Rpc\Event\FilterControllerEvent; |
|
29
|
|
|
use Bankiru\Api\Rpc\Http\RequestInterface; |
|
30
|
|
|
use Bankiru\Api\Rpc\RpcEvents; |
|
31
|
|
|
use Bankiru\Api\Rpc\RpcRequestInterface; |
|
32
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Security\ExpressionLanguage; |
|
33
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
34
|
|
|
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; |
|
35
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
36
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
37
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
38
|
|
|
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; |
|
39
|
|
|
use Symfony\Component\Security\Core\Role\RoleInterface; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* SecurityListener handles security restrictions on controllers. |
|
43
|
|
|
* |
|
44
|
|
|
* @author Fabien Potencier <[email protected]> |
|
45
|
|
|
*/ |
|
46
|
|
|
final class SecurityListener implements EventSubscriberInterface |
|
47
|
|
|
{ |
|
48
|
|
|
private $tokenStorage; |
|
49
|
|
|
private $authChecker; |
|
50
|
|
|
private $language; |
|
51
|
|
|
private $trustResolver; |
|
52
|
|
|
private $roleHierarchy; |
|
53
|
|
|
|
|
54
|
8 |
|
public function __construct( |
|
55
|
|
|
TokenStorageInterface $tokenStorage, |
|
56
|
|
|
AuthorizationCheckerInterface $authChecker, |
|
57
|
|
|
ExpressionLanguage $language = null, |
|
58
|
|
|
AuthenticationTrustResolverInterface $trustResolver = null, |
|
59
|
|
|
RoleHierarchyInterface $roleHierarchy = null |
|
60
|
|
|
) { |
|
61
|
8 |
|
$this->tokenStorage = $tokenStorage; |
|
62
|
8 |
|
$this->authChecker = $authChecker; |
|
63
|
8 |
|
$this->language = $language; |
|
64
|
8 |
|
$this->trustResolver = $trustResolver; |
|
65
|
8 |
|
$this->roleHierarchy = $roleHierarchy; |
|
66
|
8 |
|
} |
|
67
|
|
|
|
|
68
|
8 |
|
public function onKernelController(FilterControllerEvent $event) |
|
69
|
|
|
{ |
|
70
|
8 |
|
$request = $event->getRequest(); |
|
71
|
8 |
|
if (!$configuration = $request->getAttributes()->get('_security')) { |
|
72
|
6 |
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
if (null === $this->tokenStorage || null === $this->trustResolver) { |
|
76
|
|
|
throw new \LogicException('To use the @Security tag, you need to install the Symfony Security bundle.'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
if (null === $this->tokenStorage->getToken()) { |
|
80
|
|
|
throw new \LogicException('To use the @Security tag, your controller needs to be behind a firewall.'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
if (null === $this->language) { |
|
84
|
|
|
throw new \LogicException( |
|
85
|
|
|
'To use the @Security tag, you need to use the Security component 2.4 or newer and install the ExpressionLanguage component.' |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
if (!$this->language->evaluate($configuration->getExpression(), $this->getVariables($request))) { |
|
90
|
1 |
|
throw new AccessDeniedException(sprintf('Expression "%s" denied access.', $configuration->getExpression())); |
|
91
|
|
|
} |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
// code should be sync with Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter |
|
95
|
|
|
|
|
96
|
1 |
|
public static function getSubscribedEvents() |
|
97
|
|
|
{ |
|
98
|
1 |
|
return [RpcEvents::CONTROLLER => ['onKernelController', -255]]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
private function getVariables(RpcRequestInterface $request) |
|
102
|
|
|
{ |
|
103
|
2 |
|
$token = $this->tokenStorage->getToken(); |
|
104
|
|
|
|
|
105
|
2 |
|
if (null !== $this->roleHierarchy) { |
|
106
|
2 |
|
$roles = $this->roleHierarchy->getReachableRoles($token->getRoles()); |
|
107
|
2 |
|
} else { |
|
108
|
|
|
$roles = $token->getRoles(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$variables = [ |
|
112
|
2 |
|
'token' => $token, |
|
113
|
2 |
|
'user' => $token->getUser(), |
|
114
|
2 |
|
'object' => $request, |
|
115
|
2 |
|
'request' => $request, |
|
116
|
2 |
|
'roles' => array_map( |
|
117
|
2 |
|
function (RoleInterface $role) { |
|
118
|
|
|
return $role->getRole(); |
|
119
|
2 |
|
}, |
|
120
|
|
|
$roles |
|
121
|
2 |
|
), |
|
122
|
2 |
|
'trust_resolver' => $this->trustResolver, |
|
123
|
|
|
// needed for the is_granted expression function |
|
124
|
2 |
|
'auth_checker' => $this->authChecker, |
|
125
|
2 |
|
]; |
|
126
|
|
|
|
|
127
|
|
|
// controller variables should also be accessible |
|
128
|
2 |
|
return array_merge($request->getAttributes()->all(), $variables); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|