Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit af97cf9

Browse files
Merge branch '4.3' into 4.4
* 4.3: [Yaml] Throw on unquoted exclamation mark Use supportsClass where possible
2 parents f57c927 + d3ddd41 commit af97cf9

File tree

4 files changed

+89
-13
lines changed

4 files changed

+89
-13
lines changed

Core/Tests/User/ChainUserProviderTest.php

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,62 @@ public function testRefreshUser()
7070
$provider1 = $this->getProvider();
7171
$provider1
7272
->expects($this->once())
73-
->method('refreshUser')
74-
->willThrowException(new UnsupportedUserException('unsupported'))
73+
->method('supportsClass')
74+
->willReturn(false)
7575
;
7676

7777
$provider2 = $this->getProvider();
78+
$provider2
79+
->expects($this->once())
80+
->method('supportsClass')
81+
->willReturn(true)
82+
;
83+
7884
$provider2
85+
->expects($this->once())
86+
->method('refreshUser')
87+
->willThrowException(new UnsupportedUserException('unsupported'))
88+
;
89+
90+
$provider3 = $this->getProvider();
91+
$provider3
92+
->expects($this->once())
93+
->method('supportsClass')
94+
->willReturn(true)
95+
;
96+
97+
$provider3
7998
->expects($this->once())
8099
->method('refreshUser')
81100
->willReturn($account = $this->getAccount())
82101
;
83102

84-
$provider = new ChainUserProvider([$provider1, $provider2]);
103+
$provider = new ChainUserProvider([$provider1, $provider2, $provider3]);
85104
$this->assertSame($account, $provider->refreshUser($this->getAccount()));
86105
}
87106

88107
public function testRefreshUserAgain()
89108
{
90109
$provider1 = $this->getProvider();
110+
$provider1
111+
->expects($this->once())
112+
->method('supportsClass')
113+
->willReturn(true)
114+
;
115+
91116
$provider1
92117
->expects($this->once())
93118
->method('refreshUser')
94119
->willThrowException(new UsernameNotFoundException('not found'))
95120
;
96121

97122
$provider2 = $this->getProvider();
123+
$provider2
124+
->expects($this->once())
125+
->method('supportsClass')
126+
->willReturn(true)
127+
;
128+
98129
$provider2
99130
->expects($this->once())
100131
->method('refreshUser')
@@ -109,13 +140,25 @@ public function testRefreshUserThrowsUnsupportedUserException()
109140
{
110141
$this->expectException('Symfony\Component\Security\Core\Exception\UnsupportedUserException');
111142
$provider1 = $this->getProvider();
143+
$provider1
144+
->expects($this->once())
145+
->method('supportsClass')
146+
->willReturn(true)
147+
;
148+
112149
$provider1
113150
->expects($this->once())
114151
->method('refreshUser')
115152
->willThrowException(new UnsupportedUserException('unsupported'))
116153
;
117154

118155
$provider2 = $this->getProvider();
156+
$provider2
157+
->expects($this->once())
158+
->method('supportsClass')
159+
->willReturn(true)
160+
;
161+
119162
$provider2
120163
->expects($this->once())
121164
->method('refreshUser')
@@ -173,13 +216,25 @@ public function testSupportsClassWhenNotSupported()
173216
public function testAcceptsTraversable()
174217
{
175218
$provider1 = $this->getProvider();
219+
$provider1
220+
->expects($this->once())
221+
->method('supportsClass')
222+
->willReturn(true)
223+
;
224+
176225
$provider1
177226
->expects($this->once())
178227
->method('refreshUser')
179228
->willThrowException(new UnsupportedUserException('unsupported'))
180229
;
181230

182231
$provider2 = $this->getProvider();
232+
$provider2
233+
->expects($this->once())
234+
->method('supportsClass')
235+
->willReturn(true)
236+
;
237+
183238
$provider2
184239
->expects($this->once())
185240
->method('refreshUser')

Core/User/ChainUserProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public function refreshUser(UserInterface $user)
7373

7474
foreach ($this->providers as $provider) {
7575
try {
76+
if (!$provider->supportsClass(\get_class($user))) {
77+
continue;
78+
}
79+
7680
return $provider->refreshUser($user);
7781
} catch (UnsupportedUserException $e) {
7882
// try next one

Http/Firewall/ContextListener.php

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

219219
$userNotFoundByProvider = false;
220220
$userDeauthenticated = false;
221+
$userClass = \get_class($user);
221222

222223
foreach ($this->userProviders as $provider) {
223224
if (!$provider instanceof UserProviderInterface) {
224225
throw new \InvalidArgumentException(sprintf('User provider "%s" must implement "%s".', \get_class($provider), UserProviderInterface::class));
225226
}
226227

228+
if (!$provider->supportsClass($userClass)) {
229+
continue;
230+
}
231+
227232
try {
228233
$refreshedUser = $provider->refreshUser($user);
229234
$newToken = clone $token;
@@ -287,7 +292,7 @@ protected function refreshUser(TokenInterface $token)
287292
return null;
288293
}
289294

290-
throw new \RuntimeException(sprintf('There is no user provider for user "%s".', \get_class($user)));
295+
throw new \RuntimeException(sprintf('There is no user provider for user "%s".', $userClass));
291296
}
292297

293298
private function safelyUnserialize(string $serializedToken)

Http/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());
@@ -431,14 +431,26 @@ private function handleEventWithPreviousSession($userProviders, UserInterface $u
431431

432432
class NotSupportingUserProvider implements UserProviderInterface
433433
{
434+
/** @var bool */
435+
private $throwsUnsupportedException;
436+
437+
public function __construct($throwsUnsupportedException)
438+
{
439+
$this->throwsUnsupportedException = $throwsUnsupportedException;
440+
}
441+
434442
public function loadUserByUsername($username): UserInterface
435443
{
436444
throw new UsernameNotFoundException();
437445
}
438446

439447
public function refreshUser(UserInterface $user): UserInterface
440448
{
441-
throw new UnsupportedUserException();
449+
if ($this->throwsUnsupportedException) {
450+
throw new UnsupportedUserException();
451+
}
452+
453+
return $user;
442454
}
443455

444456
public function supportsClass($class): bool

0 commit comments

Comments
 (0)