AccessRepository.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Repository\Access;
  4. use App\DQL\DateConditions;
  5. use App\Entity\Access\Access;
  6. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. /**
  11. * @method Access|null find($id, $lockMode = null, $lockVersion = null)
  12. * @method Access|null findOneBy(array $criteria, array $orderBy = null)
  13. * @method Access[] findAll()
  14. * @method Access[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  15. */
  16. class AccessRepository extends ServiceEntityRepository implements UserLoaderInterface
  17. {
  18. const ACCESS_NAME_HEADER = 'X-AccessId';
  19. const HTTP_X_SWITCH_USER = 'X-Switch-User';
  20. public function __construct(ManagerRegistry $registry, private RequestStack $requestStack)
  21. {
  22. parent::__construct($registry, Access::class);
  23. }
  24. /**
  25. * @deprecated in symfony 6.0, replace by loadUserByIdentifier
  26. * @param string $username
  27. * @return \Symfony\Component\Security\Core\User\UserInterface|void|null
  28. */
  29. public function loadUserByUsername(string $username)
  30. {}
  31. /**
  32. * Méthode permettant de fournir un userProvider custom (voir config provider : access_provider)
  33. * @param string $username
  34. * @return Access|mixed|object|\Symfony\Component\Security\Core\User\UserInterface|null
  35. * @throws \Doctrine\ORM\ORMException
  36. * @throws \Doctrine\ORM\OptimisticLockException
  37. * @throws \Doctrine\ORM\TransactionRequiredException
  38. */
  39. public function loadUserByIdentifier($identifier): mixed
  40. {
  41. if($this->requestStack->getMainRequest()->headers->get(self::HTTP_X_SWITCH_USER) == $identifier)
  42. return $this->getEntityManager()->find(Access::class, $identifier);
  43. return $this->findAccessByUsernameAndAccessId($identifier, $this->requestStack->getMainRequest()->headers->get(self::ACCESS_NAME_HEADER));
  44. }
  45. /**
  46. * Récupère un access grâce à son username et son ID
  47. * @param string $username
  48. * @param $id
  49. * @return mixed
  50. * @throws \Doctrine\ORM\NonUniqueResultException
  51. */
  52. private function findAccessByUsernameAndAccessId(string $username, $id): mixed
  53. {
  54. $entityManager = $this->getEntityManager();
  55. return $entityManager->createQuery(
  56. 'SELECT a
  57. FROM App\Entity\Access\Access a
  58. INNER JOIN a.person p
  59. WHERE p.username = :query AND a.id = :id'
  60. )
  61. ->setParameter('query', $username)
  62. ->setParameter('id', $id)
  63. ->getOneOrNullResult();
  64. }
  65. /**
  66. * @param Access $acces
  67. * @return mixed
  68. * @throws \Exception
  69. */
  70. public function findAllValidAccesses(Access $acces): array
  71. {
  72. $datetime = new \DateTime();
  73. $today = $datetime->format('Y-m-d');
  74. return $this->createQueryBuilder('access')
  75. ->innerJoin('access.organization', 'organization')
  76. ->innerJoin('organization.networkOrganizations', 'networkOrganizations')
  77. ->where('access.person = :person')
  78. ->andWhere('networkOrganizations.startDate <= :today')
  79. ->setParameter('person', $acces->getPerson())
  80. ->setParameter('today', $today)
  81. ->getQuery()
  82. ->getResult()
  83. ;
  84. }
  85. public function hasGotFunctionAtThisDate(Access $access, $function, \DateTime $date): bool
  86. {
  87. $this->_em->getFilters()->disable('date_time_filter');
  88. $qb = $this->createQueryBuilder('access');
  89. $qb
  90. ->innerJoin('access.organizationFunction', 'organization_function')
  91. ->innerJoin('organization_function.functionType', 'function_type')
  92. ->where('function_type.mission = :mission')
  93. ->andWhere('access.id = :id')
  94. ->setParameter('id', $access->getId())
  95. ->setParameter('mission', $function)
  96. ;
  97. DateConditions::addDateInPeriodCondition($qb, 'organization_function', $date->format('Y-m-d'));
  98. $result = count($qb->getQuery()->getResult()) > 0;
  99. $this->_em->getFilters()->enable('date_time_filter');
  100. return $result;
  101. }
  102. /**
  103. * Returns the current number of accesses with the given active role for the
  104. * organization.
  105. *
  106. * @throws \Doctrine\ORM\NonUniqueResultException
  107. * @throws \Doctrine\ORM\NoResultException
  108. */
  109. public function countAccessesWithActiveMission(int $organizationId, string $function): int {
  110. $qb = $this->createQueryBuilder('a');
  111. $qb->select('count(a.id)')
  112. ->innerJoin('a.organizationFunction', 'ofn')
  113. ->innerJoin('ofn.functionType', 'f')
  114. ->innerJoin('a.organization', 'o')
  115. ->where($qb->expr()->eq('f.mission', $qb->expr()->literal($function)))
  116. ->andWhere($qb->expr()->eq('o.id', $organizationId));
  117. DateConditions::addDateInPeriodCondition($qb, 'ofn', date('Y-m-d'));
  118. $q = $qb->getQuery();
  119. return (int)$q->getSingleResult($q::HYDRATE_SINGLE_SCALAR);
  120. }
  121. /**
  122. * Returns the current number of accesses with an admin access to the
  123. * organization.
  124. *
  125. * @throws \Doctrine\ORM\NonUniqueResultException
  126. * @throws \Doctrine\ORM\NoResultException
  127. */
  128. public function countAdminAccounts(int $organizationId): int {
  129. $qb = $this->createQueryBuilder('a');
  130. $qb->select('count(a.id)')
  131. ->innerJoin('a.organization', 'o')
  132. ->where($qb->expr()->eq('a.adminAccess', true))
  133. ->andWhere($qb->expr()->eq('o.id', $organizationId));
  134. $q = $qb->getQuery();
  135. return (int)$q->getSingleResult($q::HYDRATE_SINGLE_SCALAR);
  136. }
  137. }