Skip to content

Commit bf2b36f

Browse files
committed
[Security] Deprecate getUsername() in favor of getUserIdentifier()
1 parent f052d11 commit bf2b36f

28 files changed

+111
-38
lines changed

src/Symfony/Bridge/Monolog/Processor/AbstractTokenProcessor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ public function __invoke(array $record): array
4242

4343
if (null !== $token = $this->getToken()) {
4444
$record['extra'][$this->getKey()] = [
45-
'username' => $token->getUsername(),
4645
'authenticated' => $token->isAuthenticated(),
4746
'roles' => $token->getRoleNames(),
4847
];
48+
49+
if (method_exists($token, 'getUserIdentifier')) {
50+
$record['extra'][$this->getKey()]['username'] = $record['extra'][$this->getKey()]['user_identifier'] = $token->getUserIdentifier();
51+
} else {
52+
$record['extra'][$this->getKey()]['username'] = $token->getUsername();
53+
}
4954
}
5055

5156
return $record;

src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public function collect(Request $request, Response $response, \Throwable $except
9797

9898
$impersonatorUser = null;
9999
if ($token instanceof SwitchUserToken) {
100-
$impersonatorUser = $token->getOriginalToken()->getUsername();
100+
$originalToken = $token->getOriginalToken();
101+
$impersonatorUser = method_exists($originalToken, 'getUserIdentifier') ? $originalToken->getUserIdentifier() : $originalToken->getUsername();
101102
}
102103

103104
if (null !== $this->roleHierarchy) {
@@ -126,7 +127,7 @@ public function collect(Request $request, Response $response, \Throwable $except
126127
'token' => $token,
127128
'token_class' => $this->hasVarDumper ? new ClassStub(\get_class($token)) : \get_class($token),
128129
'logout_url' => $logoutUrl,
129-
'user' => $token->getUsername(),
130+
'user' => method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername(),
130131
'roles' => $assignedRoles,
131132
'inherited_roles' => array_unique($inheritedRoles),
132133
'supports_role_hierarchy' => null !== $this->roleHierarchy,

src/Symfony/Component/Ldap/Security/CheckLdapCredentialsListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function onCheckPassport(CheckPassportEvent $event)
8383
} else {
8484
throw new LogicException('Using the "query_string" config without using a "search_dn" and a "search_password" is not supported.');
8585
}
86-
$username = $ldap->escape($user->getUsername(), '', LdapInterface::ESCAPE_FILTER);
86+
$username = $ldap->escape(method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(), '', LdapInterface::ESCAPE_FILTER);
8787
$query = str_replace('{username}', $username, $ldapBadge->getQueryString());
8888
$result = $ldap->query($ldapBadge->getDnString(), $query)->execute();
8989
if (1 !== $result->count()) {
@@ -92,7 +92,7 @@ public function onCheckPassport(CheckPassportEvent $event)
9292

9393
$dn = $result[0]->getDn();
9494
} else {
95-
$username = $ldap->escape($user->getUsername(), '', LdapInterface::ESCAPE_DN);
95+
$username = $ldap->escape(method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(), '', LdapInterface::ESCAPE_DN);
9696
$dn = str_replace('{username}', $username, $ldapBadge->getDnString());
9797
}
9898

src/Symfony/Component/Ldap/Security/LdapUser.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public function getSalt(): ?string
7575
* {@inheritdoc}
7676
*/
7777
public function getUsername(): string
78+
{
79+
trigger_deprecation('symfony/security-core', '5.3', 'Method "%s" is deprecated and will be removed in 6.0, use getUserIdentifier() instead.', __METHOD__);
80+
81+
return $this->username;
82+
}
83+
84+
public function getUserIdentifier(): string
7885
{
7986
return $this->username;
8087
}

src/Symfony/Component/Ldap/Security/LdapUserProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function refreshUser(UserInterface $user)
117117
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_debug_type($user)));
118118
}
119119

120-
return new LdapUser($user->getEntry(), $user->getUsername(), $user->getPassword(), $user->getRoles(), $user->getExtraFields());
120+
return new LdapUser($user->getEntry(), method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(), $user->getPassword(), $user->getRoles(), $user->getExtraFields());
121121
}
122122

