-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SecurityBundle][Routing] Add
LogoutRouteLoader
- Loading branch information
Showing
5 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Symfony/Bundle/SecurityBundle/Routing/LogoutRouteLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Symfony/Bundle/SecurityBundle/Tests/Routing/LogoutRouteLoaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |