Skip to content

Commit 0b6887e

Browse files
committed
bug #8997 [Security] Fixed problem with losing ROLE_PREVIOUS_ADMIN role. (pawaclawczyk)
This PR was squashed before being merged into the 2.3 branch (closes #8997). Discussion ---------- [Security] Fixed problem with losing ROLE_PREVIOUS_ADMIN role. <table> <tr> <td><b>Q</b></td> <td><b>A</b></td> </tr> <tr> <td>Bug fix?</td> <td>yes</td> </tr> <tr> <td>New feature</td> <td>no</td> </tr> <tr> <td>BC breaks?</td> <td>no</td> </tr> <tr> <td>Deprecations?</td> <td>no</td> </tr> <tr> <td>Tests pass?</td> <td>yes</td> </tr> <tr> <td>Fixed tickets</td> <td>#3085, #8974</td> </tr> <tr> <td>License</td> <td>MIT</td> </tr> <tr> <td>Doc PR</td> <td>n/a</td> </tr> </table> Problem occurs while user is impersonated. Authentication process generates new token and doeas not preserve role ```ROLE_PREVIOUS_ADMIN```. Ex. when parameter ```security.always_authenticate_before_granting``` is enabled. Commits ------- a7baa3b [Security] Fixed problem with losing ROLE_PREVIOUS_ADMIN role.
2 parents 29006c2 + 7f9a64b commit 0b6887e

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

Authentication/Provider/UserAuthenticationProvider.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
2020
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2121
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
22+
use Symfony\Component\Security\Core\Role\SwitchUserRole;
2223

2324
/**
2425
* UserProviderInterface retrieves users for UsernamePasswordToken tokens.
@@ -92,7 +93,7 @@ public function authenticate(TokenInterface $token)
9293
throw $e;
9394
}
9495

95-
$authenticatedToken = new UsernamePasswordToken($user, $token->getCredentials(), $this->providerKey, $user->getRoles());
96+
$authenticatedToken = new UsernamePasswordToken($user, $token->getCredentials(), $this->providerKey, $this->getRoles($user, $token));
9697
$authenticatedToken->setAttributes($token->getAttributes());
9798

9899
return $authenticatedToken;
@@ -106,6 +107,29 @@ public function supports(TokenInterface $token)
106107
return $token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey();
107108
}
108109

110+
/**
111+
* Retrieves roles from user and appends SwitchUserRole if original token contained one.
112+
*
113+
* @param UserInterface $user The user
114+
* @param TokenInterface $token The token
115+
*
116+
* @return Role[] The user roles
117+
*/
118+
private function getRoles(UserInterface $user, TokenInterface $token)
119+
{
120+
$roles = $user->getRoles();
121+
122+
foreach ($token->getRoles() as $role) {
123+
if ($role instanceof SwitchUserRole) {
124+
$roles[] = $role;
125+
126+
break;
127+
}
128+
}
129+
130+
return $roles;
131+
}
132+
109133
/**
110134
* Retrieves the user from an implementation-specific location.
111135
*

0 commit comments

Comments
 (0)