Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[SecurityBundle] Allow specifying attributes for RequestMatcher #45907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[SecurityBundle] Allow specifying attributes for RequestMatcher
  • Loading branch information
freiondrej-lmc authored and fabpot committed Oct 20, 2022
commit c19d1cb3ef962ac7f0a813f447114abad73ca01d
2 changes: 2 additions & 0 deletions 2 src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ CHANGELOG
---

* The `security.access_control` now accepts a `RequestMatcherInterface` under the `request_matcher` option as scope configuration
* The `security.access_control` now accepts an `attributes` array to match request attributes in the `RequestMatcher`
* The `security.access_control` now accepts a `route` option to match request route in the `RequestMatcher`
* Display the inherited roles of the logged-in user in the Web Debug Toolbar

6.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ private function addAccessControlSection(ArrayNodeDefinition $rootNode)
->beforeNormalization()->ifString()->then(function ($v) { return [$v]; })->end()
->prototype('scalar')->end()
->end()
->arrayNode('attributes')
->prototype('scalar')->end()
->end()
->scalarNode('route')->defaultNull()->end()
->arrayNode('methods')
->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end()
->prototype('scalar')->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,24 +199,34 @@ private function createAuthorization(array $config, ContainerBuilder $container)
{
foreach ($config['access_control'] as $access) {
if (isset($access['request_matcher'])) {
if ($access['path'] || $access['host'] || $access['port'] || $access['ips'] || $access['methods']) {
if ($access['path'] || $access['host'] || $access['port'] || $access['ips'] || $access['methods'] || $access['attributes'] || $access['route']) {
throw new InvalidConfigurationException('The "request_matcher" option should not be specified alongside other options. Consider integrating your constraints inside your RequestMatcher directly.');
}
$matcher = new Reference($access['request_matcher']);
} else {
$attributes = $access['attributes'];

if ($access['route']) {
if (\array_key_exists('_route', $attributes)) {
throw new InvalidConfigurationException('The "route" option should not be specified alongside "attributes._route" option. Use just one of the options.');
}
$attributes['_route'] = $access['route'];
}

$matcher = $this->createRequestMatcher(
$container,
$access['path'],
$access['host'],
$access['port'],
$access['methods'],
$access['ips']
$access['ips'],
$attributes
);
}

$attributes = $access['roles'];
$roles = $access['roles'];
if ($access['allow_if']) {
$attributes[] = $this->createExpression($container, $access['allow_if']);
$roles[] = $this->createExpression($container, $access['allow_if']);
}

$emptyAccess = 0 === \count(array_filter($access));
Expand All @@ -226,7 +236,7 @@ private function createAuthorization(array $config, ContainerBuilder $container)
}

$container->getDefinition('security.access_map')
->addMethodCall('add', [$matcher, $attributes, $access['requires_channel']]);
->addMethodCall('add', [$matcher, $roles, $access['requires_channel']]);
}

// allow cache warm-up for expressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,105 @@ public function provideAdditionalRequestMatcherConstraints()
yield 'Invalid configuration with port' => [['port' => 80]];
yield 'Invalid configuration with methods' => [['methods' => ['POST']]];
yield 'Invalid configuration with ips' => [['ips' => ['0.0.0.0']]];
yield 'Invalid configuration with attributes' => [['attributes' => ['_route' => 'foo_route']]];
yield 'Invalid configuration with route' => [['route' => 'foo_route']];
}

public function testRegisterAccessControlWithSpecifiedAttributes()
{
$container = $this->getRawContainer();
$container->loadFromExtension('security', [
'enable_authenticator_manager' => true,
'providers' => [
'default' => ['id' => 'foo'],
],
'firewalls' => [
'some_firewall' => [
'pattern' => '/.*',
'http_basic' => [],
],
],
'access_control' => [
['attributes' => ['_route' => 'foo_route']],
],
]);

$container->compile();

$accessMap = $container->getDefinition('security.access_map');
$this->assertCount(1, $accessMap->getMethodCalls());
$call = $accessMap->getMethodCalls()[0];
$this->assertSame('add', $call[0]);
$args = $call[1];
$requestMatcherId = (string) $args[0];

$requestMatcherDefinition = $container->getDefinition($requestMatcherId);
$requestMatcherConstructorArguments = $requestMatcherDefinition->getArguments();
$this->assertArrayHasKey(4, $requestMatcherConstructorArguments);
$attributes = $requestMatcherConstructorArguments[4];
$this->assertArrayHasKey('_route', $attributes);
$this->assertSame('foo_route', $attributes['_route']);
}

public function testRegisterAccessControlWithSpecifiedRoute()
{
$container = $this->getRawContainer();
$container->loadFromExtension('security', [
'enable_authenticator_manager' => true,
'providers' => [
'default' => ['id' => 'foo'],
],
'firewalls' => [
'some_firewall' => [
'pattern' => '/.*',
'http_basic' => [],
],
],
'access_control' => [
['route' => 'foo_route'],
],
]);

$container->compile();

$accessMap = $container->getDefinition('security.access_map');
$this->assertCount(1, $accessMap->getMethodCalls());
$call = $accessMap->getMethodCalls()[0];
$this->assertSame('add', $call[0]);
$args = $call[1];
$requestMatcherId = (string) $args[0];

$requestMatcherDefinition = $container->getDefinition($requestMatcherId);
$requestMatcherConstructorArguments = $requestMatcherDefinition->getArguments();
$this->assertArrayHasKey(4, $requestMatcherConstructorArguments);
$attributes = $requestMatcherConstructorArguments[4];
$this->assertArrayHasKey('_route', $attributes);
$this->assertSame('foo_route', $attributes['_route']);
}

public function testRegisterAccessControlWithSpecifiedAttributesThrowsException()
{
$container = $this->getRawContainer();
$container->loadFromExtension('security', [
'enable_authenticator_manager' => true,
'providers' => [
'default' => ['id' => 'foo'],
],
'firewalls' => [
'some_firewall' => [
'pattern' => '/.*',
'http_basic' => [],
],
],
'access_control' => [
['route' => 'anything', 'attributes' => ['_route' => 'foo_route']],
],
]);

$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('The "route" option should not be specified alongside "attributes._route" option. Use just one of the options.');

$container->compile();
}

public function testRemovesExpressionCacheWarmerDefinitionIfNoExpressions()
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.