Skip to content

Commit 2e7746b

Browse files
Merge branch '4.1'
* 4.1: (27 commits) Added the Code of Conduct file do not override custom access decision configs [Security] Do not deauthenticate user when the first refreshed user has changed fix a return type hint invalidate stale commits for PRs too add missing cache prefix seed attribute to XSD fix command description Fix class documentation [Validator] Add a missing translation [FrameworkBundle] Fix 3.4 tests [DI] fix dumping inline services again Rename consumer to receiver Register messenger before the profiler Fix phpdocs [EventDispatcher] Remove template method in test case Added LB translation for #27993 (UUID validator message translation) Replace deprecated validateValue with validate [FWBundle] Automatically enable PropertyInfo when using Flex [Process] fix locking of pipe files on Windows Correct PHPDoc type for float ttl ...
2 parents 7b3e687 + 46fc0b1 commit 2e7746b

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

Firewall/ContextListener.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ protected function refreshUser(TokenInterface $token)
161161
}
162162

163163
$userNotFoundByProvider = false;
164+
$userDeauthenticated = false;
164165

165166
foreach ($this->userProviders as $provider) {
166167
if (!$provider instanceof UserProviderInterface) {
@@ -169,17 +170,26 @@ protected function refreshUser(TokenInterface $token)
169170

170171
try {
171172
$refreshedUser = $provider->refreshUser($user);
172-
$token->setUser($refreshedUser);
173+
$newToken = unserialize(serialize($token));
174+
$newToken->setUser($refreshedUser);
173175

174176
// tokens can be deauthenticated if the user has been changed.
175-
if (!$token->isAuthenticated()) {
176-
if (null !== $this->logger) {
177-
$this->logger->debug('Token was deauthenticated after trying to refresh it.', array('username' => $refreshedUser->getUsername(), 'provider' => \get_class($provider)));
177+
if (!$newToken->isAuthenticated()) {
178+
if ($this->logoutOnUserChange) {
179+
$userDeauthenticated = true;
180+
181+
if (null !== $this->logger) {
182+
$this->logger->debug('Cannot refresh token because user has changed.', array('username' => $refreshedUser->getUsername(), 'provider' => \get_class($provider)));
183+
}
184+
185+
continue;
178186
}
179187

180188
return null;
181189
}
182190

191+
$token->setUser($refreshedUser);
192+
183193
if (null !== $this->logger) {
184194
$context = array('provider' => \get_class($provider), 'username' => $refreshedUser->getUsername());
185195

@@ -205,6 +215,14 @@ protected function refreshUser(TokenInterface $token)
205215
}
206216
}
207217

218+
if ($userDeauthenticated) {
219+
if (null !== $this->logger) {
220+
$this->logger->debug('Token was deauthenticated after trying to refresh it.');
221+
}
222+
223+
return null;
224+
}
225+
208226
if ($userNotFoundByProvider) {
209227
return null;
210228
}

Tests/Firewall/ContextListenerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@ public function testIfTokenIsDeauthenticated()
260260
$this->assertNull($tokenStorage->getToken());
261261
}
262262

263+
public function testIfTokenIsNotDeauthenticated()
264+
{
265+
$tokenStorage = new TokenStorage();
266+
$badRefreshedUser = new User('foobar', 'baz');
267+
$goodRefreshedUser = new User('foobar', 'bar');
268+
$this->handleEventWithPreviousSession($tokenStorage, array(new SupportingUserProvider($badRefreshedUser), new SupportingUserProvider($goodRefreshedUser)), $goodRefreshedUser, true);
269+
$this->assertSame($goodRefreshedUser, $tokenStorage->getToken()->getUser());
270+
}
271+
263272
public function testTryAllUserProvidersUntilASupportingUserProviderIsFound()
264273
{
265274
$tokenStorage = new TokenStorage();

0 commit comments

Comments
 (0)