src/Repository/UserRepository.php line 24

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\PasswordUpgraderInterface;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Doctrine\ORM\Mapping as ORM;
  10. /**
  11.  * @method User|null find($id, $lockMode = null, $lockVersion = null)
  12.  * @method User|null findOneBy(array $criteria, array $orderBy = null)
  13.  * @method User[]    findAll()
  14.  * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  15.  */
  16. class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
  17. {
  18.     private $role = [];
  19.     public function __construct(ManagerRegistry $registry)
  20.     {
  21.         parent::__construct($registry, User::class);
  22.     }
  23.      /**
  24.      * @throws ORMException
  25.      * @throws OptimisticLockException
  26.      */
  27.     public function add(User $entity, bool $flush = true): void
  28.     {
  29.         $this->_em->persist($entity);
  30.         if ($flush) {
  31.             $this->_em->flush();
  32.         }
  33.     }
  34.     /**
  35.      * @throws ORMException
  36.      * @throws OptimisticLockException
  37.      */
  38.     public function remove(User $entity, bool $flush = true): void
  39.     {
  40.         $this->_em->remove($entity);
  41.         if ($flush) {
  42.             $this->_em->flush();
  43.         }
  44.     }
  45.     /**
  46.      * Used to upgrade (rehash) the user's password automatically over time.
  47.      */
  48.     public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
  49.     {
  50.         if (!$user instanceof User) {
  51.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
  52.         }
  53.         $user->setPassword($newHashedPassword);
  54.         $this->_em->persist($user);
  55.         $this->_em->flush();
  56.     }
  57.     /**
  58.      * @param string $role
  59.      * 
  60.      * @return array
  61.      */
  62.     public function findByRole($role)
  63.     {
  64.         $qb = $this->_em->createQueryBuilder();
  65.         $qb->select('u')
  66.             ->from($this->_entityName, 'u')
  67.             ->where('u.roles LIKE :roles')
  68.             ->setParameter('roles', '%"'.$role.'"%')
  69.             ->orderBy('u.id','DESC');
  70.         return $qb->getQuery()->getResult();
  71.     }
  72.     public function findByRoles()
  73.     {
  74.         $qb = $this->_em->createQueryBuilder();
  75.         $qb->select('u')
  76.             ->from($this->_entityName, 'u')
  77.             ->andWhere('u.roles LIKE :role or u.roles LIKE :role2 or u.roles LIKE :role3 or u.roles LIKE :role4')
  78.             ->setParameter('role', '%ROLE_SUPER_ADMIN%')
  79.             ->setParameter('role2', '%ROLE_EDITOR%')
  80.             ->setParameter('role3', '%ROLE_COM_MANAGER%')
  81.             ->setParameter('role4', '%ROLE_MODERATOR%');
  82.         return $qb->getQuery()->getResult();
  83.     }
  84.     public function findByRoleEnabled($role, $enable)
  85.     {
  86.         $qb = $this->_em->createQueryBuilder();
  87.         $qb->select('u')
  88.             ->from($this->_entityName, 'u')
  89.             ->where('u.roles LIKE :roles')
  90.             ->andWhere('u.isVerified =:enable')
  91.             ->setParameter('roles', '%"'.$role.'"%')
  92.             ->setParameter('enable', $enable);
  93.         return $qb->getQuery()->getResult(); 
  94.     }
  95.     
  96. }