Skip to content

Commit 982d79f

Browse files
committed
Deprecate FirewallMapInterface::getListeners and FirewallContext::getListeners
1 parent acbd7b4 commit 982d79f

File tree

12 files changed

+131
-23
lines changed

12 files changed

+131
-23
lines changed

src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CHANGELOG
1818
* Display the roles of the logged-in user in the Web Debug Toolbar
1919
* Add the `security.access_decision_manager.strategy_service` option
2020
* Deprecate not configuring explicitly a provider for custom_authenticators when there is more than one registered provider
21+
* Deprecate `FirewallContext::getListeners`, use `FirewallContext::getFirewallListeners` and `FirewallContext::getExceptionListener` instead
2122

2223
5.3
2324
---

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ private function createFirewalls(array $config, ContainerBuilder $container)
320320

321321
$configId = 'security.firewall.map.config.'.$name;
322322

323-
[$matcher, $listeners, $exceptionListener, $logoutListener] = $this->createFirewall($container, $name, $firewall, $authenticationProviders, $providerIds, $configId);
323+
[$matcher, $listeners, $exceptionListener] = $this->createFirewall($container, $name, $firewall, $authenticationProviders, $providerIds, $configId);
324324

325325
$contextId = 'security.firewall.map.context.'.$name;
326326
$isLazy = !$firewall['stateless'] && (!empty($firewall['anonymous']['lazy']) || $firewall['lazy']);
@@ -329,8 +329,7 @@ private function createFirewalls(array $config, ContainerBuilder $container)
329329
$context
330330
->replaceArgument(0, new IteratorArgument($listeners))
331331
->replaceArgument(1, $exceptionListener)
332-
->replaceArgument(2, $logoutListener)
333-
->replaceArgument(3, new Reference($configId))
332+
->replaceArgument(2, new Reference($configId))
334333
;
335334

336335
$contextRefs[$contextId] = new Reference($contextId);
@@ -450,6 +449,7 @@ private function createFirewall(ContainerBuilder $container, string $id, array $
450449
'csrf_token_id' => $firewall['logout']['csrf_token_id'],
451450
'logout_path' => $firewall['logout']['path'],
452451
]);
452+
$listeners[] = new Reference($logoutListenerId);
453453

454454
// add default logout listener
455455
if (isset($firewall['logout']['success_handler'])) {
@@ -599,7 +599,7 @@ private function createFirewall(ContainerBuilder $container, string $id, array $
599599
$config->replaceArgument(10, $listenerKeys);
600600
$config->replaceArgument(11, $firewall['switch_user'] ?? null);
601601

602-
return [$matcher, $listeners, $exceptionListener, null !== $logoutListenerId ? new Reference($logoutListenerId) : null];
602+
return [$matcher, $listeners, $exceptionListener];
603603
}
604604

605605
private function createContextListener(ContainerBuilder $container, string $contextKey, ?string $firewallEventDispatcherId)

src/Symfony/Bundle/SecurityBundle/Resources/config/security.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@
196196
->args([
197197
[],
198198
service('security.exception_listener'),
199-
abstract_arg('LogoutListener'),
200199
abstract_arg('FirewallConfig'),
201200
])
202201

@@ -205,7 +204,6 @@
205204
->args([
206205
[],
207206
service('security.exception_listener'),
208-
abstract_arg('LogoutListener'),
209207
abstract_arg('FirewallConfig'),
210208
service('security.untracked_token_storage'),
211209
])

src/Symfony/Bundle/SecurityBundle/Security/FirewallContext.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ class FirewallContext
2424
{
2525
private $listeners;
2626
private $exceptionListener;
27-
private $logoutListener;
2827
private $config;
2928

30-
public function __construct(iterable $listeners, ExceptionListener $exceptionListener = null, LogoutListener $logoutListener = null, FirewallConfig $config = null)
29+
public function __construct(iterable $listeners, ExceptionListener $exceptionListener = null, FirewallConfig $config = null)
3130
{
3231
$this->listeners = $listeners;
3332
$this->exceptionListener = $exceptionListener;
34-
$this->logoutListener = $logoutListener;
3533
$this->config = $config;
3634
}
3735

@@ -40,7 +38,24 @@ public function getConfig()
4038
return $this->config;
4139
}
4240

41+
/**
42+
* @deprecated since Symfony 5.4, use "getFirewallListeners()" instead
43+
*/
4344
public function getListeners(): iterable
45+
{
46+
if (0 === \func_num_args() || func_get_arg(0)) {
47+
trigger_deprecation('symfony/security-bundle', '5.4', 'The %s() method is deprecated, use getFirewallListeners() instead.', __METHOD__);
48+
}
49+
50+
// Ensure LogoutListener is not included
51+
foreach ($this->listeners as $listener) {
52+
if (!($listener instanceof LogoutListener)) {
53+
yield $listener;
54+
}
55+
}
56+
}
57+
58+
public function getFirewallListeners(): iterable
4459
{
4560
return $this->listeners;
4661
}
@@ -50,8 +65,22 @@ public function getExceptionListener()
5065
return $this->exceptionListener;
5166
}
5267

