|
13 | 13 |
|
14 | 14 | use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
15 | 15 | use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
|
| 16 | +use Symfony\Component\Security\Core\Exception\UserNotFoundException; |
16 | 17 | use Symfony\Component\Security\Core\User\UserInterface;
|
17 | 18 | use Symfony\Component\Security\Http\EventListener\UserProviderListener;
|
18 | 19 |
|
@@ -60,20 +61,29 @@ public function getUserIdentifier(): string
|
60 | 61 | */
|
61 | 62 | public function getUser(): UserInterface
|
62 | 63 | {
|
63 |
| - if (!isset($this->user)) { |
64 |
| - if (null === $this->userLoader) { |
65 |
| - throw new \LogicException(sprintf('No user loader is configured, did you forget to register the "%s" listener?', UserProviderListener::class)); |
66 |
| - } |
| 64 | + if (null !== $this->user) { |
| 65 | + return $this->user; |
| 66 | + } |
| 67 | + |
| 68 | + if (null === $this->userLoader) { |
| 69 | + throw new \LogicException(sprintf('No user loader is configured, did you forget to register the "%s" listener?', UserProviderListener::class)); |
| 70 | + } |
67 | 71 |
|
68 |
| - $user = ($this->userLoader)($this->userIdentifier); |
69 |
| - if (!$user instanceof UserInterface) { |
70 |
| - throw new AuthenticationServiceException(sprintf('The user provider must return a UserInterface object, "%s" given.', get_debug_type($this->user))); |
71 |
| - } |
| 72 | + $user = ($this->userLoader)($this->userIdentifier); |
| 73 | + |
| 74 | + // No user has been found via the $this->userLoader callback |
| 75 | + if (null === $user) { |
| 76 | + $exception = new UserNotFoundException(); |
| 77 | + $exception->setUserIdentifier($this->userIdentifier); |
| 78 | + |
| 79 | + throw $exception; |
| 80 | + } |
72 | 81 |
|
73 |
| - $this->user = $user; |
| 82 | + if (!$user instanceof UserInterface) { |
| 83 | + throw new AuthenticationServiceException(sprintf('The user provider must return a UserInterface object, "%s" given.', get_debug_type($user))); |
74 | 84 | }
|
75 | 85 |
|
76 |
| - return $this->user; |
| 86 | + return $this->user = $user; |
77 | 87 | }
|
78 | 88 |
|
79 | 89 | public function getUserLoader(): ?callable
|
|
0 commit comments