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

Skip to content

Commit 985c190

Browse files
committed
Add interface to let security factories add their own firewall listeners
1 parent 6f6e4ce commit 985c190

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
7+
interface FirewallListenerFactoryInterface
8+
{
9+
/**
10+
* Creates the firewall listener services for the provided configuration.
11+
*
12+
* @return string[] The listener service IDs to be used by the firewall
13+
*/
14+
public function createListeners(ContainerBuilder $container, string $firewallName, array $config): array;
15+
}

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AuthenticatorFactoryInterface;
1515
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\EntryPointFactoryInterface;
16+
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\FirewallListenerFactoryInterface;
1617
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\RememberMeFactory;
1718
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
1819
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface;
@@ -570,6 +571,14 @@ private function createAuthenticationListeners(ContainerBuilder $container, stri
570571
$listeners[] = new Reference($listenerId);
571572
$authenticationProviders[] = $provider;
572573
}
574+
575+
if ($factory instanceof FirewallListenerFactoryInterface) {
576+
$firewallListenerIds = $factory->createListeners($container, $id, $firewall[$key]);
577+
foreach ($firewallListenerIds as $firewallListenerId) {
578+
$listeners[] = new Reference($firewallListenerId);
579+
}
580+
}
581+
573582
$hasListeners = true;
574583
}
575584
}

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
16+
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\FirewallListenerFactoryInterface;
17+
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
1618
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
1719
use Symfony\Bundle\SecurityBundle\SecurityBundle;
1820
use Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Fixtures\UserProvider\DummyProvider;
1921
use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FirewallEntryPointBundle\Security\EntryPointStub;
2022
use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\GuardedBundle\AppCustomAuthenticator;
23+
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
2124
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
2225
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
2326
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -601,6 +604,29 @@ public function testCompilesWithSessionListenerWithStatefulllFirewallWithAuthent
601604
$this->assertTrue($container->has('security.listener.session.'.$firewallId));
602605
}
603606

607+
public function testConfigureCustomFirewallListener(): void
608+
{
609+
$container = $this->getRawContainer();
610+
/** @var SecurityExtension $extension */
611+
$extension = $container->getExtension('security');
612+
$extension->addSecurityListenerFactory(new TestFirewallListenerFactory());
613+
614+
$container->loadFromExtension('security', [
615+
'firewalls' => [
616+
'main' => [
617+
'custom_listener' => true,
618+
],
619+
],
620+
]);
621+
622+
$container->compile();
623+
624+
/** @var IteratorArgument $listenersIteratorArgument */
625+
$listenersIteratorArgument = $container->getDefinition('security.firewall.map.context.main')->getArgument(0);
626+
$firewallListeners = array_map('strval', $listenersIteratorArgument->getValues());
627+
$this->assertContains('custom_firewall_listener_id', $firewallListeners);
628+
}
629+
604630
protected function getRawContainer()
605631
{
606632
$container = new ContainerBuilder();
@@ -689,3 +715,30 @@ public function supportsRememberMe()
689715
{
690716
}
691717
}
718+
719+
class TestFirewallListenerFactory implements SecurityFactoryInterface, FirewallListenerFactoryInterface
720+
{
721+
public function createListeners(ContainerBuilder $container, string $firewallName, array $config): array
722+
{
723+
return ['custom_firewall_listener_id'];
724+
}
725+
726+
public function create(ContainerBuilder $container, string $id, array $config, string $userProvider, ?string $defaultEntryPoint)
727+
{
728+
return ['provider_id', 'listener_id', $defaultEntryPoint];
729+
}
730+
731+
public function getPosition()
732+
{
733+
return 'form';
734+
}
735+
736+
public function getKey()
737+
{
738+
return 'custom_listener';
739+
}
740+
741+
public function addConfiguration(NodeDefinition $builder)
742+
{
743+
}
744+
}

0 commit comments

Comments
 (0)