src/Repository/UserRepository.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Contract;
  4. use App\Entity\InterventionRequest;
  5. use App\Entity\Reminder\Handler;
  6. use App\Entity\Reminder\Log;
  7. use App\Entity\User;
  8. use App\Service\InterventionRequest\WorkflowPlace;
  9. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
  12. /**
  13.  * @method User|null find($id, $lockMode = null, $lockVersion = null)
  14.  * @method User|null findOneBy(array $criteria, array $orderBy = null)
  15.  * @method User[]    findAll()
  16.  * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  17.  */
  18. class UserRepository extends ServiceEntityRepository implements UserLoaderInterface
  19. {
  20.     public function __construct(ManagerRegistry $registry)
  21.     {
  22.         parent::__construct($registryUser::class);
  23.     }
  24.     /**
  25.      * Used to upgrade (rehash) the user's password automatically over time.
  26.      */
  27.     public function upgradePassword(User $userstring $newHashedPassword): void
  28.     {
  29.         $user->setPassword($newHashedPassword);
  30.         $this->_em->persist($user);
  31.         $this->_em->flush();
  32.     }
  33.     /**
  34.      * On every page this method is called to load the user from session
  35.      * We need here to load authorizations and groups in a single query
  36.      * for perf issues.
  37.      *
  38.      * @throws \Doctrine\ORM\NonUniqueResultException
  39.      *
  40.      * @see UserLoaderInterface
  41.      */
  42.     public function loadUserByIdentifier(string $identifier): ?User
  43.     {
  44.         return $this->createQueryBuilder('u')
  45.             ->leftJoin('u.authorizations''ua')
  46.             ->addSelect('ua')
  47.             ->leftJoin('ua.authorization''a')
  48.             ->addSelect('a')
  49.             ->leftJoin('u.groups''ug')
  50.             ->addSelect('ug')
  51.             ->leftJoin('ug.group''g')
  52.             ->addSelect('g')
  53.             ->leftJoin('g.authorizations''gga')
  54.             ->addSelect('gga')
  55.             ->leftJoin('gga.authorization''ga')
  56.             ->addSelect('ga')
  57.             ->andWhere('u.enabled = 1 AND u.deleted=0')
  58.             ->andWhere('u.email = :val')
  59.             ->setParameter('val'$identifier)
  60.             ->getQuery()
  61.             ->getOneOrNullResult()
  62.             ;
  63.     }
  64.     /** @deprecated since Symfony 5.3 */
  65.     public function loadUserByUsername(string $username): ?User
  66.     {
  67.         return $this->loadUserByIdentifier($username);
  68.     }
  69.     // needed to refresh user by the provider
  70.     /**
  71.      * Refreshes the user after being reloaded from the session.
  72.      *
  73.      * When a user is logged in, at the beginning of each request, the
  74.      * User object is loaded from the session and then this method is
  75.      * called. Your job is to make sure the user's data is still fresh by,
  76.      * for example, re-querying for fresh User data.
  77.      *
  78.      * If your firewall is "stateless: true" (for a pure API), this
  79.      * method is not called.
  80.      *
  81.      * @return User
  82.      */
  83.     public function refreshUser(User $user)
  84.     {
  85.         return $this->createQueryBuilder('u')
  86.             ->leftJoin('u.authorizations''ua')
  87.             ->addSelect('ua')
  88.             ->leftJoin('ua.authorization''a')
  89.             ->addSelect('a')
  90.             ->leftJoin('u.groups''ug')
  91.             ->addSelect('ug')
  92.             ->leftJoin('ug.group''g')
  93.             ->addSelect('g')
  94.             ->leftJoin('g.authorizations''gga')
  95.             ->addSelect('gga')
  96.             ->leftJoin('gga.authorization''ga')
  97.             ->addSelect('ga')
  98.             ->andWhere('u.enabled = 1 AND u.deleted=0')
  99.             ->andWhere('u.id = :val')
  100.             ->setParameter('val'$user->getId())
  101.             ->getQuery()
  102.             ->getOneOrNullResult()
  103.             ;
  104.     }
  105.     public function hasAccess(int $subjectCompanyIdint $userCompanyId): bool
  106.     {
  107.         return $this->getEntityManager()
  108.             ->getRepository(Contract::class)
  109.             ->hasContractBetween($subjectCompanyId$userCompanyId);
  110.     }
  111.     public function findLateInterventionRequest(Handler $handler)
  112.     {
  113.         $expression $this->_em->getExpressionBuilder();
  114.         $minDate = (new \DateTimeImmutable())->modify('-2 days');
  115.         $minReminder = (new \DateTimeImmutable())->modify('-1 day');
  116.         return $this
  117.             ->createQueryBuilder('u')
  118.             ->where('u.enabled = 1')
  119.             ->andwhere('u.deleted = 0')
  120.             ->andWhere(
  121.                 $expression->exists(
  122.                     $this->_em
  123.                         ->createQueryBuilder()
  124.                         ->select('i')
  125.                         ->from(InterventionRequest::class, 'i')
  126.                         ->where('i.deleted = 0 and i.enabled = 1')
  127.                         ->andWhere('(i.place = :planning AND i.updatedAt < :minDate)')
  128.                         ->andWhere('intervention_request__available_for__user(i.id, u.id) = true')
  129.                 )
  130.             )
  131.             ->andWhere(
  132.                 $expression->not($expression->exists(
  133.                     $this->_em->getRepository(Log::class)
  134.                     ->createQueryBuilder('l')
  135.                     ->where('l.handler = :handler')
  136.                     ->andWhere('l.createdAt > :minReminder')
  137.                 ))
  138.             )
  139.             ->setParameter('minDate'$minDate)
  140.             ->setParameter('minReminder'$minReminder)
  141.             ->setParameter('handler'$handler)
  142.             ->setParameter('planning'WorkflowPlace::PLANNING)
  143.             ->setMaxResults(50)
  144.             ->getQuery()
  145.             ->getResult()
  146.         ;
  147.     }
  148.     public function getAllUsersForCompany(int $companyId)
  149.     {
  150.         $existingUsers $this->createQueryBuilder('u')
  151.             ->where('u.externalId IS NOT NULL')
  152.             ->andWhere('u.company = :companyId')
  153.             ->setParameter('companyId'$companyId)
  154.             ->getQuery()
  155.             ->getResult();
  156.         $usersByExternalId = [];
  157.         foreach ($existingUsers as $us) {
  158.             $usersByExternalId[$us->getExternalId()] = $us;
  159.         }
  160.         return $usersByExternalId;
  161.     }
  162.     public function getAllByExternalId(int $companyId)
  163.     {
  164.         $existingUsers $this->createQueryBuilder('u')
  165.             ->where('u.externalId IS NOT NULL')
  166.             ->andWhere('u.company = :companyId')
  167.             ->setParameter('companyId'$companyId)
  168.             ->getQuery()
  169.             ->getResult();
  170.         $usersByExternalId = [];
  171.         foreach ($existingUsers as $us) {
  172.             $usersByExternalId[$us->getExternalId()] = $us;
  173.         }
  174.         return $usersByExternalId;
  175.     }
  176. }