| @@ 12-67 (lines=56) @@ | ||
| 9 | use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
|
| 10 | use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
|
| 11 | ||
| 12 | class ObjectUserEmailProvider implements UserProviderInterface |
|
| 13 | { |
|
| 14 | /** @var BaseUserRepositoryCollection */ |
|
| 15 | private $userRepositoryCollection; |
|
| 16 | ||
| 17 | public function __construct(BaseUserRepositoryCollection $userRepositoryCollection) |
|
| 18 | { |
|
| 19 | $this->userRepositoryCollection = $userRepositoryCollection; |
|
| 20 | } |
|
| 21 | ||
| 22 | /** |
|
| 23 | * @param string $emailAddress |
|
| 24 | * |
|
| 25 | * @return User |
|
| 26 | * |
|
| 27 | * @throws UsernameNotFoundException |
|
| 28 | */ |
|
| 29 | public function loadUserByUsername($emailAddress): User |
|
| 30 | { |
|
| 31 | foreach ($this->userRepositoryCollection->all() as $repository) { |
|
| 32 | $user = $repository->findByEmailAddress($emailAddress); |
|
| 33 | ||
| 34 | if ($user instanceof User) { |
|
| 35 | return $user; |
|
| 36 | } |
|
| 37 | } |
|
| 38 | ||
| 39 | // Since we are using the email as username we keep this exception since it is a part of symfony |
|
| 40 | throw new UsernameNotFoundException( |
|
| 41 | sprintf('Email "%s" does not exist.', $emailAddress) |
|
| 42 | ); |
|
| 43 | } |
|
| 44 | ||
| 45 | /** |
|
| 46 | * @param UserInterface $user |
|
| 47 | * |
|
| 48 | * @return User |
|
| 49 | * |
|
| 50 | * @throws UnsupportedUserException |
|
| 51 | */ |
|
| 52 | public function refreshUser(UserInterface $user): User |
|
| 53 | { |
|
| 54 | if (!$this->supportsClass(get_class($user))) { |
|
| 55 | throw new UnsupportedUserException( |
|
| 56 | sprintf('Instances of "%s" are not supported.', get_class($user)) |
|
| 57 | ); |
|
| 58 | } |
|
| 59 | ||
| 60 | return $this->loadUserByUsername($user->getEmail()); |
|
| 61 | } |
|
| 62 | ||
| 63 | public function supportsClass($class): bool |
|
| 64 | { |
|
| 65 | return $this->userRepositoryCollection->supportsClass($class); |
|
| 66 | } |
|
| 67 | } |
|
| 68 | ||
| @@ 12-66 (lines=55) @@ | ||
| 9 | use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
|
| 10 | use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
|
| 11 | ||
| 12 | class ObjectUserProvider implements UserProviderInterface |
|
| 13 | { |
|
| 14 | /** @var BaseUserRepositoryCollection */ |
|
| 15 | private $userRepositoryCollection; |
|
| 16 | ||
| 17 | public function __construct(BaseUserRepositoryCollection $userRepositoryCollection) |
|
| 18 | { |
|
| 19 | $this->userRepositoryCollection = $userRepositoryCollection; |
|
| 20 | } |
|
| 21 | ||
| 22 | /** |
|
| 23 | * @param string $username |
|
| 24 | * |
|
| 25 | * @return User |
|
| 26 | * |
|
| 27 | * @throws UsernameNotFoundException |
|
| 28 | */ |
|
| 29 | public function loadUserByUsername($username): User |
|
| 30 | { |
|
| 31 | foreach ($this->userRepositoryCollection->all() as $repository) { |
|
| 32 | $user = $repository->findByUsername($username); |
|
| 33 | ||
| 34 | if ($user instanceof User) { |
|
| 35 | return $user; |
|
| 36 | } |
|
| 37 | } |
|
| 38 | ||
| 39 | throw new UsernameNotFoundException( |
|
| 40 | sprintf('Username "%s" does not exist.', $username) |
|
| 41 | ); |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * @param UserInterface $user |
|
| 46 | * |
|
| 47 | * @return User |
|
| 48 | * |
|
| 49 | * @throws UnsupportedUserException |
|
| 50 | */ |
|
| 51 | public function refreshUser(UserInterface $user): User |
|
| 52 | { |
|
| 53 | if (!$this->supportsClass(get_class($user))) { |
|
| 54 | throw new UnsupportedUserException( |
|
| 55 | sprintf('Instances of "%s" are not supported.', get_class($user)) |
|
| 56 | ); |
|
| 57 | } |
|
| 58 | ||
| 59 | return $this->loadUserByUsername($user->getUsername()); |
|
| 60 | } |
|
| 61 | ||
| 62 | public function supportsClass($class): bool |
|
| 63 | { |
|
| 64 | return $this->userRepositoryCollection->supportsClass($class); |
|
| 65 | } |
|
| 66 | } |
|
| 67 | ||