<?php
namespace Platform\SecurityBundle\Service;
use Cms\CoreBundle\Util\Doctrine\EntityManager;
use Platform\SecurityBundle\Doctrine\Identity\AccountRepository;
use Platform\SecurityBundle\Entity\Identity\Account;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
// TODO: is this class needed?
/**
* Responsible for loading up user Account objects.
*
* Class AccountProvider
* @package Platform\SecurityBundle\Service
*/
final class AccountProvider implements UserProviderInterface
{
/**
* @var EntityManager
*/
protected EntityManager $em;
/**
* @param EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* @param string $identifier
* @return UserInterface
*/
public function loadUserByIdentifier(string $identifier): UserInterface
{
// check for null or empty username
if (empty($identifier)) {
throw new UserNotFoundException();
}
/** @var AccountRepository $accountRepository */
$accountRepository = $this->em->getRepository(Account::class);
$account = $accountRepository->find($identifier);
// check for null
if ($account === null) {
throw new UserNotFoundException();
}
// done
return $account;
}
/**
* "Usernames" in the system are actually the numerical PKID of an Account.
*
* {@inheritdoc}
* @return Account
*/
public function loadUserByUsername($username): UserInterface
{
// TODO: remove loadUserByUsername once we are on symfony 6
return $this->loadUserByIdentifier($username);
}
/**
* {@inheritdoc}
*/
public function refreshUser(UserInterface $user): UserInterface
{
if ($this->supportsClass($user) === false) {
throw new UnsupportedUserException();
}
return $this->loadUserByIdentifier($user->getUserIdentifier());
}
/**
* {@inheritdoc}
*/
public function supportsClass($class): bool
{
return ($class === Account::class || is_a($class, Account::class) || is_subclass_of($class, Account::class));
}
}