68+
/**
69+
* @deprecated since Symfony 5.4, use "getFirewallListeners()" instead
70+
*/
5371
public function getLogoutListener()
5472
{
55-
return $this->logoutListener;
73+
if (0 === \func_num_args() || func_get_arg(0)) {
74+
trigger_deprecation('symfony/security-bundle', '5.4', 'The %s() method is deprecated, use getFirewallListeners() instead.', __METHOD__);
75+
}
76+
77+
// Return LogoutListener from listeners
78+
foreach ($this->listeners as $listener) {
79+
if ($listener instanceof LogoutListener) {
80+
return $listener;
81+
}
82+
}
83+
84+
return null;
5685
}
5786
}

src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Container\ContainerInterface;
1515
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
1617
use Symfony\Component\Security\Http\FirewallMapInterface;
1718

1819
/**
@@ -35,13 +36,39 @@ public function __construct(ContainerInterface $container, iterable $map)
3536

3637
public function getListeners(Request $request)
3738
{
39+
if (2 > \func_num_args() || func_get_arg(1)) {
40+
trigger_deprecation('symfony/security-bundle', '5.4', 'The %s() method is deprecated, use getFirewallListeners() or "getExceptionListener()" instead.', __METHOD__);
41+
}
42+
3843
$context = $this->getFirewallContext($request);
3944

4045
if (null === $context) {
4146
return [[], null, null];
4247
}
4348

44-
return [$context->getListeners(), $context->getExceptionListener(), $context->getLogoutListener()];
49+
return [$context->getListeners(false), $context->getExceptionListener(), $context->getLogoutListener(false)];
50+
}
51+
52+
public function getFirewallListeners(Request $request): iterable
53+
{
54+
$context = $this->getFirewallContext($request);
55+
56+
if (null === $context) {
57+
return [];
58+
}
59+
60+
return $context->getFirewallListeners();
61+
}
62+
63+
public function getExceptionListener(Request $request): ?ExceptionListener
64+
{
65+
$context = $this->getFirewallContext($request);
66+
67+
if (null === $context) {
68+
return null;
69+
}
70+
71+
return $context->getExceptionListener();
4572
}
4673

4774
/**

src/Symfony/Bundle/SecurityBundle/Security/LazyFirewallContext.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\Security\Http\Event\LazyResponseEvent;
1717
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
1818
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;
19-
use Symfony\Component\Security\Http\Firewall\LogoutListener;
2019

2120
/**
2221
* Lazily calls authentication listeners when actually required by the access listener.
@@ -27,13 +26,16 @@ class LazyFirewallContext extends FirewallContext
2726
{
2827
private $tokenStorage;
2928

30-
public function __construct(iterable $listeners, ?ExceptionListener $exceptionListener, ?LogoutListener $logoutListener, ?FirewallConfig $config, TokenStorage $tokenStorage)
29+
public function __construct(iterable $listeners, ?ExceptionListener $exceptionListener, ?FirewallConfig $config, TokenStorage $tokenStorage)
3130
{
32-
parent::__construct($listeners, $exceptionListener, $logoutListener, $config);
31+
parent::__construct($listeners, $exceptionListener, $config);
3332

3433
$this->tokenStorage = $tokenStorage;
3534
}
3635

36+
/**
37+
* {@inheritdoc}
38+
*/
3739
public function getListeners(): iterable
3840
{
3941
return [$this];
@@ -45,7 +47,7 @@ public function __invoke(RequestEvent $event)
4547
$request = $event->getRequest();
4648
$lazy = $request->isMethodCacheable();
4749

48-
foreach (parent::getListeners() as $listener) {
50+
foreach (parent::getListeners(false) as $listener) {
4951
if (!$lazy || !$listener instanceof FirewallListenerInterface) {
5052
$listeners[] = $listener;
5153
$lazy = $lazy && $listener instanceof FirewallListenerInterface;

src/Symfony/Bundle/SecurityBundle/Tests/Security/FirewallMapTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testGetListenersWithEmptyMap()
3535

3636
$firewallMap = new FirewallMap($container, $map);
3737

38-
$this->assertEquals([[], null, null], $firewallMap->getListeners($request));
38+
$this->assertEquals([[], null, null], $firewallMap->getListeners($request, false));
3939
$this->assertNull($firewallMap->getFirewallConfig($request));
4040
$this->assertFalse($request->attributes->has(self::ATTRIBUTE_FIREWALL_CONTEXT));
4141
}
@@ -51,7 +51,7 @@ public function testGetListenersWithInvalidParameter()
5151

5252
$firewallMap = new FirewallMap($container, $map);
5353

54-
$this->assertEquals([[], null, null], $firewallMap->getListeners($request));
54+
$this->assertEquals([[], null, null], $firewallMap->getListeners($request, false));
5555
$this->assertNull($firewallMap->getFirewallConfig($request));
5656
$this->assertFalse($request->attributes->has(self::ATTRIBUTE_FIREWALL_CONTEXT));
5757
}
@@ -85,7 +85,7 @@ public function testGetListeners()
8585

8686
$firewallMap = new FirewallMap($container, ['security.firewall.map.context.foo' => $matcher]);
8787

88-
$this->assertEquals([[$listener], $exceptionListener, $logoutListener], $firewallMap->getListeners($request));
88+
$this->assertEquals([[$listener], $exceptionListener, $logoutListener], $firewallMap->getListeners($request, false));
8989
$this->assertEquals($firewallConfig, $firewallMap->getFirewallConfig($request));
9090
$this->assertEquals('security.firewall.map.context.foo', $request->attributes->get(self::ATTRIBUTE_FIREWALL_CONTEXT));
9191
}

src/Symfony/Component/Security/Http/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CHANGELOG
1616
* Deprecate `CookieClearingLogoutHandler`, `SessionLogoutHandler` and `CsrfTokenClearingLogoutHandler`.
1717
Use `CookieClearingLogoutListener`, `SessionLogoutListener` and `CsrfTokenClearingLogoutListener` instead
1818
* Deprecate `PassportInterface`, `UserPassportInterface` and `PassportTrait`, use `Passport` instead
19+
* Deprecate `FirewallMapInterface::getListeners`, use `FirewallMapInterface::getFirewallListeners` and `FirewallMapInterface::getExceptionListener` instead
1920

2021
5.3
2122
---

src/Symfony/Component/Security/Http/Firewall.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function onKernelRequest(RequestEvent $event)
4848
}
4949

