Skip to content

Commit b862156

Browse files
author
Robin Chalas
committed
[Security][SecurityBundle] FirewallMap/FirewallContext deprecations
1 parent 57a1dd1 commit b862156

File tree

7 files changed

+68
-4
lines changed

7 files changed

+68
-4
lines changed

UPGRADE-4.2.md

+8
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ Security
55
--------
66

77
* Using the `has_role()` function in security expressions is deprecated, use the `is_granted()` function instead.
8+
* Not returning an array of 3 elements from `FirewallMapInterface::getListeners()` is deprecated, the 3rd element
9+
must be an instance of `LogoutListener` or `null`.
10+
11+
SecurityBundle
12+
--------------
13+
14+
* Passing a `FirewallConfig` instance as 3rd argument to the `FirewallContext` constructor is deprecated,
15+
pass a `LogoutListener` instance instead.

UPGRADE-5.0.md

+4
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,17 @@ Security
7878
* The `ContextListener::setLogoutOnUserChange()` method has been removed.
7979
* The `Symfony\Component\Security\Core\User\AdvancedUserInterface` has been removed.
8080
* The `ExpressionVoter::addExpressionLanguageProvider()` method has been removed.
81+
* The `FirewallMapInterface::getListeners()` method must return an array of 3 elements,
82+
the 3rd one must be either a `LogoutListener` instance or `null`.
8183

8284
SecurityBundle
8385
--------------
8486

8587
* The `logout_on_user_change` firewall option has been removed.
8688
* The `switch_user.stateless` firewall option has been removed.
8789
* The `SecurityUserValueResolver` class has been removed.
90+
* Passing a `FirewallConfig` instance as 3rd argument to the `FirewallContext` constructor
91+
now throws a `\TypeError`, pass a `LogoutListener` instance instead.
8892

8993
Translation
9094
-----------

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

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function __construct(iterable $listeners, ExceptionListener $exceptionLis
3636
$this->exceptionListener = $exceptionListener;
3737
if ($logoutListener instanceof FirewallConfig) {
3838
$this->config = $logoutListener;
39+
@trigger_error('Passing an instance of %s as 3rd argument to %s() is deprecated since Symfony 4.2. Pass a %s instance instead.', FirewallConfig::class, __METHOD__, LogoutListener::class, E_USER_DEPRECATED);
3940
} elseif (null === $logoutListener || $logoutListener instanceof LogoutListener) {
4041
$this->logoutListener = $logoutListener;
4142
$this->config = $config;

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

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public function testGetters()
4040
$this->assertEquals($config, $context->getConfig());
4141
}
4242

43+
/**
44+
* @group legacy
45+
* @expectedDeprecation Passing an instance of Symfony\Bundle\SecurityBundle\Security\FirewallConfig as 3rd argument to Symfony\Bundle\SecurityBundle\Security\FirewallContext::__construct() is deprecated since Symfony 4.2. Pass a Symfony\Component\Security\Http\Firewall\LogoutListener instance instead.
46+
*/
47+
public function testFirewallConfigAs3rdConstructorArgument()
48+
{
49+
new FirewallContext(array(), $this->getExceptionListenerMock(), new FirewallConfig('main', 'user_checker', 'request_matcher'));
50+
}
51+
4352
private function getExceptionListenerMock()
4453
{
4554
return $this

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
1717
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1818
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19+
use Symfony\Component\Security\Http\Firewall\LogoutListener;
1920

2021
/**
2122
* Firewall uses a FirewallMap to register security listeners for the given
@@ -49,9 +50,14 @@ public function onKernelRequest(GetResponseEvent $event)
4950
// register listeners for this firewall
5051
$listeners = $this->map->getListeners($event->getRequest());
5152

53+
if (3 !== \count($listeners)) {
54+
@trigger_error(sprintf('Not returning an array of 3 elements from %s::getListeners() is deprecated since Symfony 4.2, the 3rd element must be an instance of %s or null.', FirewallMapInterface::class, LogoutListener::class), E_USER_DEPRECATED);
55+
$listeners[2] = null;
56+
}
57+
5258
$authenticationListeners = $listeners[0];
5359
$exceptionListener = $listeners[1];
54-
$logoutListener = isset($listeners[2]) ? $listeners[2] : null;
60+
$logoutListener = $listeners[2];
5561

5662
if (null !== $exceptionListener) {
5763
$this->exceptionListeners[$event->getRequest()] = $exceptionListener;

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ interface FirewallMapInterface
3030
* If there is no exception listener, the second element of the outer array
3131
* must be null.
3232
*
33-
* @return array of the format array(array(AuthenticationListener), ExceptionListener)
33+
* If there is no logout listener, the third element of the outer array
34+
* must be null.
35+
*
36+
* @return array of the format array(array(AuthenticationListener), ExceptionListener, LogoutListener)
3437
*/
3538
public function getListeners(Request $request);
3639
}

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

+35-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
namespace Symfony\Component\Security\Http\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16+
use Symfony\Component\HttpFoundation\Request;
1517
use Symfony\Component\HttpFoundation\Response;
1618
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1719
use Symfony\Component\HttpKernel\HttpKernelInterface;
1820
use Symfony\Component\Security\Http\Firewall;
21+
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
22+
use Symfony\Component\Security\Http\FirewallMapInterface;
1923

2024
class FirewallTest extends TestCase
2125
{
@@ -37,7 +41,7 @@ public function testOnKernelRequestRegistersExceptionListener()
3741
->expects($this->once())
3842
->method('getListeners')
3943
->with($this->equalTo($request))
40-
->will($this->returnValue(array(array(), $listener)))
44+
->will($this->returnValue(array(array(), $listener, null)))
4145
;
4246

4347
$event = new GetResponseEvent($this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(), $request, HttpKernelInterface::MASTER_REQUEST);
@@ -66,7 +70,7 @@ public function testOnKernelRequestStopsWhenThereIsAResponse()
6670
$map
6771
->expects($this->once())
6872
->method('getListeners')
69-
->will($this->returnValue(array(array($first, $second), null)))
73+
->will($this->returnValue(array(array($first, $second), null, null)))
7074
;
7175

7276
$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
@@ -107,4 +111,33 @@ public function testOnKernelRequestWithSubRequest()
107111

108112
$this->assertFalse($event->hasResponse());
109113
}
114+
115+
/**
116+
* @group legacy
117+
* @expectedDeprecation Not returning an array of 3 elements from Symfony\Component\Security\Http\FirewallMapInterface::getListeners() is deprecated since Symfony 4.2, the 3rd element must be an instance of Symfony\Component\Security\Http\Firewall\LogoutListener or null.
118+
*/
119+
public function testMissingLogoutListener()
120+
{
121+
$dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
122+
123+
$listener = $this->getMockBuilder(ExceptionListener::class)->disableOriginalConstructor()->getMock();
124+
$listener
125+
->expects($this->once())
126+
->method('register')
127+
->with($this->equalTo($dispatcher))
128+
;
129+
130+
$request = new Request();
131+
132+
$map = $this->getMockBuilder(FirewallMapInterface::class)->getMock();
133+
$map
134+
->expects($this->once())
135+
->method('getListeners')
136+
->with($this->equalTo($request))
137+
->willReturn(array(array(), $listener))
138+
;
139+
140+
$firewall = new Firewall($map, $dispatcher);
141+
$firewall->onKernelRequest(new GetResponseEvent($this->getMockBuilder(HttpKernelInterface::class)->getMock(), $request, HttpKernelInterface::MASTER_REQUEST));
142+
}
110143
}

0 commit comments

Comments
 (0)