entityManager = $entityManager; $this->repository = $entityManager->getRepository(User::class); } public function find($id, $lockMode = null, $lockVersion = null): ?User { return $this->repository->find($id, $lockMode, $lockVersion); } public function findOneBy(array $criteria, array $orderBy = null): ?User { return $this->repository->findOneBy($criteria, $orderBy); } /** * @return User[] */ public function findAll(): array { return $this->repository->findAll(); } /** * @return User[] */ public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null): array { return $this->repository->findBy($criteria, $orderBy, $limit, $offset); } public function countBy(array $criteria): int { return $this->repository->count($criteria); } public function countByActive(): int { return $this->countBy(['enabled' => true]); } /** * @return User[]|array */ public function findByActive(array $orderBy = null, int $limit = null, int $offset = null): array { return $this->findBy(['enabled' => true], $orderBy, $limit, $offset); } public function getClassName() { return User::class; } public function countByUsernameOrEmail(string $pattern): int { $qb = $this->queryByUsernameOrEmail($pattern); $qb->select('COUNT(u)'); return (int) $qb->getQuery()->getSingleScalarResult(); } public function findByUsernameOrEmail(string $pattern) { $qb = $this->queryByUsernameOrEmail($pattern); return $qb->getQuery()->getResult(); } public function findOneByUsernameOrEmail(string $pattern) { $qb = $this->queryByUsernameOrEmail($pattern); return $qb->getQuery()->getSingleResult(); } /** * Get the users having a specific flags * * If provided, only the users amongst "filtered users" are searched. This * allows to make a first search amongst users based on role and center * and, then filter those users having some flags. * * @param \Chill\MainBundle\Entity\User[] $amongstUsers */ public function findUsersHavingFlags($flag, array $amongstUsers = []): array { $gcs = $this ->entityManager ->createQuery( "SELECT DISTINCT gc " . "FROM ".GroupCenter::class." gc " . "JOIN gc.permissionsGroup pg " . "WHERE " . "JSONB_EXISTS_IN_ARRAY(pg.flags, :flag) = :true " ) ->setParameters([ 'true' => true, 'flag' => $flag ]) ->getResult(); if (count($gcs) === 0) { return []; } $qb = $this->entityManager->createQueryBuilder(); $qb ->select('DISTINCT u') ->from(User::class, 'u') ->where("u.enabled = 'TRUE'"); $orx = $qb->expr()->orX(); foreach($gcs as $i => $gc) { $orx->add(':gc_' . $i . ' MEMBER OF u.groupCenters'); $qb->setParameter('gc_' . $i, $gc); } $qb->andWhere($orx); if ($amongstUsers !== []) { $qb ->andWhere($qb->expr()->in('u', ':amongstUsers')) ->setParameter('amongstUsers', $amongstUsers); } return $qb->getQuery()->getResult(); } protected function queryByUsernameOrEmail(string $pattern): QueryBuilder { $qb = $this->entityManager->createQueryBuilder('u'); $searchByPattern = $qb->expr()->orX(); $searchByPattern ->add($qb->expr()->eq('u.usernameCanonical', 'LOWER(UNACCENT(:pattern))')) ->add($qb->expr()->eq('u.emailCanonical', 'LOWER(UNACCENT(:pattern))')); $qb ->where($searchByPattern) ->setParameter('pattern', $pattern); return $qb; } }