src/Platform/SecurityBundle/Service/AccountProvider.php line 81

Open in your IDE?
  1. <?php
  2. namespace Platform\SecurityBundle\Service;
  3. use Cms\CoreBundle\Util\Doctrine\EntityManager;
  4. use Platform\SecurityBundle\Doctrine\Identity\AccountRepository;
  5. use Platform\SecurityBundle\Entity\Identity\Account;
  6. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  7. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use Symfony\Component\Security\Core\User\UserProviderInterface;
  10. // TODO: is this class needed?
  11. /**
  12.  * Responsible for loading up user Account objects.
  13.  *
  14.  * Class AccountProvider
  15.  * @package Platform\SecurityBundle\Service
  16.  */
  17. final class AccountProvider implements UserProviderInterface
  18. {
  19.     /**
  20.      * @var EntityManager
  21.      */
  22.     protected EntityManager $em;
  23.     /**
  24.      * @param EntityManager $em
  25.      */
  26.     public function __construct(EntityManager $em)
  27.     {
  28.         $this->em $em;
  29.     }
  30.     /**
  31.      * @param string $identifier
  32.      * @return UserInterface
  33.      */
  34.     public function loadUserByIdentifier(string $identifier): UserInterface
  35.     {
  36.         // check for null or empty username
  37.         if (empty($identifier)) {
  38.             throw new UserNotFoundException();
  39.         }
  40.         /** @var AccountRepository $accountRepository */
  41.         $accountRepository $this->em->getRepository(Account::class);
  42.         $account $accountRepository->find($identifier);
  43.         // check for null
  44.         if ($account === null) {
  45.             throw new UserNotFoundException();
  46.         }
  47.         // done
  48.         return $account;
  49.     }
  50.     /**
  51.      * "Usernames" in the system are actually the numerical PKID of an Account.
  52.      *
  53.      * {@inheritdoc}
  54.      * @return Account
  55.      */
  56.     public function loadUserByUsername($username): UserInterface
  57.     {
  58.         // TODO: remove loadUserByUsername once we are on symfony 6
  59.         return $this->loadUserByIdentifier($username);
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public function refreshUser(UserInterface $user): UserInterface
  65.     {
  66.         if ($this->supportsClass($user) === false) {
  67.             throw new UnsupportedUserException();
  68.         }
  69.         return $this->loadUserByIdentifier($user->getUserIdentifier());
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      */
  74.     public function supportsClass($class): bool
  75.     {
  76.         return ($class === Account::class || is_a($classAccount::class) || is_subclass_of($classAccount::class));
  77.     }
  78. }