Files
myep-team/src/Repository/UserRepository.php
T

182 lines
5.7 KiB
PHP

<?php
namespace App\Repository;
use App\Entity\User;
use App\Repository\Traits\QueryHelperTrait;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @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
{
use QueryHelperTrait;
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
/**
* Users holding an administrative role or awaiting approval for one, plus blocked
* accounts without a teamer. A user demoted by the CRM may keep no role that would list
* them, and a block is only ever lifted from a list: teamers are unblocked from theirs,
* everyone else has none but this one.
*
* @return User[]
*/
public function getAdministrativeUsers(): array
{
$qb = $this->createQueryBuilder('u');
// administrative roles, plus the pending markers standing in for them
$roles = [...array_keys(User::PENDING_ROLES), ...array_values(User::PENDING_ROLES)];
foreach ($roles as $index => $role) {
$qb
->orWhere('JSON_CONTAINS(u.roles, :role'.$index.') = 1')
->setParameter('role'.$index, json_encode($role))
;
}
$qb->orWhere($qb->expr()->andX(
$qb->expr()->isNotNull('u.disabledAt'),
$qb->expr()->isNull('u.teamer'),
));
return $qb
// andWhere after a chain of orWhere yields "(... OR ...) AND ...", so this
// narrows the whole list rather than widening it by one more alternative
->andWhere($qb->expr()->isNull('u.deletedAt'))
->orderBy('u.lastName', \SortDirection::Ascending)
->getQuery()
->getResult()
;
}
/**
* Active users holding at least one of the given roles.
*
* @param string[] $roles
*
* @return User[]
*/
public function getUsersByRoles(array $roles): array
{
$qb = $this->createQueryBuilder('user');
$roleMatches = $qb->expr()->orX();
foreach ($roles as $index => $role) {
$roleMatches->add('JSON_CONTAINS(user.roles, :role_'.$index.') = 1');
$qb->setParameter('role_'.$index, json_encode($role));
}
return $qb
->andWhere($roleMatches)
->andWhere($qb->expr()->isNull('user.deletedAt'))
->andWhere($qb->expr()->isNull('user.disabledAt'))
->orderBy('user.lastName', \SortDirection::Ascending)
->addOrderBy('user.firstName', \SortDirection::Ascending)
->getQuery()
->getResult()
;
}
/**
* @param string[]|string $hotelCode
*
* @return User[]
*/
public function getUsersByRoleAndHotelCode(string $role, mixed $hotelCode): array
{
$hotelCodes = (array) $hotelCode;
$hotelCodesBase = array_map(function ($code) {
return substr($code, 0, 3);
}, $hotelCodes);
$qb = $this->createQueryBuilder('user');
foreach ($hotelCodesBase as $index => $code) {
$qb
->orWhere('JSON_CONTAINS(user.hotelCodes, :hotelCode_'.$index.') = 1')
->setParameter('hotelCode_'.$index, json_encode($code))
;
}
return $qb
->andWhere('JSON_CONTAINS(user.roles, :role) = 1')
->andWhere($qb->expr()->isNull('user.deletedAt'))
->setParameter('role', json_encode($role))
->getQuery()
->getResult()
;
}
/**
* The house managers the nightly reminder may write to: not deleted and not blocked - a blocked
* account cannot log in to do what it is reminded of. Hotel codes and muted notifications are
* the caller's to check.
*
* @return User[]
*/
public function findHouseManagersToRemind(): array
{
$qb = $this->createQueryBuilder('user');
return $qb
->where('JSON_CONTAINS(user.roles, :role) = 1')
->andWhere($qb->expr()->isNull('user.deletedAt'))
->andWhere($qb->expr()->isNull('user.disabledAt'))
->setParameter('role', json_encode('ROLE_HOUSE_MANAGER'))
->orderBy('user.id')
->getQuery()
->getResult()
;
}
public function getAutocompletionData(string $search, string $role): array
{
$qb = $this->createQueryBuilder('user');
$users = $qb
->where($qb->expr()->orX(
$qb->expr()->like('user.lastName', ':search'),
$qb->expr()->like('user.firstName', ':search'),
))
->andWhere('JSON_CONTAINS(user.roles, :role) = 1')
->andWhere($qb->expr()->isNull('user.deletedAt'))
->orderBy('user.lastName', \SortDirection::Ascending)
->addOrderBy('user.firstName', \SortDirection::Ascending)
->setParameter('search', '%'.$this->escapeLikeWildcards($search).'%')
->setParameter('role', json_encode($role))
->getQuery()
->getResult()
;
$data = [
[
'value' => '',
'text' => '...',
],
];
foreach ($users as $user) {
$data[] = [
'value' => $user->getId(),
'text' => (string) $user,
];
}
return $data;
}
}