<?php
/*
* Created on Fri Dec 02 2022
*
* DAVID-OLIVIER DESCOMBES
*
* @licence
* You may not sell, sub-license, rent or lease any portion of the Software or Documentation to anyone.
*
* Copyright (c) 2022 dodarchitecte.com (https://dodarchitecte.com)
*
* Developed by developpeur-informatique.ma (https://www.developpeur-informatique.ma)
*/
namespace App\Repository;
use App\Entity\Company\Company;
use App\Entity\Enterprise\Enterprise;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
/**
* @extends ServiceEntityRepository<User>
*
* @method User|null find($id, $lockMode = null, $lockVersion = null)
* @method User|null findOneBy(array $criteria, array $orderBy = null)
* @method User[] findAll()
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
public function add(User $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(User $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
}
$user->setPassword($newHashedPassword);
$this->add($user, true);
}
// /**
// * @return User[] Returns an array of User objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('u.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?User
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
public function findGestionnaires()
{
$admin = mb_strtoupper('admin');
$chef = mb_strtoupper('chef');
$secretary = mb_strtoupper('secretary');
return $this->createQueryBuilder('u')
->andWhere('JSON_CONTAINS(u.roles, :admin) = 1')
->orWhere('JSON_CONTAINS(u.roles, :chef) = 1')
->orWhere('JSON_CONTAINS(u.roles, :secretary) = 1')
->setParameter('admin', '"ROLE_' . $admin . '"')
->setParameter('chef', '"ROLE_' . $chef . '"')
->setParameter('secretary', '"ROLE_' . $secretary . '"')
->getQuery()
->getResult();
}
public function getMyGestionnaires()
{
$chef = mb_strtoupper('chef');
$secretary = mb_strtoupper('secretary');
return $this->createQueryBuilder('u')
->andWhere('JSON_CONTAINS(u.roles, :chef) = 1')
->orWhere('JSON_CONTAINS(u.roles, :secretary) = 1')
->setParameter('chef', '"ROLE_' . $chef . '"')
->setParameter('secretary', '"ROLE_' . $secretary . '"')
->getQuery()
->getResult();
}
// public function getMyGestionnaires()
// {
// $super_admin = mb_strtoupper('super_admin');
// return $this->createQueryBuilder('u')
// // ->andWhere('JSON_CONTAINS(u.roles,count(u.roles) == 1')
// ->andWhere('JSON_CONTAINS(u.roles, :super_admin) = 0')
// ->setParameter('super_admin', '"ROLE_' . $super_admin . '"')
// ->getQuery()
// ->getResult();
// }
public function getAdminSecretary()
{
$admin = mb_strtoupper('admin');
$secretary = mb_strtoupper('secretary');
return $this->createQueryBuilder('u')
->andWhere('JSON_CONTAINS(u.roles, :chef) = 1')
->orWhere('JSON_CONTAINS(u.roles, :secretary) = 1')
->setParameter('chef', '"ROLE_' . $admin . '"')
->setParameter('secretary', '"ROLE_' . $secretary . '"')
->getQuery()
->getResult();
}
public function getAdmins()
{
$admin = mb_strtoupper('admin');
return $this->createQueryBuilder('u')
->andWhere('JSON_CONTAINS(u.roles, :admin) = 1')
->setParameter('admin', '"ROLE_' . $admin . '"')
->getQuery()
->getResult();
}
public function findOneByEmail($email)
{
return $this->createQueryBuilder('c')
->andWhere('c.email = :email')
->setParameter('email', $email)
->getQuery()
->getOneOrNullResult();
}
/**
* @param Enterprise $enterprise
* @return User|null
* @throws NonUniqueResultException
*/
public function findEnterpriseOwner(Enterprise $enterprise): User|null
{
return $this->createQueryBuilder("c")
->where('c.enterprise = :enterprise')
->andWhere("c.collaborator is NULL")
->setParameter('enterprise', $enterprise)
->getQuery() ->getOneOrNullResult();
}
}