5050
// register listeners for this firewall
51-
$listeners = $this->map->getListeners($event->getRequest());
51+
$listeners = $this->map->getListeners($event->getRequest(), false);
5252

5353
$authenticationListeners = $listeners[0];
5454
$exceptionListener = $listeners[1];

src/Symfony/Component/Security/Http/FirewallMap.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,64 @@ class FirewallMap implements FirewallMapInterface
2828

2929
public function add(RequestMatcherInterface $requestMatcher = null, array $listeners = [], ExceptionListener $exceptionListener = null, LogoutListener $logoutListener = null)
3030
{
31-
$this->map[] = [$requestMatcher, $listeners, $exceptionListener, $logoutListener];
31+
$allListeners = $listeners;
32+
33+
if (null !== $logoutListener) {
34+
trigger_deprecation('symfony/security', '5.4', 'Passing the LogoutListener as forth argument is deprecated, add it to $listeners instead.', __METHOD__);
35+
36+
// Ensure LogoutListener is contained in listeners
37+
if (!\in_array($logoutListener, $allListeners)) {
38+
$allListeners[] = $logoutListener;
39+
}
40+
} else {
41+
// Take LogoutListeners from listeners
42+
foreach ($listeners as $listener) {
43+
if ($listener instanceof LogoutListener) {
44+
$logoutListener = $listener;
45+
}
46+
}
47+
}
48+
49+
$this->map[] = [$requestMatcher, $allListeners, $exceptionListener, $listeners, $logoutListener];
3250
}
3351

