Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit def171d

Browse files
committed
minor #15279 [Security] Removed some deprecated features related to User::getUsername() (javiereguiluz)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Security] Removed some deprecated features related to User::getUsername() Fixes #15164. The only occurrence of `getUsername()` left in the docs is in this file: https://github.com/symfony/symfony-docs/blob/5.x/components/messenger.rst Please help me to know how should I proceed to remove it. Thanks! Commits ------- ce457e0 [Security] Removed some deprecated features related to User::getUsername()
2 parents 4d500e9 + ce457e0 commit def171d

File tree

9 files changed

+37
-32
lines changed

9 files changed

+37
-32
lines changed

controller/argument_value_resolver.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ with the ``User`` class::
7575
{
7676
public function index(User $user)
7777
{
78-
return new Response('Hello '.$user->getUsername().'!');
78+
return new Response('Hello '.$user->getUserIdentifier().'!');
7979
}
8080
}
8181

reference/configuration/security.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ If ``true``, when a user is not found a generic exception of type
8989
is thrown with the message "Bad credentials".
9090

9191
If ``false``, the exception thrown is of type
92-
:class:`Symfony\\Component\\Security\\Core\\Exception\\UsernameNotFoundException`
93-
and it includes the given not found username.
92+
:class:`Symfony\\Component\\Security\\Core\\Exception\\UserNotFoundException`
93+
and it includes the given not found user identifier.
9494

9595
session_fixation_strategy
9696
~~~~~~~~~~~~~~~~~~~~~~~~~

routing.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,7 +2402,7 @@ use the ``generateUrl()`` helper::
24022402

24032403
// generate a URL with route arguments
24042404
$userProfilePage = $this->generateUrl('user_profile', [
2405-
'username' => $user->getUsername(),
2405+
'username' => $user->getUserIdentifier(),
24062406
]);
24072407

24082408
// generated URLs are "absolute paths" by default. Pass a third optional
@@ -2472,7 +2472,7 @@ the :class:`Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface` class
24722472

24732473
// generate a URL with route arguments
24742474
$userProfilePage = $this->router->generate('user_profile', [
2475-
'username' => $user->getUsername(),
2475+
'username' => $user->getUserIdentifier(),
24762476
]);
24772477

24782478
// generated URLs are "absolute paths" by default. Pass a third optional
@@ -2595,7 +2595,7 @@ Now you'll get the expected results when generating URLs in your commands::
25952595

25962596
// generate a URL with route arguments
25972597
$userProfilePage = $this->router->generate('user_profile', [
2598-
'username' => $user->getUsername(),
2598+
'username' => $user->getUserIdentifier(),
25992599
]);
26002600

26012601
// generated URLs are "absolute paths" by default. Pass a third optional

security/custom_authentication_provider.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ the ``PasswordDigest`` header value matches with the user's password::
218218

219219
public function authenticate(TokenInterface $token): WsseUserToken
220220
{
221-
$user = $this->userProvider->loadUserByUsername($token->getUsername());
221+
// The loadUserByIdentifier() and getUserIdentifier() methods were
222+
// introduced in Symfony 5.3. In previous versions they were called
223+
// loadUserByUsername() and getUsername() respectively
224+
$user = $this->userProvider->loadUserByIdentifier($token->getUserIdentifier());
222225

223226
if ($user && $this->validateDigest($token->digest, $token->nonce, $token->created, $user->getPassword())) {
224227
$authenticatedToken = new WsseUserToken($user->getRoles());

security/experimental_authenticators.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ using :ref:`the user provider <security-user-providers>`::
456456
You can optionally pass a user loader as second argument to the
457457
``UserBadge``. This callable receives the ``$userIdentifier``
458458
and must return a ``UserInterface`` object (otherwise a
459-
``UsernameNotFoundException`` is thrown)::
459+
``UserNotFoundException`` is thrown)::
460460

461461
// src/Security/CustomAuthenticator.php
462462
namespace App\Security;

security/guard_authentication.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ This requires you to implement several methods::
172172
return null;
173173
}
174174

175-
// The "username" in this case is the apiToken, see the key `property`
175+
// The user identifier in this case is the apiToken, see the key `property`
176176
// of `your_db_provider` in `security.yaml`.
177177
// If this returns a user, checkCredentials() is called next:
178-
return $userProvider->loadUserByUsername($credentials);
178+
return $userProvider->loadUserByIdentifier($credentials);
179179
}
180180

181181
public function checkCredentials($credentials, UserInterface $user): bool

security/json_login_setup.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ The next step is to configure a route in your app matching this path:
8585
$user = $this->getUser();
8686
8787
return $this->json([
88-
'username' => $user->getUsername(),
88+
// The getUserIdentifier() method was introduced in Symfony 5.3.
89+
// In previous versions it was called getUsername()
90+
'username' => $user->getUserIdentifier(),
8991
'roles' => $user->getRoles(),
9092
]);
9193
}

