8000 [Routing][SecurityBundle] Add `LogoutRouteLoader` by MatTheCat · Pull Request #50946 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing][SecurityBundle] Add LogoutRouteLoader #50946

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 15, 2023
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Allow an array of `pattern` in firewall configuration
* Add `$badges` argument to `Security::login`
* Deprecate the `require_previous_session` config option. Setting it has no effect anymore
* Add `LogoutRouteLoader`

6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use Symfony\Component\PasswordHasher\Hasher\Pbkdf2PasswordHasher;
use Symfony\Component\PasswordHasher\Hasher\PlaintextPasswordHasher;
use Symfony\Component\PasswordHasher\Hasher\SodiumPasswordHasher;
use Symfony\Component\Routing\Loader\ContainerLoader;
use Symfony\Component\Security\Core\Authorization\Strategy\AffirmativeStrategy;
use Symfony\Component\Security\Core\Authorization\Strategy\ConsensusStrategy;
use Symfony\Component\Security\Core\Authorization\Strategy\PriorityStrategy;
Expand Down Expand Up @@ -170,6 +171,13 @@ public function load(array $configs, ContainerBuilder $container)
}

$this->createFirewalls($config, $container);

if ($container::willBeAvailable('symfony/routing', ContainerLoader::class, ['symfony/security-bundle'])) {
$this->createLogoutUrisParameter($config['firewalls'] ?? [], $container);
} else {
$container->removeDefinition('security.route_loader.logout');
}

$this->createAuthorization($config, $container);
$this->createRoleHierarchy($config, $container);

Expand Down Expand Up @@ -1095,4 +1103,20 @@ private function getSortedFactories(): array

return $this->sortedFactories;
}

private function createLogoutUrisParameter(array $firewallsConfig, ContainerBuilder $container): void
{
$logoutUris = [];
foreach ($firewallsConfig as $name => $config) {
if (!$logoutPath = $config['logout']['path'] ?? null) {
continue;
}

if ('/' === $logoutPath[0]) {
$logoutUris[$name] = $logoutPath;
}
}

$container->setParameter('security.logout_uris', $logoutUris);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Bundle\SecurityBundle\CacheWarmer\ExpressionCacheWarmer;
use Symfony\Bundle\SecurityBundle\EventListener\FirewallListener;
use Symfony\Bundle\SecurityBundle\Routing\LogoutRouteLoader;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Bundle\SecurityBundle\Security\FirewallConfig;
use Symfony\Bundle\SecurityBundle\Security\FirewallContext;
Expand Down Expand Up @@ -229,6 +230,13 @@
service('security.token_storage')->nullOnInvalid(),
])

->set('security.route_loader.logout', LogoutRouteLoader::class)
->args([
'%security.logout_uris%',
'security.logout_uris',
])
->tag('routing.route_loader')

// Provisioning
->set('security.user.provider.missing', MissingUserProvider::class)
->abstract()
Expand Down
49 changes: 49 additions & 0 deletions src/Symfony/Bundle/SecurityBundle/Routing/LogoutRouteLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

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

namespace Symfony\Bundle\SecurityBundle\Routing;

use Symfony\Component\DependencyInjection\Config\ContainerParametersResource;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

final class LogoutRouteLoader
{
/**
* @param array<string, string> $logoutUris Logout URIs indexed by the corresponding firewall name
* @param string $parameterName Name of the container parameter containing {@see $logoutUris}' value
*/
public function __construct(
private readonly array $logoutUris,
private readonly string $parameterName,
) {
}

public function __invoke(): RouteCollection
{
$collection = new RouteCollection();
$collection->addResource(new ContainerParametersResource([$this->parameterName => $this->logoutUris]));

$routeNames = [];
foreach ($this->logoutUris as $firewallName => $logoutPath) {
$routeName = '_logout_'.$firewallName;

if (isset($routeNames[$logoutPath])) {
$collection->addAlias($routeName, $routeNames[$logoutPath]);
} else {
$routeNames[$logoutPath] = $routeName;
$collection->add($routeName, new Route($logoutPath));
}
}

return $collection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

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

namespace Symfony\Bundle\SecurityBundle\Tests\Routing;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Routing\LogoutRouteLoader;
use Symfony\Component\DependencyInjection\Config\ContainerParametersResource;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class LogoutRouteLoaderTest extends TestCase
{
public function testLoad()
{
$logoutPaths = [
'main' => '/logout',
'admin' => '/logout',
];

$loader = new LogoutRouteLoader($logoutPaths, 'parameterName');
$collection = $loader();

self::assertInstanceOf(RouteCollection::class, $collection);
self::assertCount(1, $collection);
self::assertEquals(new Route('/logout'), $collection->get('_logout_main'));
self::assertCount(1, $collection->getAliases());
self::assertEquals('_logout_main', $collection->getAlias('_logout_admin')->getId());

$resources = $collection->getResources();
self::assertCount(1, $resources);

$resource = reset($resources);
self::assertInstanceOf(ContainerParametersResource::class, $resource);
self::assertSame(['parameterName' => $logoutPaths], $resource->getParameters());
}
}
0