@@ -124,7 +124,7 @@ the ``property`` config key. If you want a bit more control over this - e.g. you
124
124
want to find a user by ``email `` *or * ``username ``, you can do that by making
125
125
your ``UserRepository `` implement the
126
126
:class: `Symfony\\ Bridge\\ Doctrine\\ Security\\ User\\ UserLoaderInterface `. This
127
- interface only requires one method: ``loadUserByUsername($username ) ``::
127
+ interface only requires one method: ``loadUserByIdentifier($identifier ) ``::
128
128
129
129
// src/Repository/UserRepository.php
130
130
namespace App\Repository;
@@ -137,7 +137,9 @@ interface only requires one method: ``loadUserByUsername($username)``::
137
137
{
138
138
// ...
139
139
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
141
143
{
142
144
$entityManager = $this->getEntityManager();
143
145
@@ -209,7 +211,7 @@ To finish this, remove the ``property`` key from the user provider in
209
211
This tells Symfony to *not * query automatically for the User. Instead, when
210
212
needed (e.g. because :doc: `user impersonation </security/impersonating_user >`,
211
213
: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.
213
215
214
216
.. _security-memory-user-provider :
215
217
@@ -367,31 +369,29 @@ command will generate a nice skeleton to get you started::
367
369
namespace App\Security;
368
370
369
371
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
370
- use Symfony\Component\Security\Core\Exception\UsernameNotFoundException ;
372
+ use Symfony\Component\Security\Core\Exception\UserNotFoundException ;
371
373
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
372
374
use Symfony\Component\Security\Core\User\UserInterface;
373
375
use Symfony\Component\Security\Core\User\UserProviderInterface;
374
376
375
377
class UserProvider implements UserProviderInterface, PasswordUpgraderInterface
376
378
{
377
379
/**
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()
383
382
*
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.
385
386
*
386
- * @throws UsernameNotFoundException if the user is not found
387
+ * @throws UserNotFoundException if the user is not found
387
388
*/
388
- public function loadUserByUsername (string $username)
389
+ public function loadUserByIdentifier (string $identifier): UserInterface
389
390
{
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__);
395
395
}
396
396
397
397
/**
@@ -414,7 +414,7 @@ command will generate a nice skeleton to get you started::
414
414
}
415
415
416
416
// 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.
418
418
throw new \Exception('TODO: fill in refreshUser() inside '.__FILE__);
419
419
}
420
420
@@ -467,8 +467,8 @@ request, it's deserialized and then passed to your user provider to "refresh" it
467
467
Then, the two User objects (the original from the session and the refreshed User
468
468
object) are "compared" to see if they are "equal". By default, the core
469
469
``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
472
472
users can be de-authenticated if core user data changes.
473
473
474
474
However, in some cases, this process can cause unexpected authentication problems.
0 commit comments