src/Repository/UserRepository.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\User;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  7. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  8. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  9. /**
  10.  * @extends ServiceEntityRepository<User>
  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 PasswordUpgraderInterface
  18. {
  19.     public function __construct(ManagerRegistry $registry)
  20.     {
  21.         parent::__construct($registryUser::class);
  22.     }
  23.     public function add(User $entitybool $flush false): void
  24.     {
  25.         $this->getEntityManager()->persist($entity);
  26.         if ($flush) {
  27.             $this->getEntityManager()->flush();
  28.         }
  29.     }
  30.     public function remove(User $entitybool $flush false): void
  31.     {
  32.         $this->getEntityManager()->remove($entity);
  33.         if ($flush) {
  34.             $this->getEntityManager()->flush();
  35.         }
  36.     }
  37.     public function loadUserByIdentifier(string $username): ?User
  38.     {
  39.         $entityManager $this->getEntityManager();
  40.         return $entityManager->createQuery(
  41.                 'SELECT u
  42.                 FROM App\Entity\User u
  43.                 WHERE u.username = :query
  44.                 and u.locked = 0'
  45.             )
  46.             ->setParameter('query'$username)
  47.             ->getOneOrNullResult();
  48.     }
  49.     /** @deprecated since Symfony 5.3 */
  50.     public function loadUserByUsername(string $username): ?User
  51.     {
  52.         return $this->loadUserByIdentifier($username);
  53.     }
  54.     /**
  55.      * Used to upgrade (rehash) the user's password automatically over time.
  56.      */
  57.     public function upgradePassword(PasswordAuthenticatedUserInterface $userstring $newHashedPassword): void
  58.     {
  59.         if (!$user instanceof User) {
  60.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
  61.         }
  62.         $user->setPassword($newHashedPassword);
  63.         $this->add($usertrue);
  64.     }
  65.     public function findByEmail(string $email)
  66.     {
  67.        return $this->createQueryBuilder('u')
  68.            
  69.             ->where('u.email = :email')
  70.             ->setParameter('email',$email)
  71.             ->getQuery()
  72.             ->getOneOrNullResult()
  73.         ;
  74.    }
  75.    public function findByUsername(string $username)
  76.    {
  77.       return $this->createQueryBuilder('u')
  78.           
  79.            ->where('u.username = :username')
  80.            ->setParameter('username',$username)
  81.            ->getQuery()
  82.            ->getOneOrNullResult()
  83.        ;
  84.   }
  85. //    /**
  86. //     * @return User[] Returns an array of User objects
  87. //     */
  88. //    public function findByExampleField($value): array
  89. //    {
  90. //        return $this->createQueryBuilder('u')
  91. //            ->andWhere('u.exampleField = :val')
  92. //            ->setParameter('val', $value)
  93. //            ->orderBy('u.id', 'ASC')
  94. //            ->setMaxResults(10)
  95. //            ->getQuery()
  96. //            ->getResult()
  97. //        ;
  98. //    }
  99. //    public function findOneBySomeField($value): ?User
  100. //    {
  101. //        return $this->createQueryBuilder('u')
  102. //            ->andWhere('u.exampleField = :val')
  103. //            ->setParameter('val', $value)
  104. //            ->getQuery()
  105. //            ->getOneOrNullResult()
  106. //        ;
  107. //    }
  108. }