Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[SecurityBundle] Allow for custom request matchers #20272

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

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ private function addAccessControlSection(ArrayNodeDefinition $rootNode)
->prototype('array')
->fixXmlConfig('ip')
->fixXmlConfig('method')
->fixXmlConfig('role')
->children()
->scalarNode('matcher')->defaultNull()->end()
->scalarNode('requires_channel')->defaultNull()->end()
->scalarNode('path')
->defaultNull()
Expand All @@ -180,9 +182,6 @@ private function addAccessControlSection(ArrayNodeDefinition $rootNode)
->prototype('scalar')->end()
->end()
->scalarNode('allow_if')->defaultNull()->end()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this CS should be applied to a lower branch

->end()
->fixXmlConfig('role')
->children()
->arrayNode('roles')
->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 @@ -195,13 +195,11 @@ private function createAuthorization($config, ContainerBuilder $container)
));

foreach ($config['access_control'] as $access) {
$matcher = $this->createRequestMatcher(
$container,
$access['path'],
$access['host'],
$access['methods'],
$access['ips']
);
if (null !== $access['matcher']) {
$matcher = new Reference($access['matcher']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the currently created matcher is configured by many props in $access. Thus, is this extension point really enough when you don't give it these parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not sure about the "right" approach here. Perhaps we should talk config/API first.

But we could do matcher|<combination of other keys>.

DX-wise i think - { route: foo, roles: [] } is best and probably same for firewalls? (using an array of routes).

As goes for - { expression: foo }. But that could work counter-intuitive with allow_if.

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

$attributes = $access['roles'];
if ($access['allow_if']) {
Expand Down
42 changes: 42 additions & 0 deletions src/Symfony/Component/HttpFoundation/ChainRequestMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation;

/**
* @author Roland Franssen <[email protected]>
*/
class ChainRequestMatcher implements RequestMatcherInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this class related to this PR? It's not used anywhere?
About the name, "chain" usually means "delegation chain", ie the first applicable in the chain takes responsibility for the job. Here, this is something else, like AllMatchersRequestMatcher (dummy suggestion, I'm not really proud of it :) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. It's outdate-ish. I moved it to #20274.

{
private $matchers;

/**
* @param RequestMatcherInterface[] $matchers
*/
public function __construct(array $matchers)
{
$this->matchers = $matchers;
}

/**
* {@inheritdoc}
*/
public function matches(Request $request)
{
foreach ($this->matchers as $matcher) {
if (!$matcher->matches($request)) {
return false;
}
}

return true;
}
}