|
26 | 26 | use Symfony\Component\DependencyInjection\Reference;
|
27 | 27 | use Symfony\Component\ExpressionLanguage\Expression;
|
28 | 28 | use Symfony\Component\HttpFoundation\Request;
|
| 29 | +use Symfony\Component\HttpFoundation\RequestMatcher; |
29 | 30 | use Symfony\Component\HttpFoundation\Response;
|
30 | 31 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
31 | 32 | use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
@@ -250,6 +251,80 @@ public function testRegisterRequestMatchersWithAllowIfExpression()
|
250 | 251 | );
|
251 | 252 | }
|
252 | 253 |
|
| 254 | + public function testRegisterAccessControlWithSpecifiedRequestMatcherService() |
| 255 | + { |
| 256 | + $container = $this->getRawContainer(); |
| 257 | + |
| 258 | + $requestMatcherId = 'My\Test\RequestMatcher'; |
| 259 | + $requestMatcher = new RequestMatcher('/'); |
| 260 | + $container->set($requestMatcherId, $requestMatcher); |
| 261 | + |
| 262 | + $container->loadFromExtension('security', [ |
| 263 | + 'enable_authenticator_manager' => true, |
| 264 | + 'providers' => [ |
| 265 | + 'default' => ['id' => 'foo'], |
| 266 | + ], |
| 267 | + 'firewalls' => [ |
| 268 | + 'some_firewall' => [ |
| 269 | + 'pattern' => '/.*', |
| 270 | + 'http_basic' => [], |
| 271 | + ], |
| 272 | + ], |
| 273 | + 'access_control' => [ |
| 274 | + ['request_matcher' => $requestMatcherId], |
| 275 | + ], |
| 276 | + ]); |
| 277 | + |
| 278 | + $container->compile(); |
| 279 | + $accessMap = $container->getDefinition('security.access_map'); |
| 280 | + $this->assertCount(1, $accessMap->getMethodCalls()); |
| 281 | + $call = $accessMap->getMethodCalls()[0]; |
| 282 | + $this->assertSame('add', $call[0]); |
| 283 | + $args = $call[1]; |
| 284 | + $this->assertCount(3, $args); |
| 285 | + $this->assertSame($requestMatcherId, (string) $args[0]); |
| 286 | + } |
| 287 | + |
| 288 | + /** @dataProvider provideAdditionalRequestMatcherConstraints */ |
| 289 | + public function testRegisterAccessControlWithRequestMatcherAndAdditionalOptionsThrowsInvalidException(array $additionalConstraints) |
| 290 | + { |
| 291 | + $container = $this->getRawContainer(); |
| 292 | + |
| 293 | + $requestMatcherId = 'My\Test\RequestMatcher'; |
| 294 | + $requestMatcher = new RequestMatcher('/'); |
| 295 | + $container->set($requestMatcherId, $requestMatcher); |
| 296 | + |
| 297 | + $container->loadFromExtension('security', [ |
| 298 | + 'enable_authenticator_manager' => true, |
| 299 | + 'providers' => [ |
| 300 | + 'default' => ['id' => 'foo'], |
| 301 | + ], |
| 302 | + 'firewalls' => [ |
| 303 | + 'some_firewall' => [ |
| 304 | + 'pattern' => '/.*', |
| 305 | + 'http_basic' => [], |
| 306 | + ], |
| 307 | + ], |
| 308 | + 'access_control' => [ |
| 309 | + array_merge(['request_matcher' => $requestMatcherId], $additionalConstraints), |
| 310 | + ], |
| 311 | + ]); |
| 312 | + |
| 313 | + $this->expectException(InvalidConfigurationException::class); |
| 314 | + $this->expectExceptionMessage('The "request_matcher" option should not be specified alongside other options. Consider integrating your constraints inside your RequestMatcher directly.'); |
| 315 | + |
| 316 | + $container->compile(); |
| 317 | + } |
| 318 | + |
| 319 | + public function provideAdditionalRequestMatcherConstraints() |
| 320 | + { |
| 321 | + yield 'Invalid configuration with path' => [['path' => '^/url']]; |
| 322 | + yield 'Invalid configuration with host' => [['host' => 'example.com']]; |
| 323 | + yield 'Invalid configuration with port' => [['port' => 80]]; |
| 324 | + yield 'Invalid configuration with methods' => [['methods' => ['POST']]]; |
| 325 | + yield 'Invalid configuration with ips' => [['ips' => ['0.0.0.0']]]; |
| 326 | + } |
| 327 | + |
253 | 328 | public function testRemovesExpressionCacheWarmerDefinitionIfNoExpressions()
|
254 | 329 | {
|
255 | 330 | $container = $this->getRawContainer();
|
|
0 commit comments