src/Repository/UserRepository.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\User;
  4. use App\Utils\Doctrine\Entity\BaseUserEntity;
  5. use App\Utils\Paginator\Repository\AjaxListingRepositoryInterface;
  6. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Doctrine\ORM\QueryBuilder;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  12. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. /**
  15.  * @method User|null find($id, $lockMode = null, $lockVersion = null)
  16.  * @method User|null findOneBy(array $criteria, array $orderBy = null)
  17.  * @method User[]    findAll()
  18.  * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  19.  */
  20. class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterfaceAjaxListingRepositoryInterface
  21. {
  22.     const ROLE_ADMIN 'ROLE_ADMIN';
  23.     const ROLE_ADMIN_SERVICE_PROVIDER 'ROLE_ADMIN_SERVICE_PROVIDER';
  24.     const ROLE_SERVICE_PROVIDER 'ROLE_SERVICE_PROVIDER';
  25.     const ROLE_TECHNICIAN 'ROLE_TECHNICIAN';
  26.     const METHOD_LISTING_NAME_USERS_ADMIN_SERVICE_PROVIDER 'getUsersAdminServiceProvider';
  27.     const METHOD_LISTING_COUNT_NAME_USERS_ADMIN_SERVICE_PROVIDER 'getCountUsersAdminServiceProvider';
  28.     const METHOD_LISTING_NAME_USERS_SERVICE_PROVIDER 'getUsersServiceProvider';
  29.     const METHOD_LISTING_COUNT_NAME_USERS_SERVICE_PROVIDER 'getCountUsersServiceProvider';
  30.     public function __construct(ManagerRegistry $registryEntityManagerInterface $manager)
  31.     {
  32.         parent::__construct($registryUser::class);
  33.         $this->manager $manager;
  34.     }
  35.     private function getUser(): QueryBuilder
  36.     {
  37.         return $this->createQueryBuilder('user');
  38.     }
  39.     private function getUserNotDeleted(): QueryBuilder
  40.     {
  41.         $qb $this->getUser();
  42.         $qb->andWhere('user.isDeleted = false');
  43.         return $qb;
  44.     }
  45.     /**
  46.      * Used to upgrade (rehash) the user's password automatically over time.
  47.      */
  48.     public function upgradePassword(UserInterface $userstring $newHashedPassword): void
  49.     {
  50.         if (!$user instanceof User) {
  51.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.'\get_class($user)));
  52.         }
  53.         $user->setPassword($newHashedPassword);
  54.         $this->_em->persist($user);
  55.         $this->_em->flush();
  56.     }
  57.     public function getUserByIdentifier(string $identifier): ?User
  58.     {
  59.         $qb $this->getUser();
  60.         $qb->andWhere('user.identifier = :identifier')
  61.             ->setParameter('identifier'$identifier);
  62.         return $qb->getQuery()->getOneOrNullResult();
  63.     }
  64.     public function ajaxListing(int $startOffsetint $length, ?string $search, array $context = []): array
  65.     {
  66.         $method $context['methodListingName'];
  67.         if (!method_exists($this$method)) {
  68.             throw new \Exception('Method name ' $method ' not defined in class : ' self::class);
  69.         }
  70.         return $this->$method($startOffset$length$search$context);
  71.     }
  72.     public function countListingSearch(?string $search, array $context = []): ?int
  73.     {
  74.         $method $context['methodCountListingName'];
  75.         if (!method_exists($this$method)) {
  76.             throw new \Exception('Method name ' $method ' not defined in class : ' self::class);
  77.         }
  78.         return $this->$method($search$context);
  79.     }
  80.     public function getUsersAdminServiceProvider(int $startOffsetint $length, ?string $search, array $context = []): array
  81.     {
  82.         $qb $this->getUserNotDeleted();
  83.         $qb->orderBy('user.updatedAt''DESC')
  84.             ->setFirstResult($startOffset)
  85.             ->setMaxResults($length);
  86.         $qb->andWhere('user.roles LIKE :roles')
  87.             ->setParameter('roles''%' BaseUserEntity::ROLE_ADMIN_SERVICE_PROVIDER '%');
  88.         return $qb->getQuery()->getResult();
  89.     }
  90.     public function getCountUsersAdminServiceProvider(?string $search): int
  91.     {
  92.         $qb $this->getUserNotDeleted();
  93.         $qb->andWhere('user.roles LIKE :roles')
  94.             ->setParameter('roles''%' BaseUserEntity::ROLE_ADMIN_SERVICE_PROVIDER '%')
  95.             ->select('count(user.id)');
  96.         return $qb->getQuery()->getSingleScalarResult();
  97.     }
  98.     public function getEnabledUserProviders($organisme null)
  99.     {
  100.         $qb $this->createQueryBuilder('user');
  101.         $qb
  102.             ->andWhere('user.isDeleted = false')
  103.             ->andWhere('user.isEnabled = true')
  104.             ->orderBy('user.updatedAt''DESC')
  105.             ->andWhere('user.roles LIKE :roles')
  106.             ->setParameter('roles''%' BaseUserEntity::ROLE_SERVICE_PROVIDER '%');
  107.         if ($organisme != null) {
  108.             $qb
  109.                 ->leftJoin('user.organisme''organisme')
  110.                 ->andWhere('organisme.name = :organisme ')
  111.                 ->setParameter('organisme'$organisme);
  112.         }
  113.         return $qb->getQuery()->getResult();
  114.     }
  115.     public function getUsersServiceProvider(int $startOffsetint $length, ?string $search, array $context = []): array
  116.     {
  117.         $qb $this->createQueryBuilder('user')
  118.             //->leftJoin('user.parent', 'parent')
  119.             //->addSelect('parent')
  120.         ;
  121.         $this->addFilterUsersServiceProvider($qb$search$context);
  122.         $qb
  123.             ->orderBy('user.updatedAt''DESC')
  124.             ->setFirstResult($startOffset)
  125.             ->setMaxResults($length)
  126.             //->leftJoin('user.userTechnicians', 'user_technicians')
  127.             //->addSelect('user_technicians')
  128.         ;
  129.         return $qb->getQuery()->getResult();
  130.     }
  131.     public function getCountUsersServiceProvider(?string $search, array $context = []): ?int
  132.     {
  133.         $qb $this->createQueryBuilder('user')
  134.             //->leftJoin('user.parent', 'parent')
  135.             //->addSelect('parent')
  136.         ;
  137.         $this->addFilterUsersServiceProvider($qb$search$context);
  138.         $qb->select('count(user.id)');
  139.         return $qb->getQuery()->getSingleScalarResult();
  140.     }
  141.     public function getQueryBuilderSelectUserServiceProvider(int $userId, ?bool $allowSelectUser): QueryBuilder
  142.     {
  143.         $qb $this->getUserNotDeleted();
  144.         $qb->andWhere('user.roles LIKE :roles')
  145.             ->setParameter('roles''%' BaseUserEntity::ROLE_ADMIN_SERVICE_PROVIDER '%')
  146.             ->orderBy('user.updatedAt''DESC');
  147.         if (!$allowSelectUser) {
  148.             $qb->andWhere('user.id = :id')
  149.                 ->setParameter('id'$userId);
  150.         }
  151.         return $qb;
  152.     }
  153.     public function getQueryBuilderSelectUserServiceProvider2(int $userId, ?bool $allowSelectUser): QueryBuilder
  154.     {
  155.         $qb $this->getUserNotDeleted();
  156.         $qb->andWhere('user.roles LIKE :roles')
  157.             ->setParameter('roles''%' BaseUserEntity::ROLE_SERVICE_PROVIDER '%')
  158.             ->orderBy('user.updatedAt''DESC');
  159.         if ($allowSelectUser) {
  160.             return $qb;
  161.         }
  162.         $qb->innerJoin('user.parent''parent');
  163.         $qb->andWhere('user.id = :id or parent.id = :id')
  164.             ->setParameter('id'$userId)
  165.             ->andWhere('parent.isDeleted = false');
  166.         return $qb;
  167.     }
  168.     /**
  169.      * @param string $role
  170.      * @return array<User>
  171.      */
  172.     public function getEnabledUserByRole(): array
  173.     {
  174.         $qb $this->createQueryBuilder('user');
  175.         $qb
  176.             ->andWhere('user.isDeleted = false')
  177.             ->andWhere('user.isEnabled = true')
  178.             ->orderBy('user.updatedAt''DESC');
  179.         return $qb->getQuery()->getResult();
  180.     }
  181.     public function getEnabledUserByRolesAndOrganisme(): array
  182.     {
  183.         $qb $this->createQueryBuilder('user');
  184.         $qb
  185.             ->andWhere('user.isDeleted = false')
  186.             ->andWhere('user.isEnabled = true')
  187.             ->groupBy('user.organisme')
  188.             ->orderBy('user.updatedAt''DESC');
  189.         return $qb->getQuery()->getResult();
  190.     }
  191.     public function getUserServiceProviderByOrganisme($query$role)
  192.     {
  193.         $qb $this->createQueryBuilder('user');
  194.         if (!empty($query)) {
  195.             $qb
  196.                 ->andWhere('user.organisme = :organisme')
  197.                 ->setParameter('organisme'$query);
  198.         }
  199.         $qb->andWhere('user.roles LIKE :role')
  200.             ->setParameter('role'"%$role%");
  201.         return $qb->getQuery()->getArrayResult();
  202.     }
  203.     private function addFilterUsersServiceProvider(QueryBuilder $queryBuilder, ?string $search, array $context = []): QueryBuilder
  204.     {
  205. //        dump($context);
  206.         $queryBuilder
  207.             ->andWhere('user.isDeleted = false')
  208.             ->andWhere('user.roles LIKE :roles')
  209.             ->setParameter('roles''%' BaseUserEntity::ROLE_SERVICE_PROVIDER '%');
  210.         if (!empty($search)) {
  211.             $queryBuilder
  212.                 ->andWhere('user.identifier = :search')
  213.                 ->setParameter('search'$search);
  214.         }
  215.         if (isset($context['filterIdUser']) && !empty($context['filterIdUser'])) {
  216.             $queryBuilder
  217.                 ->andWhere('user.id = :id')
  218.                 ->setParameter('id'$context['filterIdUser']);
  219.         }
  220.         if (isset($context['filter_user']) && ($context['filter_user'] !== null) && !empty($context['filter_user'])) {
  221.             $queryBuilder
  222.                 ->innerJoin('user.organisme','organisme')
  223.                 ->andWhere('organisme.name = :organisme')
  224.                 ->setParameter('organisme'$context['filter_user']);
  225.         }
  226.         //if ($context['filterUserId']) {
  227.         //$queryBuilder->andWhere('parent.id = :idParent')
  228.         //    ->setParameter('idParent', $context['filterUserId'])
  229.         //;
  230.         //}
  231.         return $queryBuilder;
  232.     }
  233. }