accompanyingPeriodRepository = $accompanyingPeriodRepository; $this->security = $security; $this->authorizationHelper = $authorizationHelper; $this->centerResolver = $centerResolverDispatcher; } public function buildQueryOpenedAccompanyingCourseByUser(?User $user, array $postalCodes = []): QueryBuilder { $qb = $this->accompanyingPeriodRepository->createQueryBuilder('ap'); $qb->where($qb->expr()->eq('ap.user', ':user')) ->andWhere( $qb->expr()->neq('ap.step', ':draft'), $qb->expr()->orX( $qb->expr()->isNull('ap.closingDate'), $qb->expr()->gt('ap.closingDate', ':now') ) ) ->setParameter('user', $user) ->setParameter('now', new DateTime('now')) ->setParameter('draft', AccompanyingPeriod::STEP_DRAFT); if ([] !== $postalCodes) { $qb->join('ap.locationHistories', 'location_history') ->leftJoin(PersonHouseholdAddress::class, 'person_address', Join::WITH, 'IDENTITY(location_history.personLocation) = IDENTITY(person_address.person)') ->join( Address::class, 'address', Join::WITH, 'COALESCE(IDENTITY(location_history.addressLocation), IDENTITY(person_address.address)) = address.id' ) ->andWhere( $qb->expr()->orX( $qb->expr()->isNull('person_address'), $qb->expr()->andX( $qb->expr()->lte('person_address.validFrom', ':now'), $qb->expr()->orX( $qb->expr()->isNull('person_address.validTo'), $qb->expr()->lt('person_address.validTo', ':now') ) ) ) ) ->andWhere( $qb->expr()->isNull('location_history.endDate') ) ->andWhere( $qb->expr()->in('address.postcode', ':postal_codes') ) ->setParameter('now', new DateTimeImmutable('now'), Types::DATE_IMMUTABLE) ->setParameter('postal_codes', $postalCodes); } return $qb; } public function countByUnDispatched(array $jobs, array $services, array $administrativeLocations): int { $qb = $this->addACLByUnDispatched($this->buildQueryUnDispatched($jobs, $services, $administrativeLocations)); $qb->select('COUNT(ap)'); return $qb->getQuery()->getSingleScalarResult(); } public function countByUserAndPostalCodesOpenedAccompanyingPeriod(?User $user, array $postalCodes): int { if (null === $user) { return 0; } return $this->buildQueryOpenedAccompanyingCourseByUser($user, $postalCodes) ->select('COUNT(ap)') ->getQuery() ->getSingleScalarResult(); } public function countByUserOpenedAccompanyingPeriod(?User $user): int { if (null === $user) { return 0; } return $this->buildQueryOpenedAccompanyingCourseByUser($user) ->select('COUNT(ap)') ->getQuery() ->getSingleScalarResult(); } public function findByPerson( Person $person, string $role, ?array $orderBy = [], ?int $limit = null, ?int $offset = null ): array { $qb = $this->accompanyingPeriodRepository->createQueryBuilder('ap'); $scopes = $this->authorizationHelper ->getReachableScopes( $role, $this->centerResolver->resolveCenters($person) ); $scopesCanSeeConfidential = $this->authorizationHelper ->getReachableScopes( AccompanyingPeriodVoter::SEE_CONFIDENTIAL_ALL, $this->centerResolver->resolveCenters($person) ); if (0 === count($scopes)) { return []; } $qb ->join('ap.participations', 'participation') ->where($qb->expr()->eq('participation.person', ':person')) ->setParameter('person', $person); $qb = $this->addACLClauses($qb, $scopes, $scopesCanSeeConfidential); $qb = $this->addOrderLimitClauses($qb, $orderBy, $limit, $offset); return $qb->getQuery()->getResult(); } public function addOrderLimitClauses(QueryBuilder $qb, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): QueryBuilder { if (null !== $orderBy) { foreach ($orderBy as $field => $order) { $qb->addOrderBy('ap.' . $field, $order); } } if (null !== $limit) { $qb->setMaxResults($limit); } if (null !== $offset) { $qb->setFirstResult($offset); } return $qb; } /** * @param QueryBuilder $qb where the accompanying period have the `ap` alias * @param array $scopesCanSee * @param array $scopesCanSeeConfidential * @return QueryBuilder */ public function addACLClauses(QueryBuilder $qb, array $scopesCanSee, array $scopesCanSeeConfidential): QueryBuilder { $qb ->andWhere( $qb->expr()->orX( $qb->expr()->neq('ap.step', ':draft'), $qb->expr()->orX( $qb->expr()->eq('ap.createdBy', ':creator'), $qb->expr()->isNull('ap.createdBy') ) ) ) ->setParameter('draft', AccompanyingPeriod::STEP_DRAFT) ->setParameter('user', $this->security->getUser()) ->setParameter('creator', $this->security->getUser()); // add join condition for scopes $orx = $qb->expr()->orX( // even if the scope is not in one authorized, the user can see the course if it is in DRAFT state $qb->expr()->eq('ap.step', ':draft') ); foreach ($scopesCanSee as $key => $scope) { // for each scope: // - either the user is the referrer of the course // - or the accompanying course is one of the reachable scopes // - and the parcours is not confidential OR the user is the referrer OR the user can see the confidential course $orOnScope = $qb->expr()->orX( $qb->expr()->isMemberOf(':scope_' . $key, 'ap.scopes'), $qb->expr()->eq('ap.user', ':user') ); if (in_array($scope, $scopesCanSeeConfidential, true)) { $orx->add($orOnScope); } else { // we must add a condition: the course is not confidential or the user is the referrer $andXOnScope = $qb->expr()->andX( $orOnScope, $qb->expr()->orX( 'ap.confidential = FALSE', $qb->expr()->eq('ap.user', ':user') ) ); $orx->add($andXOnScope); } $qb->setParameter('scope_' . $key, $scope); } $qb->andWhere($orx); return $qb; } public function findByUnDispatched(array $jobs, array $services, array $administrativeLocations, ?int $limit = null, ?int $offset = null): array { $qb = $this->addACLByUnDispatched($this->buildQueryUnDispatched($jobs, $services, $administrativeLocations)); $qb->select('ap'); if (null !== $limit) { $qb->setMaxResults($limit); } if (null !== $offset) { $qb->setFirstResult($offset); } return $qb->getQuery()->getResult(); } public function findByUserAndPostalCodesOpenedAccompanyingPeriod(?User $user, array $postalCodes, array $orderBy = [], int $limit = 0, int $offset = 50): array { if (null === $user) { return []; } $qb = $this->buildQueryOpenedAccompanyingCourseByUser($user); $qb->setFirstResult($offset) ->setMaxResults($limit); foreach ($orderBy as $field => $direction) { $qb->addOrderBy('ap.' . $field, $direction); } return $qb->getQuery()->getResult(); } public function findByUserOpenedAccompanyingPeriod(?User $user, array $orderBy = [], int $limit = 0, int $offset = 50): array { if (null === $user) { return []; } $qb = $this->buildQueryOpenedAccompanyingCourseByUser($user); $qb->setFirstResult($offset) ->setMaxResults($limit); foreach ($orderBy as $field => $direction) { $qb->addOrderBy('ap.' . $field, $direction); } return $qb->getQuery()->getResult(); } private function addACLByUnDispatched(QueryBuilder $qb): QueryBuilder { $centers = $this->authorizationHelper->getReachableCenters( AccompanyingPeriodVoter::SEE ); $orX = $qb->expr()->orX(); if (0 === count($centers)) { return $qb->andWhere("'FALSE' = 'TRUE'"); } foreach ($centers as $key => $center) { $scopes = $this->authorizationHelper ->getReachableScopes( AccompanyingPeriodVoter::SEE, $center ); $and = $qb->expr()->andX( $qb->expr()->exists('SELECT part FROM ' . AccompanyingPeriodParticipation::class . ' part ' . "JOIN part.person p WHERE part.accompanyingPeriod = ap.id AND p.center = :center_{$key}") ); $qb->setParameter('center_' . $key, $center); $orScope = $qb->expr()->orX(); foreach ($scopes as $skey => $scope) { $orScope->add( $qb->expr()->isMemberOf(':scope_' . $key . '_' . $skey, 'ap.scopes') ); $qb->setParameter('scope_' . $key . '_' . $skey, $scope); } $and->add($orScope); $orX->add($and); } return $qb->andWhere($orX); } /** * @param array|UserJob[] $jobs * @param array|Scope[] $services * @param array|Location[] $locations */ private function buildQueryUnDispatched(array $jobs, array $services, array $locations): QueryBuilder { $qb = $this->accompanyingPeriodRepository->createQueryBuilder('ap'); $qb->where( $qb->expr()->andX( $qb->expr()->isNull('ap.user'), $qb->expr()->neq('ap.step', ':draft'), $qb->expr()->neq('ap.step', ':closed') ) ) ->setParameter('draft', AccompanyingPeriod::STEP_DRAFT) ->setParameter('closed', AccompanyingPeriod::STEP_CLOSED); if (0 < count($jobs)) { $qb->andWhere($qb->expr()->in('ap.job', ':jobs')) ->setParameter('jobs', $jobs); } if (0 < count($locations)) { $qb->andWhere($qb->expr()->in('ap.administrativeLocation', ':locations')) ->setParameter('locations', $locations); } if (0 < count($services)) { $or = $qb->expr()->orX(); foreach ($services as $key => $service) { $or->add($qb->expr()->isMemberOf(':scope_' . $key, 'ap.scopes')); $qb->setParameter('scope_' . $key, $service); } $qb->andWhere($or); } return $qb; } }