src/Repository/UserRepository.php line 40

Open in your IDE?
  1. <?php
  2. /*
  3.  * Created on Fri Dec 02 2022
  4.  *
  5.  * DAVID-OLIVIER DESCOMBES
  6.  *
  7.  * @licence
  8.  * You may not sell, sub-license, rent or lease any portion of the Software or Documentation to anyone.
  9.  *
  10.  * Copyright (c) 2022 dodarchitecte.com (https://dodarchitecte.com)
  11.  *
  12.  * Developed by developpeur-informatique.ma (https://www.developpeur-informatique.ma)
  13.  */
  14. namespace App\Repository;
  15. use App\Entity\Company\Company;
  16. use App\Entity\Enterprise\Enterprise;
  17. use App\Entity\User;
  18. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  19. use Doctrine\ORM\NonUniqueResultException;
  20. use Doctrine\Persistence\ManagerRegistry;
  21. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  22. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  23. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  24. /**
  25.  * @extends ServiceEntityRepository<User>
  26.  *
  27.  * @method User|null find($id, $lockMode = null, $lockVersion = null)
  28.  * @method User|null findOneBy(array $criteria, array $orderBy = null)
  29.  * @method User[]    findAll()
  30.  * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  31.  */
  32. class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
  33. {
  34.     public function __construct(ManagerRegistry $registry)
  35.     {
  36.         parent::__construct($registryUser::class);
  37.     }
  38.     public function add(User $entitybool $flush false): void
  39.     {
  40.         $this->getEntityManager()->persist($entity);
  41.         if ($flush) {
  42.             $this->getEntityManager()->flush();
  43.         }
  44.     }
  45.     public function remove(User $entitybool $flush false): void
  46.     {
  47.         $this->getEntityManager()->remove($entity);
  48.         if ($flush) {
  49.             $this->getEntityManager()->flush();
  50.         }
  51.     }
  52.     /**
  53.      * Used to upgrade (rehash) the user's password automatically over time.
  54.      */
  55.     public function upgradePassword(PasswordAuthenticatedUserInterface $userstring $newHashedPassword): void
  56.     {
  57.         if (!$user instanceof User) {
  58.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.'\get_class($user)));
  59.         }
  60.         $user->setPassword($newHashedPassword);
  61.         $this->add($usertrue);
  62.     }
  63.     //    /**
  64.     //     * @return User[] Returns an array of User objects
  65.     //     */
  66.     //    public function findByExampleField($value): array
  67.     //    {
  68.     //        return $this->createQueryBuilder('u')
  69.     //            ->andWhere('u.exampleField = :val')
  70.     //            ->setParameter('val', $value)
  71.     //            ->orderBy('u.id', 'ASC')
  72.     //            ->setMaxResults(10)
  73.     //            ->getQuery()
  74.     //            ->getResult()
  75.     //        ;
  76.     //    }
  77.     //    public function findOneBySomeField($value): ?User
  78.     //    {
  79.     //        return $this->createQueryBuilder('u')
  80.     //            ->andWhere('u.exampleField = :val')
  81.     //            ->setParameter('val', $value)
  82.     //            ->getQuery()
  83.     //            ->getOneOrNullResult()
  84.     //        ;
  85.     //    }
  86.     public function findGestionnaires()
  87.     {
  88.         $admin mb_strtoupper('admin');
  89.         $chef mb_strtoupper('chef');
  90.         $secretary mb_strtoupper('secretary');
  91.         return $this->createQueryBuilder('u')
  92.             ->andWhere('JSON_CONTAINS(u.roles, :admin) = 1')
  93.             ->orWhere('JSON_CONTAINS(u.roles, :chef) = 1')
  94.             ->orWhere('JSON_CONTAINS(u.roles, :secretary) = 1')
  95.             ->setParameter('admin''"ROLE_' $admin '"')
  96.             ->setParameter('chef''"ROLE_' $chef '"')
  97.             ->setParameter('secretary''"ROLE_' $secretary '"')
  98.             ->getQuery()
  99.             ->getResult();
  100.     }
  101.     public function getMyGestionnaires()
  102.     {
  103.         $chef mb_strtoupper('chef');
  104.         $secretary mb_strtoupper('secretary');
  105.         return $this->createQueryBuilder('u')
  106.             ->andWhere('JSON_CONTAINS(u.roles, :chef) = 1')
  107.             ->orWhere('JSON_CONTAINS(u.roles, :secretary) = 1')
  108.             ->setParameter('chef''"ROLE_' $chef '"')
  109.             ->setParameter('secretary''"ROLE_' $secretary '"')
  110.             ->getQuery()
  111.             ->getResult();
  112.     }
  113.     // public function getMyGestionnaires()
  114.     // {
  115.     //     $super_admin = mb_strtoupper('super_admin');
  116.     //     return $this->createQueryBuilder('u')
  117.     //         // ->andWhere('JSON_CONTAINS(u.roles,count(u.roles) == 1')
  118.     //         ->andWhere('JSON_CONTAINS(u.roles, :super_admin) = 0')
  119.     //         ->setParameter('super_admin', '"ROLE_' . $super_admin . '"')
  120.     //         ->getQuery()
  121.     //         ->getResult();
  122.     // }
  123.     public function getAdminSecretary()
  124.     {
  125.         $admin mb_strtoupper('admin');
  126.         $secretary mb_strtoupper('secretary');
  127.         return $this->createQueryBuilder('u')
  128.             ->andWhere('JSON_CONTAINS(u.roles, :chef) = 1')
  129.             ->orWhere('JSON_CONTAINS(u.roles, :secretary) = 1')
  130.             ->setParameter('chef''"ROLE_' $admin '"')
  131.             ->setParameter('secretary''"ROLE_' $secretary '"')
  132.             ->getQuery()
  133.             ->getResult();
  134.     }
  135.     public function getAdmins()
  136.     {
  137.         $admin mb_strtoupper('admin');
  138.         return $this->createQueryBuilder('u')
  139.             ->andWhere('JSON_CONTAINS(u.roles, :admin) = 1')
  140.             ->setParameter('admin''"ROLE_' $admin '"')
  141.             ->getQuery()
  142.             ->getResult();
  143.     }
  144.     public function findOneByEmail($email)
  145.     {
  146.         return $this->createQueryBuilder('c')
  147.             ->andWhere('c.email = :email')
  148.             ->setParameter('email'$email)
  149.             ->getQuery()
  150.             ->getOneOrNullResult();
  151.     }
  152.     /**
  153.      * @param Enterprise $enterprise
  154.      * @return User|null
  155.      * @throws NonUniqueResultException
  156.      */
  157.     public function findEnterpriseOwner(Enterprise $enterprise): User|null
  158.     {
  159.         return $this->createQueryBuilder("c")
  160.             ->where('c.enterprise = :enterprise')
  161.             ->andWhere("c.collaborator is NULL")
  162.             ->setParameter('enterprise'$enterprise)
  163.             ->getQuery() ->getOneOrNullResult();
  164.     }
  165. }