Skip to content

Commit 19ba77a

Browse files
Merge branch '4.4' into 5.0
* 4.4: [Yaml] Throw on unquoted exclamation mark Use supportsClass where possible
2 parents a0f7281 + 3a872ea commit 19ba77a

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
@@ -201,12 +201,17 @@ protected function refreshUser(TokenInterface $token): ?TokenInterface
201201

202202
$userNotFoundByProvider = false;
203203
$userDeauthenticated = false;
204+
$userClass = \get_class($user);
204205

205206
foreach ($this->userProviders as $provider) {
206207
if (!$provider instanceof UserProviderInterface) {
207208
throw new \InvalidArgumentException(sprintf('User provider "%s" must implement "%s".', \get_class($provider), UserProviderInterface::class));
208209
}
209210

211+
if (!$provider->supportsClass($userClass)) {
212+
continue;
213+
}
214+
210215
try {
211216
$refreshedUser = $provider->refreshUser($user);
212217
$newToken = clone $token;
@@ -263,7 +268,7 @@ protected function refreshUser(TokenInterface $token): ?TokenInterface
263268
return null;
264269
}
265270

266-
throw new \RuntimeException(sprintf('There is no user provider for user "%s".', \get_class($user)));
271+
throw new \RuntimeException(sprintf('There is no user provider for user "%s".', $userClass));
267272
}
268273

269274
private function safelyUnserialize(string $serializedToken)

Tests/Firewall/ContextListenerTest.php

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

258258
$this->assertNull($tokenStorage->getToken());
259259
}
@@ -275,44 +275,44 @@ public function testRememberMeGetsCanceledIfTokenIsDeauthenticated()
275275
$rememberMeServices = $this->createMock(RememberMeServicesInterface::class);
276276
$rememberMeServices->expects($this->once())->method('loginFail');
277277

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

280280
$this->assertNull($tokenStorage->getToken());
281281
}
282282

283283
public function testTryAllUserProvidersUntilASupportingUserProviderIsFound()
284284
{
285285
$refreshedUser = new User('foobar', 'baz');
286-
$tokenStorage = $this->handleEventWithPreviousSession([new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser)], $refreshedUser);
286+
$tokenStorage = $this->handleEventWithPreviousSession([new NotSupportingUserProvider(true), new NotSupportingUserProvider(false), new SupportingUserProvider($refreshedUser)], $refreshedUser);
287287

288288
$this->assertSame($refreshedUser, $tokenStorage->getToken()->getUser());
289289
}
290290

291291
public function testNextSupportingUserProviderIsTriedIfPreviousSupportingUserProviderDidNotLoadTheUser()
292292
{
293293
$refreshedUser = new User('foobar', 'baz');
294-
$tokenStorage = $this->handleEventWithPreviousSession([new SupportingUserProvider(), new SupportingUserProvider($refreshedUser)], $refreshedUser);
294+
$tokenStorage = $this->handleEventWithPreviousSession([new NotSupportingUserProvider(true), new NotSupportingUserProvider(false), new SupportingUserProvider($refreshedUser)], $refreshedUser);
295295

296296
$this->assertSame($refreshedUser, $tokenStorage->getToken()->getUser());
297297
}
298298

299299
public function testTokenIsSetToNullIfNoUserWasLoadedByTheRegisteredUserProviders()
300300
{
301-
$tokenStorage = $this->handleEventWithPreviousSession([new NotSupportingUserProvider(), new SupportingUserProvider()]);
301+
$tokenStorage = $this->handleEventWithPreviousSession([new NotSupportingUserProvider(true), new NotSupportingUserProvider(false), new SupportingUserProvider()]);
302302

303303
$this->assertNull($tokenStorage->getToken());
304304
}
305305

306306
public function testRuntimeExceptionIsThrownIfNoSupportingUserProviderWasRegistered()
307307
{
308308
$this->expectException('RuntimeException');
309-
$this->handleEventWithPreviousSession([new NotSupportingUserProvider(), new NotSupportingUserProvider()]);
309+
$this->handleEventWithPreviousSession([new NotSupportingUserProvider(false), new NotSupportingUserProvider(true)]);
310310
}
311311

312312
public function testAcceptsProvidersAsTraversable()
313313
{
314314
$refreshedUser = new User('foobar', 'baz');
315-
$tokenStorage = $this->handleEventWithPreviousSession(new \ArrayObject([new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser)]), $refreshedUser);
315+
$tokenStorage = $this->handleEventWithPreviousSession(new \ArrayObject([new NotSupportingUserProvider(true), new NotSupportingUserProvider(false), new SupportingUserProvider($refreshedUser)]), $refreshedUser);
316316

317317
$this->assertSame($refreshedUser, $tokenStorage->getToken()->getUser());
318318
}
@@ -338,7 +338,7 @@ public function testDeauthenticatedEvent()
338338
$this->assertNotEquals($event->getRefreshedToken()->getUser(), $user);
339339
});
340340

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

344344
$this->assertNull($tokenStorage->getToken());
@@ -422,14 +422,26 @@ private function handleEventWithPreviousSession($userProviders, UserInterface $u
422422

423423
class NotSupportingUserProvider implements UserProviderInterface
424424
{
425+
/** @var bool */
426+
private $throwsUnsupportedException;
427+
428+
public function __construct($throwsUnsupportedException)
429+
{
430+
$this->throwsUnsupportedException = $throwsUnsupportedException;
431+
}
432+
425433
public function loadUserByUsername($username): UserInterface
426434
{
427435
throw new UsernameNotFoundException();
428436
}
429437

430438
public function refreshUser(UserInterface $user): UserInterface
431439
{
432-
throw new UnsupportedUserException();
440+
if ($this->throwsUnsupportedException) {
441+
throw new UnsupportedUserException();
442+
}
443+
444+
return $user;
433445
}
434446

435447
public function supportsClass($class): bool

0 commit comments

Comments
 (0)