3452
/**
3553
* {@inheritdoc}
3654
*/
3755
public function getListeners(Request $request)
3856
{
57+
if (2 > \func_num_args() || func_get_arg(1)) {
58+
trigger_deprecation('symfony/security', '5.4', 'The %s() method is deprecated, use getFirewallListeners() or "getExceptionListener()" instead.', __METHOD__);
59+
}
60+
3961
foreach ($this->map as $elements) {
4062
if (null === $elements[0] || $elements[0]->matches($request)) {
41-
return [$elements[1], $elements[2], $elements[3]];
63+
return [$elements[3], $elements[2], $elements[4]];
4264
}
4365
}
4466

4567
return [[], null, null];
4668
}
69+
70+
public function getFirewallListeners(Request $request): iterable
71+
{
72+
foreach ($this->map as $elements) {
73+
if (null === $elements[0] || $elements[0]->matches($request)) {
74+
return $elements[1];
75+
}
76+
}
77+
78+
return [];
79+
}
80+
81+
public function getExceptionListener(Request $request): ?ExceptionListener
82+
{
83+
foreach ($this->map as $elements) {
84+
if (null === $elements[0] || $elements[0]->matches($request)) {
85+
return $elements[2];
86+
}
87+
}
88+
89+
return null;
90+
}
4791
}

src/Symfony/Component/Security/Http/FirewallMapInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
namespace Symfony\Component\Security\Http;
1313

1414
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
1516

1617
/**
1718
* This interface must be implemented by firewall maps.
1819
*
20+
* @method iterable getFirewallListeners(Request $request) returns the sorted list of firewall listeners
21+
* @method ExceptionListener|null getExceptionListener(Request $request) returns the firewall's exception listener
22+
*
1923
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
2024
*/
2125
interface FirewallMapInterface
@@ -34,6 +38,8 @@ interface FirewallMapInterface
3438
* must be null.
3539
*
3640
* @return array of the format [[AuthenticationListener], ExceptionListener, LogoutListener]
41+
*
42+
* @deprecated since Symfony 5.4, use "getFirewallListeners()" instead
3743
*/
3844
public function getListeners(Request $request);
3945
}

src/Symfony/Component/Security/Http/Tests/FirewallMapTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function testGetListenersWithAnEntryHavingNoRequestMatcher()
9090

9191
$map->add($tooLateMatcher, [function () {}]);
9292

93-
[$listeners, $exception] = $map->getListeners($request);
93+
[$listeners, $exception] = $map->getListeners($request, false);
9494

9595
$this->assertEquals([$theListener], $listeners);
9696
$this->assertEquals($theException, $exception);
@@ -112,7 +112,7 @@ public function testGetListenersWithNoMatchingEntry()
112112

113113
$map->add($notMatchingMatcher, [function () {}]);
114114

115-
[$listeners, $exception] = $map->getListeners($request);
115+
[$listeners, $exception] = $map->getListeners($request, false);
116116

117117
$this->assertEquals([], $listeners);
118118
$this->assertNull($exception);

0 commit comments

Comments
 (0)