security/login_link.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ The signed URL contains 3 parameters:
401401
The UNIX timestamp when the link expires.
402402

403403
``user``
404-
The value returned from ``$user->getUsername()`` for this user.
404+
The value returned from ``$user->getUserIdentifier()`` for this user.
405405

406406
``hash``
407407
A hash of ``expires``, ``user`` and any configured signature

security/user_provider.rst

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ the ``property`` config key. If you want a bit more control over this - e.g. you
124124
want to find a user by ``email`` *or* ``username``, you can do that by making
125125
your ``UserRepository`` implement the
126126
:class:`Symfony\\Bridge\\Doctrine\\Security\\User\\UserLoaderInterface`. This
127-
interface only requires one method: ``loadUserByUsername($username)``::
127+
interface only requires one method: ``loadUserByIdentifier($identifier)``::
128128

129129
// src/Repository/UserRepository.php
130130
namespace App\Repository;
@@ -137,7 +137,9 @@ interface only requires one method: ``loadUserByUsername($username)``::
137137
{
138138
// ...
139139

140-
public function loadUserByUsername(string $usernameOrEmail): ?User
140+
// The loadUserByIdentifier() method was introduced in Symfony 5.3.
141+
// In previous versions it was called loadUserByUsername()
142+
public function loadUserByIdentifier(string $usernameOrEmail): ?User
141143
{
142144
$entityManager = $this->getEntityManager();
143145

@@ -209,7 +211,7 @@ To finish this, remove the ``property`` key from the user provider in
209211
This tells Symfony to *not* query automatically for the User. Instead, when
210212
needed (e.g. because :doc:`user impersonation </security/impersonating_user>`,
211213
:doc:`Remember Me </security/remember_me>`, or some other security feature is
212-
activated), the ``loadUserByUsername()`` method on ``UserRepository`` will be called.
214+
activated), the ``loadUserByIdentifier()`` method on ``UserRepository`` will be called.
213215

214216
.. _security-memory-user-provider:
215217

@@ -367,31 +369,29 @@ command will generate a nice skeleton to get you started::
367369
namespace App\Security;
368370

369371
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
370-
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
372+
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
371373
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
372374
use Symfony\Component\Security\Core\User\UserInterface;
373375
use Symfony\Component\Security\Core\User\UserProviderInterface;
374376

375377
class UserProvider implements UserProviderInterface, PasswordUpgraderInterface
376378
{
377379
/**
378-
* Symfony calls this method if you use features like switch_user
379-
* or remember_me.
380-
*
381-
* If you're not using these features, you do not need to implement
382-
* this method.
380+
* The loadUserByIdentifier() method was introduced in Symfony 5.3.
381+
* In previous versions it was called loadUserByUsername()
383382
*
384-
* @return UserInterface
383+
* Symfony calls this method if you use features like switch_user
384+
* or remember_me. If you're not using these features, you do not
385+
* need to implement this method.
385386
*
386-
* @throws UsernameNotFoundException if the user is not found
387+
* @throws UserNotFoundException if the user is not found
387388
*/
388-
public function loadUserByUsername(string $username)
389+
public function loadUserByIdentifier(string $identifier): UserInterface
389390
{
390-
// Load a User object from your data source or throw UsernameNotFoundException.
391-
// The $username argument may not actually be a username:
392-
// it is whatever value is being returned by the getUsername()
393-
// method in your User class.
394-
throw new \Exception('TODO: fill in loadUserByUsername() inside '.__FILE__);
391+
// Load a User object from your data source or throw UserNotFoundException.
392+
// The $identifier argument is whatever value is being returned by the
393+
// getUserIdentifier() method in your User class.
394+
throw new \Exception('TODO: fill in loadUserByIdentifier() inside '.__FILE__);
395395
}
396396

397397
/**
@@ -414,7 +414,7 @@ command will generate a nice skeleton to get you started::
414414
}
415415

416416
// Return a User object after making sure its data is "fresh".
417-
// Or throw a UsernameNotFoundException if the user no longer exists.
417+
// Or throw a UserNotFoundException if the user no longer exists.
418418
throw new \Exception('TODO: fill in refreshUser() inside '.__FILE__);
419419
}
420420

@@ -467,8 +467,8 @@ request, it's deserialized and then passed to your user provider to "refresh" it
467467
Then, the two User objects (the original from the session and the refreshed User
468468
object) are "compared" to see if they are "equal". By default, the core
469469
``AbstractToken`` class compares the return values of the ``getPassword()``,
470-
``getSalt()`` and ``getUsername()`` methods. If any of these are different, your
471-
user will be logged out. This is a security measure to make sure that malicious
470+
``getSalt()`` and ``getUserIdentifier()`` methods. If any of these are different,
471+
your user will be logged out. This is a security measure to make sure that malicious
472472
users can be de-authenticated if core user data changes.
473473

474474
However, in some cases, this process can cause unexpected authentication problems.

0 commit comments

Comments
 (0)