8000 [Security] Let security factories add firewall listeners by scheb · Pull Request #37336 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] Let security factories add firewall listeners #37336

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
Jun 20, 2020
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
5 changes: 5 additions & 0 deletions src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.2.0
-----

* Added `FirewallListenerFactoryInterface`, which can be implemented by security factories to add firewall listeners

5.1.0
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\DependencyInjection\Security\Factory;

use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Can be implemented by a security factory to add a listener to the firewall.
*
* @author Christian Scheb <me@christianscheb.de>
*/
interface FirewallListenerFactoryInterface
{
/**
* Creates the firewall listener services for the provided configuration.
*
* @return string[] The listener service IDs to be used by the firewall
*/
public function createListeners(ContainerBuilder $container, string $firewallName, array $config): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AuthenticatorFactoryInterface;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\EntryPointFactoryInterface;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\FirewallListenerFactoryInterface;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\RememberMeFactory;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface;
Expand Down Expand Up @@ -570,6 +571,14 @@ private function createAuthenticationListeners(ContainerBuilder $container, stri
$listeners[] = new Reference($listenerId);
$authenticationProviders[] = $provider;
}

if ($factory instanceof FirewallListenerFactoryInterface) {
$firewallListenerIds = $factory->createListeners($container, $id, $firewall[$key]);
foreach ($firewallListenerIds as $firewallListenerId) {
$listeners[] = new Reference($firewallListenerId);
}
}

$hasListeners = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\FirewallListenerFactoryInterface;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Fixtures\UserProvider\DummyProvider;
use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FirewallEntryPointBundle\Security\EntryPointStub;
use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\GuardedBundle\AppCustomAuthenticator;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -601,6 +604,29 @@ public function testCompilesWithSessionListenerWithStatefulllFirewallWithAuthent
$this->assertTrue($container->has('security.listener.session.'.$firewallId));
}

public function testConfigureCustomFirewallListener(): void
{
$container = $this->getRawContainer();
/** @var SecurityExtension $extension */
$extension = $container->getExtension('security');
$extension->addSecurityListenerFactory(new TestFirewallListenerFactory());

$container->loadFromExtension('security', [
'firewalls' => [
'main' => [
'custom_listener' => true,
],
],
]);

$container->compile();

/** @var IteratorArgument $listenersIteratorArgument */
$listenersIteratorArgument = $container->getDefinition('security.firewall.map.context.main')->getArgument(0);
$firewallListeners = array_map('strval', $listenersIteratorArgument->getValues());
$this->assertContains('custom_firewall_listener_id', $firewallListeners);
}

protected function getRawContainer()
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -689,3 +715,30 @@ public function supportsRememberMe()
{
}
}

class TestFirewallListenerFactory implements SecurityFactoryInterface, FirewallListenerFactoryInterface
{
public function createListeners(ContainerBuilder $container, string $firewallName, array $config): array
{
return ['custom_firewall_listener_id'];
}

public function create(ContainerBuilder $container, string $id, array $config, string $userProvider, ?string $defaultEntryPoint)
{
return ['provider_id', 'listener_id', $defaultEntryPoint];
}

public function getPosition()
{
return 'form';
}

public function getKey()
{
return 'custom_listener';
}

public function addConfiguration(NodeDefinition $builder)
{
}
}
0