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

Commit d3ddd41

Browse files
Merge branch '3.4' into 4.3
* 3.4: Use supportsClass where possible
2 parents 8f9ef6f + 0a2ccc2 commit d3ddd41

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
@@ -68,31 +68,62 @@ public function testRefreshUser()
6868
$provider1 = $this->getProvider();
6969
$provider1
7070
->expects($this->once())
71-
->method('refreshUser')
72-
->willThrowException(new UnsupportedUserException('unsupported'))
71+
->method('supportsClass')
72+
->willReturn(false)
7373
;
7474

7575
$provider2 = $this->getProvider();
76+
$provider2
77+
->expects($this->once())
78+
->method('supportsClass')
79+
->willReturn(true)
80+
;
81+
7682
$provider2
83+
->expects($this->once())
84+
->method('refreshUser')
85+
->willThrowException(new UnsupportedUserException('unsupported'))
86+
;
87+
88+
$provider3 = $this->getProvider();
89+
$provider3
90+
->expects($this->once())
91+
->method('supportsClass')
92+
->willReturn(true)
93+
;
94+
95+
$provider3
7796
->expects($this->once())
7897
->method('refreshUser')
7998
->willReturn($account = $this->getAccount())
8099
;
81100

82-
$provider = new ChainUserProvider([$provider1, $provider2]);
101+
$provider = new ChainUserProvider([$provider1, $provider2, $provider3]);
83102
$this->assertSame($account, $provider->refreshUser($this->getAccount()));
84103
}
85104

86105
public function testRefreshUserAgain()
87106
{
88107
$provider1 = $this->getProvider();
108+
$provider1
109+
->expects($this->once())
110+
->method('supportsClass')
111+
->willReturn(true)
112+
;
113+
89114
$provider1
90115
->expects($this->once())
91116
->method('refreshUser')
92117
->willThrowException(new UsernameNotFoundException('not found'))
93118
;
94119

95120
$provider2 = $this->getProvider();
121+
$provider2
122+
->expects($this->once())
123+
->method('supportsClass')
124+
->willReturn(true)
125+
;
126+
96127
$provider2
97128
->expects($this->once())
98129
->method('refreshUser')
@@ -107,13 +138,25 @@ public function testRefreshUserThrowsUnsupportedUserException()
107138
{
108139
$this->expectException('Symfony\Component\Security\Core\Exception\UnsupportedUserException');
109140
$provider1 = $this->getProvider();
141+
$provider1
142+
->expects($this->once())
143+
->method('supportsClass')
144+
->willReturn(true)
145+
;
146+
110147
$provider1
111148
->expects($this->once())
112149
->method('refreshUser')
113150
->willThrowException(new UnsupportedUserException('unsupported'))
114151
;
115152

116153
$provider2 = $this->getProvider();
154+
$provider2
155+
->expects($this->once())
156+
->method('supportsClass')
157+
->willReturn(true)
158+
;
159+
117160
$provider2
118161
->expects($this->once())
119162
->method('refreshUser')
@@ -171,13 +214,25 @@ public function testSupportsClassWhenNotSupported()
171214
public function testAcceptsTraversable()
172215
{
173216
$provider1 = $this->getProvider();
217+
$provider1
218+
->expects($this->once())
219+
->method('supportsClass')
220+
->willReturn(true)
221+
;
222+
174223
$provider1
175224
->expects($this->once())
176225
->method('refreshUser')
177226
->willThrowException(new UnsupportedUserException('unsupported'))
178227
;
179228

180229
$provider2 = $this->getProvider();
230+
$provider2
231+
->expects($this->once())
232+
->method('supportsClass')
233+
->willReturn(true)
234+
;
235+
181236
$provider2
182237
->expects($this->once())
183238
->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
@@ -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)

Http/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)