Skip to content

Commit e9c8a3a

Browse files
committed
Implement the new API for the user provider
1 parent 84dedcb commit e9c8a3a

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Security/UserProvider.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use FOS\UserBundle\Model\UserManagerInterface;
1616
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
1717
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
18+
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
1819
use Symfony\Component\Security\Core\User\UserInterface as SecurityUserInterface;
1920
use Symfony\Component\Security\Core\User\UserProviderInterface;
2021

@@ -33,6 +34,17 @@ public function __construct(UserManagerInterface $userManager)
3334
$this->userManager = $userManager;
3435
}
3536

37+
public function loadUserByIdentifier(string $identifier): SecurityUserInterface
38+
{
39+
$user = $this->findUser($identifier);
40+
41+
if (!$user) {
42+
throw new UserNotFoundException(sprintf('Username "%s" does not exist.', $identifier));
43+
}
44+
45+
return $user;
46+
}
47+
3648
/**
3749
* {@inheritdoc}
3850
*/
@@ -41,6 +53,10 @@ public function loadUserByUsername($username)
4153
$user = $this->findUser($username);
4254

4355
if (!$user) {
56+
if (class_exists(UserNotFoundException::class)) {
57+
throw new UserNotFoundException(sprintf('Username "%s" does not exist.', $username));
58+
}
59+
4460
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
4561
}
4662

@@ -50,7 +66,7 @@ public function loadUserByUsername($username)
5066
/**
5167
* {@inheritdoc}
5268
*/
53-
public function refreshUser(SecurityUserInterface $user)
69+
public function refreshUser(SecurityUserInterface $user): SecurityUserInterface
5470
{
5571
if (!$user instanceof UserInterface) {
5672
throw new UnsupportedUserException(sprintf('Expected an instance of FOS\UserBundle\Model\UserInterface, but got "%s".', get_class($user)));
@@ -61,6 +77,10 @@ public function refreshUser(SecurityUserInterface $user)
6177
}
6278

6379
if (null === $reloadedUser = $this->userManager->findUserBy(['id' => $user->getId()])) {
80+
if (class_exists(UserNotFoundException::class)) {
81+
throw new UserNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $user->getId()));
82+
}
83+
6484
throw new UsernameNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $user->getId()));
6585
}
6686

@@ -70,7 +90,7 @@ public function refreshUser(SecurityUserInterface $user)
7090
/**
7191
* {@inheritdoc}
7292
*/
73-
public function supportsClass($class)
93+
public function supportsClass($class): bool
7494
{
7595
$userClass = $this->userManager->getClass();
7696

0 commit comments

Comments
 (0)