<?php
namespace App\Repository;
use App\Entity\User;
use App\Utils\Doctrine\Entity\BaseUserEntity;
use App\Utils\Paginator\Repository\AjaxListingRepositoryInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @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, AjaxListingRepositoryInterface
{
const ROLE_ADMIN = 'ROLE_ADMIN';
const ROLE_ADMIN_SERVICE_PROVIDER = 'ROLE_ADMIN_SERVICE_PROVIDER';
const ROLE_SERVICE_PROVIDER = 'ROLE_SERVICE_PROVIDER';
const ROLE_TECHNICIAN = 'ROLE_TECHNICIAN';
const METHOD_LISTING_NAME_USERS_ADMIN_SERVICE_PROVIDER = 'getUsersAdminServiceProvider';
const METHOD_LISTING_COUNT_NAME_USERS_ADMIN_SERVICE_PROVIDER = 'getCountUsersAdminServiceProvider';
const METHOD_LISTING_NAME_USERS_SERVICE_PROVIDER = 'getUsersServiceProvider';
const METHOD_LISTING_COUNT_NAME_USERS_SERVICE_PROVIDER = 'getCountUsersServiceProvider';
public function __construct(ManagerRegistry $registry, EntityManagerInterface $manager)
{
parent::__construct($registry, User::class);
$this->manager = $manager;
}
private function getUser(): QueryBuilder
{
return $this->createQueryBuilder('user');
}
private function getUserNotDeleted(): QueryBuilder
{
$qb = $this->getUser();
$qb->andWhere('user.isDeleted = false');
return $qb;
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword(UserInterface $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->_em->persist($user);
$this->_em->flush();
}
public function getUserByIdentifier(string $identifier): ?User
{
$qb = $this->getUser();
$qb->andWhere('user.identifier = :identifier')
->setParameter('identifier', $identifier);
return $qb->getQuery()->getOneOrNullResult();
}
public function ajaxListing(int $startOffset, int $length, ?string $search, array $context = []): array
{
$method = $context['methodListingName'];
if (!method_exists($this, $method)) {
throw new \Exception('Method name ' . $method . ' not defined in class : ' . self::class);
}
return $this->$method($startOffset, $length, $search, $context);
}
public function countListingSearch(?string $search, array $context = []): ?int
{
$method = $context['methodCountListingName'];
if (!method_exists($this, $method)) {
throw new \Exception('Method name ' . $method . ' not defined in class : ' . self::class);
}
return $this->$method($search, $context);
}
public function getUsersAdminServiceProvider(int $startOffset, int $length, ?string $search, array $context = []): array
{
$qb = $this->getUserNotDeleted();
$qb->orderBy('user.updatedAt', 'DESC')
->setFirstResult($startOffset)
->setMaxResults($length);
$qb->andWhere('user.roles LIKE :roles')
->setParameter('roles', '%' . BaseUserEntity::ROLE_ADMIN_SERVICE_PROVIDER . '%');
return $qb->getQuery()->getResult();
}
public function getCountUsersAdminServiceProvider(?string $search): int
{
$qb = $this->getUserNotDeleted();
$qb->andWhere('user.roles LIKE :roles')
->setParameter('roles', '%' . BaseUserEntity::ROLE_ADMIN_SERVICE_PROVIDER . '%')
->select('count(user.id)');
return $qb->getQuery()->getSingleScalarResult();
}
public function getEnabledUserProviders($organisme = null)
{
$qb = $this->createQueryBuilder('user');
$qb
->andWhere('user.isDeleted = false')
->andWhere('user.isEnabled = true')
->orderBy('user.updatedAt', 'DESC')
->andWhere('user.roles LIKE :roles')
->setParameter('roles', '%' . BaseUserEntity::ROLE_SERVICE_PROVIDER . '%');
if ($organisme != null) {
$qb
->leftJoin('user.organisme', 'organisme')
->andWhere('organisme.name = :organisme ')
->setParameter('organisme', $organisme);
}
return $qb->getQuery()->getResult();
}
public function getUsersServiceProvider(int $startOffset, int $length, ?string $search, array $context = []): array
{
$qb = $this->createQueryBuilder('user')
//->leftJoin('user.parent', 'parent')
//->addSelect('parent')
;
$this->addFilterUsersServiceProvider($qb, $search, $context);
$qb
->orderBy('user.updatedAt', 'DESC')
->setFirstResult($startOffset)
->setMaxResults($length)
//->leftJoin('user.userTechnicians', 'user_technicians')
//->addSelect('user_technicians')
;
return $qb->getQuery()->getResult();
}
public function getCountUsersServiceProvider(?string $search, array $context = []): ?int
{
$qb = $this->createQueryBuilder('user')
//->leftJoin('user.parent', 'parent')
//->addSelect('parent')
;
$this->addFilterUsersServiceProvider($qb, $search, $context);
$qb->select('count(user.id)');
return $qb->getQuery()->getSingleScalarResult();
}
public function getQueryBuilderSelectUserServiceProvider(int $userId, ?bool $allowSelectUser): QueryBuilder
{
$qb = $this->getUserNotDeleted();
$qb->andWhere('user.roles LIKE :roles')
->setParameter('roles', '%' . BaseUserEntity::ROLE_ADMIN_SERVICE_PROVIDER . '%')
->orderBy('user.updatedAt', 'DESC');
if (!$allowSelectUser) {
$qb->andWhere('user.id = :id')
->setParameter('id', $userId);
}
return $qb;
}
public function getQueryBuilderSelectUserServiceProvider2(int $userId, ?bool $allowSelectUser): QueryBuilder
{
$qb = $this->getUserNotDeleted();
$qb->andWhere('user.roles LIKE :roles')
->setParameter('roles', '%' . BaseUserEntity::ROLE_SERVICE_PROVIDER . '%')
->orderBy('user.updatedAt', 'DESC');
if ($allowSelectUser) {
return $qb;
}
$qb->innerJoin('user.parent', 'parent');
$qb->andWhere('user.id = :id or parent.id = :id')
->setParameter('id', $userId)
->andWhere('parent.isDeleted = false');
return $qb;
}
/**
* @param string $role
* @return array<User>
*/
public function getEnabledUserByRole(): array
{
$qb = $this->createQueryBuilder('user');
$qb
->andWhere('user.isDeleted = false')
->andWhere('user.isEnabled = true')
->orderBy('user.updatedAt', 'DESC');
return $qb->getQuery()->getResult();
}
public function getEnabledUserByRolesAndOrganisme(): array
{
$qb = $this->createQueryBuilder('user');
$qb
->andWhere('user.isDeleted = false')
->andWhere('user.isEnabled = true')
->groupBy('user.organisme')
->orderBy('user.updatedAt', 'DESC');
return $qb->getQuery()->getResult();
}
public function getUserServiceProviderByOrganisme($query, $role)
{
$qb = $this->createQueryBuilder('user');
if (!empty($query)) {
$qb
->andWhere('user.organisme = :organisme')
->setParameter('organisme', $query);
}
$qb->andWhere('user.roles LIKE :role')
->setParameter('role', "%$role%");
return $qb->getQuery()->getArrayResult();
}
private function addFilterUsersServiceProvider(QueryBuilder $queryBuilder, ?string $search, array $context = []): QueryBuilder
{
// dump($context);
$queryBuilder
->andWhere('user.isDeleted = false')
->andWhere('user.roles LIKE :roles')
->setParameter('roles', '%' . BaseUserEntity::ROLE_SERVICE_PROVIDER . '%');
if (!empty($search)) {
$queryBuilder
->andWhere('user.identifier = :search')
->setParameter('search', $search);
}
if (isset($context['filterIdUser']) && !empty($context['filterIdUser'])) {
$queryBuilder
->andWhere('user.id = :id')
->setParameter('id', $context['filterIdUser']);
}
if (isset($context['filter_user']) && ($context['filter_user'] !== null) && !empty($context['filter_user'])) {
$queryBuilder
->innerJoin('user.organisme','organisme')
->andWhere('organisme.name = :organisme')
->setParameter('organisme', $context['filter_user']);
}
//if ($context['filterUserId']) {
//$queryBuilder->andWhere('parent.id = :idParent')
// ->setParameter('idParent', $context['filterUserId'])
//;
//}
return $queryBuilder;
}
}