71 lines
2.2 KiB
PHP
71 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repository;
|
|
|
|
use App\Entity\User;
|
|
use App\Form\Model\Filter\UserFilterDto;
|
|
use App\Repository\Filter\AppliesListFiltersTrait;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\ORM\QueryBuilder;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
/**
|
|
* @extends ServiceEntityRepository<User>
|
|
*/
|
|
class UserRepository extends ServiceEntityRepository
|
|
{
|
|
use AppliesListFiltersTrait;
|
|
|
|
public function __construct(ManagerRegistry $registry)
|
|
{
|
|
parent::__construct($registry, User::class);
|
|
}
|
|
|
|
public function createFilteredQueryBuilder(UserFilterDto $filter): QueryBuilder
|
|
{
|
|
$qb = $this->createQueryBuilder('user');
|
|
|
|
$this->applySearchTerm($qb, $filter->searchTerm(), [
|
|
'user.email',
|
|
'user.firstName',
|
|
'user.lastName',
|
|
'user.addressId',
|
|
'user.personId',
|
|
]);
|
|
|
|
$this->applyDateWithin($qb, 'user.lastLoginAt', $filter->dateFrom, $filter->dateTo);
|
|
|
|
if (null !== $filter->role && '' !== $filter->role) {
|
|
// Same reasoning as findGroupsStaff(): the roles are a JSON array column and no
|
|
// JSON_CONTAINS is registered, so the needle carries its quotes to stay anchored
|
|
// to a whole array entry.
|
|
$qb
|
|
->andWhere('user.roles LIKE :role')
|
|
->setParameter('role', '%"'.$filter->role.'"%')
|
|
;
|
|
}
|
|
|
|
return $qb;
|
|
}
|
|
|
|
/**
|
|
* Users who can look after a group booking. The roles are a JSON array column and no
|
|
* JSON_CONTAINS is registered, so the needles carry their quotes to stay anchored to
|
|
* whole array entries. Only the literal roles count — a plain ROLE_ADMIN is not offered.
|
|
*
|
|
* @return User[]
|
|
*/
|
|
public function findGroupsStaff(): array
|
|
{
|
|
return $this->createQueryBuilder('u')
|
|
->andWhere('u.roles LIKE :groupsAdmin OR u.roles LIKE :groupsManager')
|
|
->setParameter('groupsAdmin', '%"ROLE_GROUPS_ADMIN"%')
|
|
->setParameter('groupsManager', '%"ROLE_GROUPS_MANAGER"%')
|
|
->orderBy('u.email', 'ASC')
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
}
|