-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymfonyAccessControlProvider.php
More file actions
231 lines (191 loc) · 6.5 KB
/
SymfonyAccessControlProvider.php
File metadata and controls
231 lines (191 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/*
*
* (c) Yaroslav Honcharuk <yaroslav.xs@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Yarhon\RouteGuardBundle\Security\TestProvider;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Routing\Route;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\ExpressionLanguage\SyntaxError;
use Yarhon\RouteGuardBundle\Controller\ControllerMetadata;
use Yarhon\RouteGuardBundle\Security\Http\RequestConstraint;
use Yarhon\RouteGuardBundle\Security\Http\RouteMatcher;
use Yarhon\RouteGuardBundle\Security\Test\TestBag;
use Yarhon\RouteGuardBundle\Security\Test\SymfonyAccessControlTest;
use Yarhon\RouteGuardBundle\Security\Http\RequestDependentTestBag;
use Yarhon\RouteGuardBundle\Security\Authorization\SymfonySecurityExpressionVoter;
use Yarhon\RouteGuardBundle\Exception\LogicException;
use Yarhon\RouteGuardBundle\Exception\InvalidArgumentException;
/**
* SymfonyAccessControlProvider processes access_control config of Symfony SecurityBundle.
*
* @see https://symfony.com/doc/4.1/security/access_control.html
*
* @author Yaroslav Honcharuk <yaroslav.xs@gmail.com>
*/
class SymfonyAccessControlProvider implements ProviderInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* @var RouteMatcher
*/
private $routeMatcher;
/**
* @var array
*/
private $rules = [];
/**
* @var ExpressionLanguage
*/
private $expressionLanguage;
/**
* @var array
*/
private $tests = [];
/**
* @param RouteMatcher $routeMatcher
*/
public function __construct(RouteMatcher $routeMatcher)
{
$this->routeMatcher = $routeMatcher;
}
/**
* @param ExpressionLanguage $expressionLanguage
*/
public function setExpressionLanguage(ExpressionLanguage $expressionLanguage)
{
$this->expressionLanguage = $expressionLanguage;
}
/**
* @param array $rules
*/
public function importRules(array $rules)
{
foreach ($rules as $rule) {
$transformed = $this->transformRule($rule);
$this->addRule(...$transformed);
}
}
/**
* @param RequestConstraint $constraint
* @param SymfonyAccessControlTest $test
*/
public function addRule(RequestConstraint $constraint, SymfonyAccessControlTest $test)
{
$this->rules[] = [$constraint, $test];
}
/**
* @param array $rule
*
* @return array
*/
private function transformRule(array $rule)
{
$constraint = new RequestConstraint($rule['path'], $rule['host'], $rule['methods'], $rule['ips']);
$attributes = $rule['roles'];
if ($rule['allow_if']) {
if (!$this->expressionLanguage) {
throw new LogicException('Cannot create expression because ExpressionLanguage is not provided.');
}
$expression = $this->createExpression($rule['allow_if']);
$attributes[] = $expression;
}
$uniqueKey = $this->getTestAttributesUniqueKey($attributes);
if (!isset($this->tests[$uniqueKey])) {
$this->tests[$uniqueKey] = new SymfonyAccessControlTest($attributes);
}
$test = $this->tests[$uniqueKey];
return [$constraint, $test];
}
/**
* {@inheritdoc}
*/
public function getTests($routeName, Route $route, ControllerMetadata $controllerMetadata = null)
{
$matches = [];
foreach ($this->rules as $index => list($constraint, $test)) {
$matchResult = $this->routeMatcher->matches($route, $constraint);
if (false === $matchResult) {
continue;
}
$tests = [$test];
if (true === $matchResult) {
$matches[$index] = [$tests, null];
break;
}
if ($matchResult instanceof RequestConstraint) {
$matches[$index] = [$tests, $matchResult];
continue;
}
}
if (!count($matches)) {
return null;
}
$originalMatches = $matches;
$matches = array_values($matches);
if (1 === count($matches) && null === $matches[0][1]) {
// Always matching rule was found, and there were no possibly matching rules found before,
// so we don't need a RequestDependentTestBag for resolving it by RequestContext in runtime.
$testBag = new TestBag($matches[0][0]);
} else {
$this->logRuntimeMatching($route, $routeName, $originalMatches);
$testBag = new RequestDependentTestBag($matches);
}
return $testBag;
}
/**
* @param string $expression
*
* @return Expression
*
* @throws InvalidArgumentException
*/
private function createExpression($expression)
{
$names = SymfonySecurityExpressionVoter::getVariableNames();
try {
$parsed = $this->expressionLanguage->parse($expression, $names);
} catch (SyntaxError $e) {
throw new InvalidArgumentException(sprintf('Cannot parse expression "%s" with following variables: "%s".', $expression, implode('", "', $names)), 0, $e);
}
return $parsed;
}
/**
* @param Route $route
* @param string $routeName
* @param array $matches
*/
private function logRuntimeMatching(Route $route, $routeName, array $matches)
{
if (!$this->logger) {
return;
}
$message = 'Route "%s" (path "%s") requires runtime matching to access_control rule(s) #%s (zero-based), this would reduce performance.';
$this->logger->warning(sprintf($message, $routeName, $route->getPath(), implode(', #', array_keys($matches))));
}
/**
* @param array $attributes
*
* @return string
*/
private function getTestAttributesUniqueKey(array $attributes)
{
$roles = $attributes;
$expressions = array_filter($attributes, function ($attribute) {
return $attribute instanceof Expression;
});
$roles = array_diff($roles, $expressions);
$expressions = array_map(function ($expression) {
return (string) $expression;
}, $expressions);
$roles = array_unique($roles);
sort($roles);
return implode('#', array_merge($roles, $expressions));
}
}