Skip to content

Commit 2ea4960

Browse files
Merge branch '3.4' into 4.3
* 3.4: Use supportsClass where possible
2 parents 927889a + a10ae32 commit 2ea4960

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

Firewall/ContextListener.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,17 @@ protected function refreshUser(TokenInterface $token)
181181

182182
$userNotFoundByProvider = false;
183183
$userDeauthenticated = false;
184+
$userClass = \get_class($user);
184185

185186
foreach ($this->userProviders as $provider) {
186187
if (!$provider instanceof UserProviderInterface) {
187188
throw new \InvalidArgumentException(sprintf('User provider "%s" must implement "%s".', \get_class($provider), UserProviderInterface::class));
188189
}
189190

191+
if (!$provider->supportsClass($userClass)) {
192+
continue;
193+
}
194+
190195
try {
191196
$refreshedUser = $provider->refreshUser($user);
192197
$newToken = clone $token;
@@ -250,7 +255,7 @@ protected function refreshUser(TokenInterface $token)
250255
return null;
251256
}
252257

253-
throw new \RuntimeException(sprintf('There is no user provider for user "%s".', \get_class($user)));
258+
throw new \RuntimeException(sprintf('There is no user provider for user "%s".', $userClass));
254259
}
255260

256261
private function safelyUnserialize($serializedToken)

Tests/Firewall/ContextListenerTest.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public function testIfTokenIsDeauthenticated()
254254
{
255255
$tokenStorage = new TokenStorage();
256256
$refreshedUser = new User('foobar', 'baz');
257-
$this->handleEventWithPreviousSession($tokenStorage, [new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser)]);
257+
$this->handleEventWithPreviousSession($tokenStorage, [new NotSupportingUserProvider(true), new NotSupportingUserProvider(false), new SupportingUserProvider($refreshedUser)]);
258258

259259
$this->assertNull($tokenStorage->getToken());
260260
}
@@ -276,7 +276,7 @@ public function testRememberMeGetsCanceledIfTokenIsDeauthenticated()
276276
$rememberMeServices = $this->createMock(RememberMeServicesInterface::class);
277277
$rememberMeServices->expects($this->once())->method('loginFail');
278278

279-
$this->handleEventWithPreviousSession($tokenStorage, [new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser)], null, $rememberMeServices);
279+
$this->handleEventWithPreviousSession($tokenStorage, [new NotSupportingUserProvider(true), new NotSupportingUserProvider(false), new SupportingUserProvider($refreshedUser)], null, $rememberMeServices);
280280

281281
$this->assertNull($tokenStorage->getToken());
282282
}
@@ -285,7 +285,7 @@ public function testTryAllUserProvidersUntilASupportingUserProviderIsFound()
285285
{
286286
$tokenStorage = new TokenStorage();
287287
$refreshedUser = new User('foobar', 'baz');
288-
$this->handleEventWithPreviousSession($tokenStorage, [new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser)], $refreshedUser);
288+
$this->handleEventWithPreviousSession($tokenStorage, [new NotSupportingUserProvider(true), new NotSupportingUserProvider(false), new SupportingUserProvider($refreshedUser)], $refreshedUser);
289289

290290
$this->assertSame($refreshedUser, $tokenStorage->getToken()->getUser());
291291
}
@@ -294,30 +294,30 @@ public function testNextSupportingUserProviderIsTriedIfPreviousSupportingUserPro
294294
{
295295
$tokenStorage = new TokenStorage();
296296
$refreshedUser = new User('foobar', 'baz');
297-
$this->handleEventWithPreviousSession($tokenStorage, [new SupportingUserProvider(), new SupportingUserProvider($refreshedUser)], $refreshedUser);
297+
$this->handleEventWithPreviousSession($tokenStorage, [new NotSupportingUserProvider(true), new NotSupportingUserProvider(false), new SupportingUserProvider($refreshedUser)], $refreshedUser);
298298

299299
$this->assertSame($refreshedUser, $tokenStorage->getToken()->getUser());
300300
}
301301

302302
public function testTokenIsSetToNullIfNoUserWasLoadedByTheRegisteredUserProviders()
303303
{
304304
$tokenStorage = new TokenStorage();
305-
$this->handleEventWithPreviousSession($tokenStorage, [new NotSupportingUserProvider(), new SupportingUserProvider()]);
305+
$this->handleEventWithPreviousSession($tokenStorage, [new NotSupportingUserProvider(true), new NotSupportingUserProvider(false), new SupportingUserProvider()]);
306306

307307
$this->assertNull($tokenStorage->getToken());
308308
}
309309

310310
public function testRuntimeExceptionIsThrownIfNoSupportingUserProviderWasRegistered()
311311
{
312312
$this->expectException('RuntimeException');
313-
$this->handleEventWithPreviousSession(new TokenStorage(), [new NotSupportingUserProvider(), new NotSupportingUserProvider()]);
313+
$this->handleEventWithPreviousSession(new TokenStorage(), [new NotSupportingUserProvider(false), new NotSupportingUserProvider(true)]);
314314
}
315315

316316
public function testAcceptsProvidersAsTraversable()
317317
{
318318
$tokenStorage = new TokenStorage();
319319
$refreshedUser = new User('foobar', 'baz');
320-
$this->handleEventWithPreviousSession($tokenStorage, new \ArrayObject([new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser)]), $refreshedUser);
320+
$this->handleEventWithPreviousSession($tokenStorage, new \ArrayObject([new NotSupportingUserProvider(true), new NotSupportingUserProvider(false), new SupportingUserProvider($refreshedUser)]), $refreshedUser);
321321

322322
$this->assertSame($refreshedUser, $tokenStorage->getToken()->getUser());
323323
}
@@ -343,7 +343,7 @@ public function testDeauthenticatedEvent()
343343
$this->assertNotEquals($event->getRefreshedToken()->getUser(), $user);
344344
});
345345

346-
$listener = new ContextListener($tokenStorage, [new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser)], 'context_key', null, $eventDispatcher);
346+
$listener = new ContextListener($tokenStorage, [new NotSupportingUserProvider(true), new NotSupportingUserProvider(false), new SupportingUserProvider($refreshedUser)], 'context_key', null, $eventDispatcher);
347347
$listener(new RequestEvent($this->getMockBuilder(HttpKernelInterface::class)->getMock(), $request, HttpKernelInterface::MASTER_REQUEST));
348348

349349
$this->assertNull($tokenStorage->getToken());
@@ -398,14 +398,26 @@ private function handleEventWithPreviousSession(TokenStorageInterface $tokenStor
398398

399399
class NotSupportingUserProvider implements UserProviderInterface
400400
{
401+
/** @var bool */
402+
private $throwsUnsupportedException;
403+
404+
public function __construct($throwsUnsupportedException)
405+
{
406+
$this->throwsUnsupportedException = $throwsUnsupportedException;
407+
}
408+
401409
public function loadUserByUsername($username)
402410
{
403411
throw new UsernameNotFoundException();
404412
}
405413

406414
public function refreshUser(UserInterface $user)
407415
{
408-
throw new UnsupportedUserException();
416+
if ($this->throwsUnsupportedException) {
417+
throw new UnsupportedUserException();
418+
}
419+
420+
return $user;
409421
}
410422

411423
public function supportsClass($class)

0 commit comments

Comments
 (0)