app/Service/Repository/ORM/UserRepository.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Sq\Service\Repository\ORM;
  3. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  4. use Doctrine\ORM\Query\ResultSetMappingBuilder;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Sq\Entity\Schema\ORM\User;
  7. use Sq\Service\Config\SqConfig;
  8. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Symfony\Component\Security\Core\User\UserProviderInterface;
  11. /**
  12.  * @method User|null find($id, $lockMode = null, $lockVersion = null)
  13.  * @method User|null findOneBy(array $criteria, array $orderBy = null)
  14.  * @method User[]    findAll()
  15.  * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  16.  */
  17. class UserRepository extends ServiceEntityRepository implements UserProviderInterface
  18. {
  19.     private string $encryptionKey '';
  20.     public function __construct(ManagerRegistry $registrySqConfig $config)
  21.     {
  22.         parent::__construct($registryUser::class);
  23.         $key $config->getConfig()['crypto_key'] ?? '';
  24.         $this->encryptionKey is_string($key) ? $key '';
  25.     }
  26.     /**
  27.      * This complicated query is necessary to ignore case differences.
  28.      */
  29.     public function findOneByEmail(string $email): ?User
  30.     {
  31.         $rsm = new ResultSetMappingBuilder($this->getEntityManager());
  32.         $rsm->addRootEntityFromClassMetadata(User::class, 'u');
  33.         $query $this->getEntityManager()->createNativeQuery(sprintf(
  34.             'SELECT %s FROM `users` AS `u` WHERE LOWER(CONVERT(AES_DECRYPT(`u`.`u_email`, :key) USING \'utf8\')) = :email LIMIT 1;',
  35.             $rsm->generateSelectClause(['u' => 'u'])
  36.         ), $rsm);
  37.         $query->setParameters(['email' => strtolower($email), 'key' => $this->encryptionKey]);
  38.         return $query->getOneOrNullResult();
  39.     }
  40.     public function supportsClass(string $class): bool
  41.     {
  42.         return $class === User::class;
  43.     }
  44.     public function refreshUser(UserInterface $user): UserInterface
  45.     {
  46.         $this->getEntityManager()->refresh($user);
  47.         return $user;
  48.     }
  49.     /** @deprecated (delete when upgrading to Symfony 6+) */
  50.     public function loadUserByUsername(string $username): UserInterface
  51.     {
  52.         return $this->loadUserByIdentifier($username);
  53.     }
  54.     public function loadUserByIdentifier(string $identifier): UserInterface
  55.     {
  56.         if (null === $user $this->findOneBy(['id' => $identifier]))
  57.         {
  58.             throw new UserNotFoundException;
  59.         }
  60.         // make sure the user is still in overhaul
  61.         if ($user->isRolledBackToLegacy())
  62.         {
  63.             throw new UserNotFoundException;
  64.         }
  65.         return $user;
  66.     }
  67. }