8000 [Security] deprecate multiple providers in context listener · symfony/symfony@66f295b · GitHub
[go: up one dir, main page]

Skip to content

Commit 66f295b

Browse files
committed
[Security] deprecate multiple providers in context listener
Passing multiple user providers to the context listener does not make much sense. The listener is only responsible to refresh users for a particular firewall. Thus, it must only be aware of the user provider for this particular firewall.
1 parent 29b5a6e commit 66f295b

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

UPGRADE-3.3.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ Process
9494
Security
9595
--------
9696

97+
* Deprecated the ability to pass multiple user providers to the `ContextListener`. Pass only the user provider responsible
98+
for the active firewall instead.
99+
97100
* The `RoleInterface` has been deprecated. Extend the `Symfony\Component\Security\Core\Role\Role`
98101
class in your custom role implementations instead.
99102

UPGRADE-4.0.md

Lines changed: 3 ad 8000 ditions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ Process
252252
Security
253253
--------
254254

255+
* Dropped support for passing multiple user providers to the `ContextListener`. Pass only the user provider responsible
256+
for the active firewall instead.
257+
255258
* The `RoleInterface` has been removed. Extend the `Symfony\Component\Security\Core\Role\Role`
256259
class instead.
257260

src/Symfony/Component/Security/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
3.3.0
5+
-----
6+
7+
* Deprecated the ability to pass multiple user providers to the `ContextListener`. Pass only the user provider responsible
8+
for the active firewall instead.
9+
410
3.2.0
511
-----
612

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,26 @@ class ContextListener implements ListenerInterface
4444
private $registered;
4545
private $trustResolver;
4646

47-
public function __construct(TokenStorageInterface $tokenStorage, array $userProviders, $contextKey, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null, AuthenticationTrustResolverInterface $trustResolver = null)
47+
/**
48+
* @param TokenStorageInterface $tokenStorage
49+
* @param UserProviderInterface|UserProviderInterface[] $userProviders
50+
* @param string $contextKey
51+
* @param LoggerInterface|null $logger
52+
* @param EventDispatcherInterface|null $dispatcher
53+
* @param AuthenticationTrustResolverInterface|null $trustResolver
54+
*/
55+
public function __construct(TokenStorageInterface $tokenStorage, $userProviders, $contextKey, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null, AuthenticationTrustResolverInterface $trustResolver = null)
4856
{
4957
if (empty($contextKey)) {
5058
throw new \InvalidArgumentException('$contextKey must not be empty.');
5159
}
5260

61+
if (is_array($userProviders)) {
62+
@trigger_error(sprintf('Being able to pass multiple user providers to the constructor of %s is deprecated since version 3.3 and will not be supported anymore in 4.0. Only pass the user provider for the current firewall context instead.', __CLASS__), E_USER_DEPRECATED);
63+
} else {
64+
$userProviders = array($userProviders);
65+
}
66+
5367
foreach ($userProviders as $userProvider) {
5468
if (!$userProvider instanceof UserProviderInterface) {
5569
throw new \InvalidArgumentException(sprintf('User provider "%s" must implement "Symfony\Component\Security\Core\User\UserProviderInterface".', get_class($userProvider)));

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
2323
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
2424
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
25+
use Symfony\Component\Security\Core\User\UserProviderInterface;
2526
use Symfony\Component\Security\Http\Firewall\ContextListener;
2627
use Symfony\Component\EventDispatcher\EventDispatcher;
2728

@@ -35,12 +36,13 @@ public function testItRequiresContextKey()
3536
{
3637
new ContextListener(
3738
$this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(),
38-
array(),
39+
$this->getMockBuilder(UserProviderInterface::class)->getMock(),
3940
''
4041
);
4142
}
4243

4344
/**
45+
* @group legacy
4446
* @expectedException \InvalidArgumentException
4547
* @expectedExceptionMessage User provider "stdClass" must implement "Symfony\Component\Security\Core\User\UserProviderInterface
4648
*/
@@ -109,7 +111,7 @@ public function testOnKernelResponseWithoutSession()
109111
new Response()
110112
);
111113

112-
$listener = new ContextListener($tokenStorage, array(), 'session', null, new EventDispatcher());
114+
$listener = new ContextListener($tokenStorage, $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'session', null, new EventDispatcher());
113115
$listener->onKernelResponse($event);
114116

115117
$this->assertTrue($session->isStarted());
@@ -128,7 +130,7 @@ public function testOnKernelResponseWithoutSessionNorToken()
128130
new Response()
129131
);
130132

131-
$listener = new ContextListener(new TokenStorage(), array(), 'session', null, new EventDispatcher());
133+
$listener = new ContextListener(new TokenStorage(), $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'session', null, new EventDispatcher());
132134
$listener->onKernelResponse($event);
133135

134136
$this->assertFalse($session->isStarted());
@@ -163,7 +165,7 @@ public function testInvalidTokenInSession($token)
163165
->method('setToken')
164166
->with(null);
165167

166-
$listener = new ContextListener($tokenStorage, array(), 'key123');
168+
$listener = new ContextListener($tokenStorage, $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'key123');
167169
$listener->handle($event);
168170
}
169171

@@ -184,7 +186,7 @@ public function testHandleAddsKernelResponseListener()
184186
->disableOriginalConstructor()
185187
->getMock();
186188

187-
$listener = new ContextListener($tokenStorage, array(), 'key123', null, $dispatcher);
189+
$listener = new ContextListener($tokenStorage, $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'key123', null, $dispatcher);
188190

189191
$event->expects($this->any())
190192
->method('isMasterRequest')
@@ -208,7 +210,7 @@ public function testOnKernelResponseListenerRemovesItself()
208210
->disableOriginalConstructor()
209211
->getMock();
210212

211-
$listener = new ContextListener($tokenStorage, array(), 'key123', null, $dispatcher);
213+
$listener = new ContextListener($tokenStorage, $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'key123', null, $dispatcher);
212214

213215
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
214216
$request->expects($this->any())
@@ -242,7 +244,7 @@ public function testHandleRemovesTokenIfNoPreviousSessionWasFound()
242244
$tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
243245
$tokenStorage->expects($this->once())->method('setToken')->with(null);
244246

245-
$listener = new ContextListener($tokenStorage, array(), 'key123');
247+
$listener = new ContextListener($tokenStorage, $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'key123');
246248
$listener->handle($event);
247249
}
248250

@@ -268,7 +270,7 @@ protected function runSessionOnKernelResponse($newToken, $original = null)
268270
new Response()
269271
);
270272

271-
$listener = new ContextListener($tokenStorage, array(), 'session', null, new EventDispatcher());
273+
$listener = new ContextListener($tokenStorage, $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'session', null, new EventDispatcher());
272274
$listener->onKernelResponse($event);
273275

274276
return $session;

0 commit comments

Comments
 (0)
0