requestStack->getMainRequest()->headers->get(self::HTTP_X_SWITCH_USER) == $identifier) return $this->getEntityManager()->find(Access::class, $identifier); return $this->findAccessByUsernameAndAccessId($identifier, $this->requestStack->getMainRequest()->headers->get(self::ACCESS_NAME_HEADER)); } /** * Récupère un access grâce à son username et son ID * @param string $username * @param $id * @return mixed * @throws \Doctrine\ORM\NonUniqueResultException */ private function findAccessByUsernameAndAccessId(string $username, $id): mixed { $entityManager = $this->getEntityManager(); return $entityManager->createQuery( 'SELECT a FROM App\Entity\Access\Access a INNER JOIN a.person p WHERE p.username = :query AND a.id = :id' ) ->setParameter('query', $username) ->setParameter('id', $id) ->getOneOrNullResult(); } /** * @param Access $acces * @return mixed * @throws \Exception */ public function findAllValidAccesses(Access $acces): array { $datetime = new \DateTime(); $today = $datetime->format('Y-m-d'); return $this->createQueryBuilder('access') ->innerJoin('access.organization', 'organization') ->innerJoin('organization.networkOrganizations', 'networkOrganizations') ->where('access.person = :person') ->andWhere('networkOrganizations.startDate <= :today') ->setParameter('person', $acces->getPerson()) ->setParameter('today', $today) ->getQuery() ->getResult() ; } public function hasGotFunctionAtThisDate(Access $access, $function, \DateTime $date): bool { $this->_em->getFilters()->disable('date_time_filter'); $qb = $this->createQueryBuilder('access'); $qb ->innerJoin('access.organizationFunction', 'organization_function') ->innerJoin('organization_function.functionType', 'function_type') ->where('function_type.mission = :mission') ->andWhere('access.id = :id') ->setParameter('id', $access->getId()) ->setParameter('mission', $function) ; DateConditions::addDateInPeriodCondition($qb, 'organization_function', $date->format('Y-m-d')); $result = count($qb->getQuery()->getResult()) > 0; $this->_em->getFilters()->enable('date_time_filter'); return $result; } /** * Returns the current number of accesses with the given active role for the * organization. * * @throws \Doctrine\ORM\NonUniqueResultException * @throws \Doctrine\ORM\NoResultException */ public function countAccessesWithActiveMission(int $organizationId, string $function): int { $qb = $this->createQueryBuilder('a'); $qb->select('count(a.id)') ->innerJoin('a.organizationFunction', 'ofn') ->innerJoin('ofn.functionType', 'f') ->innerJoin('a.organization', 'o') ->where($qb->expr()->eq('f.mission', $qb->expr()->literal($function))) ->andWhere($qb->expr()->eq('o.id', $organizationId)); DateConditions::addDateInPeriodCondition($qb, 'ofn', date('Y-m-d')); $q = $qb->getQuery(); return (int)$q->getSingleResult($q::HYDRATE_SINGLE_SCALAR); } /** * Returns the current number of accesses with an admin access to the * organization. * * @throws \Doctrine\ORM\NonUniqueResultException * @throws \Doctrine\ORM\NoResultException */ public function countAdminAccounts(int $organizationId): int { $qb = $this->createQueryBuilder('a'); $qb->select('count(a.id)') ->innerJoin('a.organization', 'o') ->where($qb->expr()->eq('a.adminAccess', true)) ->andWhere($qb->expr()->eq('o.id', $organizationId)); $q = $qb->getQuery(); return (int)$q->getSingleResult($q::HYDRATE_SINGLE_SCALAR); } }