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

Skip to content
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] Make firewalls event dispatcher traceable on debug mode
  • Loading branch information
MatTheCat authored and nicolas-grekas committed Jan 26, 2023
commit c5407ce64e93206d62b3b2ca3d42ba8f6be46e06
1 change: 1 addition & 0 deletions src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Modify "icon.svg" to improve accessibility for blind/low vision users
* Make `Security::login()` return the authenticator response
* Deprecate the `security.firewalls.logout.csrf_token_generator` config option, use `security.firewalls.logout.csrf_token_manager` instead
* Make firewalls event dispatcher traceable on debug mode

6.2
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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\Bundle\SecurityBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;

/**
* @author Mathieu Lechat <[email protected]>
*/
class MakeFirewallsEventDispatcherTraceablePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->has('event_dispatcher') || !$container->hasParameter('security.firewalls')) {
return;
}

if (!$container->getParameter('kernel.debug') || !$container->has('debug.stopwatch')) {
return;
}

$dispatchersId = [];

foreach ($container->getParameter('security.firewalls') as $firewallName) {
$dispatcherId = 'security.event_dispatcher.'.$firewallName;

if (!$container->has($dispatcherId)) {
continue;
}

$dispatchersId[$dispatcherId] = 'debug.'.$dispatcherId;

$container->register($dispatchersId[$dispatcherId], TraceableEventDispatcher::class)
->setDecoratedService($dispatcherId)
->setArguments([
new Reference($dispatchersId[$dispatcherId].'.inner'),
new Reference('debug.stopwatch'),
new Reference('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE),
new Reference('request_stack', ContainerInterface::NULL_ON_INVALID_REFERENCE),
]);
}

foreach (['kernel.event_subscriber', 'kernel.event_listener'] as $tagName) {
foreach ($container->findTaggedServiceIds($tagName) as $taggedServiceId => $tags) {
$taggedServiceDefinition = $container->findDefinition($taggedServiceId);
$taggedServiceDefinition->clearTag($tagName);

foreach ($tags as $tag) {
if ($dispatcherId = $tag['dispatcher'] ?? null) {
$tag['dispatcher'] = $dispatchersId[$dispatcherId] ?? $dispatcherId;
}
$taggedServiceDefinition->addTag($tagName, $tag);
}
}
}
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/SecurityBundle/SecurityBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddSecurityVotersPass;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddSessionDomainConstraintPass;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\CleanRememberMeVerifierPass;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\MakeFirewallsEventDispatcherTraceablePass;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\RegisterCsrfFeaturesPass;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\RegisterEntryPointPass;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\RegisterGlobalSecurityEventListenersPass;
Expand Down Expand Up @@ -92,5 +93,8 @@ public function build(ContainerBuilder $container)
AuthenticationEvents::ALIASES,
SecurityEvents::ALIASES
)));

// must be registered before DecoratorServicePass
$container->addCompilerPass(new MakeFirewallsEventDispatcherTraceablePass(), PassConfig::TYPE_OPTIMIZE, 10);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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\Bundle\SecurityBundle\Tests\DependencyInjection\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Component\DependencyInjection\Compiler\DecoratorServicePass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Stopwatch\Stopwatch;

class MakeFirewallsEventDispatcherTraceablePassTest extends TestCase
{
private $container;

protected function setUp(): void
{
$this->container = new ContainerBuilder();
$this->container->register('request_stack', \stdClass::class);
$this->container->register('event_dispatcher', EventDispatcher::class);
$this->container->register('debug.stopwatch', Stopwatch::class);

$this->container->registerExtension(new SecurityExtension());
$this->container->loadFromExtension('security', [
'firewalls' => ['main' => ['pattern' => '/', 'http_basic' => true]],
]);

$this->container->addCompilerPass(new DecoratorServicePass(), PassConfig::TYPE_OPTIMIZE);
$this->container->getCompilerPassConfig()->setRemovingPasses([]);
$this->container->getCompilerPassConfig()->setAfterRemovingPasses([]);

$securityBundle = new SecurityBundle();
$securityBundle->build($this->container);
}

public function testEventDispatcherIsDecoratedOnDebugMode()
{
$this->container->setParameter('kernel.debug', true);

$this->container->compile();

$dispatcherDefinition = $this->container->findDefinition('security.event_dispatcher.main');

$this->assertSame(TraceableEventDispatcher::class, $dispatcherDefinition->getClass());
$this->assertSame(
[['name' => 'security.event_dispatcher.main']],
$dispatcherDefinition->getTag('event_dispatcher.dispatcher')
);
}

public function testEventDispatcherIsNotDecoratedOnNonDebugMode()
{
$this->container->setParameter('kernel.debug', false);

$this->container->compile();

$dispatcherDefinition = $this->container->findDefinition('security.event_dispatcher.main');

$this->assertSame(EventDispatcher::class, $dispatcherDefinition->getClass());
}
}