em = $em; $this->authorizationHelper = $authorizationHelper; $this->centerResolverDispatcher = $centerResolverDispatcher; $this->security = $security; } public function buildQueryByPerson(Person $person): QueryBuilder { $qb = $this->em->getRepository(PersonDocument::class)->createQueryBuilder('d'); $qb ->where($qb->expr()->eq('d.person', ':person')) ->setParameter('person', $person); return $qb; } public function countByPerson(Person $person): int { $qb = $this->buildQueryByPerson($person)->select('COUNT(d)'); $this->addACL($qb, $person); return $qb->getQuery()->getSingleScalarResult(); } public function findByPerson(Person $person, array $orderBy = [], int $limit = 20, int $offset = 0): array { $qb = $this->buildQueryByPerson($person)->select('d'); $this->addACL($qb, $person); foreach ($orderBy as $field => $order) { $qb->addOrderBy('d.' . $field, $order); } $qb->setFirstResult($offset)->setMaxResults($limit); return $qb->getQuery()->getResult(); } private function addACL(QueryBuilder $qb, Person $person): void { $center = $this->centerResolverDispatcher->resolveCenter($person); $reachableScopes = $this->authorizationHelper ->getReachableScopes( $this->security->getUser(), PersonDocumentVoter::SEE, $center ); $qb->andWhere($qb->expr()->in('d.scope', ':scopes')) ->setParameter('scopes', $reachableScopes); } }