<?php
namespace App\Repository;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @method User|null find($id, $lockMode = null, $lockVersion = null)
* @method User|null findOneBy(array $criteria, array $orderBy = null)
* @method User[] findAll()
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
{
private $role = [];
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function add(User $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function remove(User $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
}
$user->setPassword($newHashedPassword);
$this->_em->persist($user);
$this->_em->flush();
}
/**
* @param string $role
*
* @return array
*/
public function findByRole($role)
{
$qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from($this->_entityName, 'u')
->where('u.roles LIKE :roles')
->setParameter('roles', '%"'.$role.'"%')
->orderBy('u.id','DESC');
return $qb->getQuery()->getResult();
}
public function findByRoles()
{
$qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from($this->_entityName, 'u')
->andWhere('u.roles LIKE :role or u.roles LIKE :role2 or u.roles LIKE :role3 or u.roles LIKE :role4')
->setParameter('role', '%ROLE_SUPER_ADMIN%')
->setParameter('role2', '%ROLE_EDITOR%')
->setParameter('role3', '%ROLE_COM_MANAGER%')
->setParameter('role4', '%ROLE_MODERATOR%');
return $qb->getQuery()->getResult();
}
public function findByRoleEnabled($role, $enable)
{
$qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from($this->_entityName, 'u')
->where('u.roles LIKE :roles')
->andWhere('u.isVerified =:enable')
->setParameter('roles', '%"'.$role.'"%')
->setParameter('enable', $enable);
return $qb->getQuery()->getResult();
}
}