123123
/**

src/Symfony/Component/Security/Core/Authentication/AuthenticationProviderManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ public function authenticate(TokenInterface $token)
105105
$this->eventDispatcher->dispatch(new AuthenticationSuccessEvent($result), AuthenticationEvents::AUTHENTICATION_SUCCESS);
106106
}
107107

108+
if ($user = $result->getUser() instanceof UserInterface && !method_exists($result->getUser(), 'getUserIdentifier')) {
109+
trigger_deprecation('symfony/security-core', '5.3', 'Not implementing method "getUserIdentifier(): string" in user class "%s" is deprecated. This method will replace "getUsername()" in Symfony 6.0.', \get_class($result->getUser()));
110+
}
111+
108112
return $result;
109113
}
110114

src/Symfony/Component/Security/Core/Authentication/Provider/LdapBindAuthenticationProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected function retrieveUser(string $username, UsernamePasswordToken $token)
7474
*/
7575
protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token)
7676
{
77-
$username = $token->getUsername();
77+
$username = method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername();
7878
$password = $token->getCredentials();
7979

8080
if ('' === (string) $password) {

src/Symfony/Component/Security/Core/Authentication/Provider/RememberMeAuthenticationProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function authenticate(TokenInterface $token)
5151

5252
$user = $token->getUser();
5353

54-
if (!$token->getUser() instanceof UserInterface) {
54+
if (!$user instanceof UserInterface) {
5555
throw new LogicException(sprintf('Method "%s::getUser()" must return a "%s" instance, "%s" returned.', get_debug_type($token), UserInterface::class, get_debug_type($user)));
5656
}
5757

src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function authenticate(TokenInterface $token)
5555
throw new AuthenticationException('The token is not supported by this authentication provider.');
5656
}
5757

58-
$username = $token->getUsername();
58+
$username = method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername();
5959
if ('' === $username || null === $username) {
6060
$username = AuthenticationProviderInterface::USERNAME_NONE_PROVIDED;
6161
}

src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,36 @@ public function getRoleNames(): array
5151
/**
5252
* {@inheritdoc}
5353
*/
54-
public function getUsername()
54+
public function getUsername(/* $legacy = true */)
5555
{
56+
if (1 === func_num_args() && false === func_get_arg(0)) {
57+
return null;
58+
}
59+
60+
trigger_deprecation('symfony/security-core', '5.3', 'Method "%s" is deprecated and will be removed in 6.0, use getUserIdentifier() instead.', __METHOD__);
61+
5662
if ($this->user instanceof UserInterface) {
5763
return $this->user->getUsername();
5864
}
5965

6066
return (string) $this->user;
6167
}
6268

69+
public function getUserIdentifier(): string
70+
{
71+
// method returns "null" in non-legacy mode if not overriden
72+
$username = $this->getUsername(false);
73+
if (null !== $username) {
74+
trigger_deprecation('symfony/security-core', '5.3', 'Method "%s::getUsername()" is deprecated and will be removed in 6.0, override "getUserIdentifier()" instead.', \get_class($this));
75+
}
76+
77+
if ($this->user instanceof UserInterface) {
78+
return method_exists($this->user, 'getUserIdentifier') ? $this->user->getUserIdentifier() : $this->user->getUsername();
79+
}
80+
81+
return (string) $this->user;
82+
}
83+
6384
/**
6485
* {@inheritdoc}
6586
*/
@@ -234,7 +255,7 @@ public function __toString()
234255
$roles[] = $role;
235256
}
236257

237-
return sprintf('%s(user="%s", authenticated=%s, roles="%s")', $class, $this->getUsername(), json_encode($this->authenticated), implode(', ', $roles));
258+
return sprintf('%s(user="%s", authenticated=%s, roles="%s")', $class, $this->getUserIdentifier(), json_encode($this->authenticated), implode(', ', $roles));
238259
}
239260

240261
/**
@@ -283,7 +304,11 @@ private function hasUserChanged(UserInterface $user): bool
283304
return true;
284305
}
285306

286-
if ($this->user->getUsername() !== $user->getUsername()) {
307+
// @deprecated since Symfony 5.3, drop getUsername() in 6.0
308+
$userIdentifier = function ($user) {
309+
return method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();
310+
};
311+
if ($userIdentifier($this->user) !== $userIdentifier($user)) {
287312
return true;
288313
}
289314

0 commit comments

Comments
 (0)