mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
112 lines
3.3 KiB
PHP
112 lines
3.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\PersonBundle\Repository\Household;
|
|
|
|
use Chill\MainBundle\Entity\AddressReference;
|
|
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
|
use Chill\PersonBundle\Entity\Household\Household;
|
|
use Chill\PersonBundle\Security\Authorization\HouseholdVoter;
|
|
use DateTime;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\ORM\QueryBuilder;
|
|
use Symfony\Component\Security\Core\Security;
|
|
|
|
final class HouseholdACLAwareRepository implements HouseholdACLAwareRepositoryInterface
|
|
{
|
|
private AuthorizationHelper $authorizationHelper;
|
|
|
|
private EntityManagerInterface $em;
|
|
|
|
private Security $security;
|
|
|
|
public function __construct(EntityManagerInterface $em, AuthorizationHelper $authorizationHelper, Security $security)
|
|
{
|
|
$this->em = $em;
|
|
$this->authorizationHelper = $authorizationHelper;
|
|
$this->security = $security;
|
|
}
|
|
|
|
public function addACL(QueryBuilder $qb, string $alias = 'h'): QueryBuilder
|
|
{
|
|
$centers = $this->authorizationHelper->getReachableCenters(
|
|
$this->security->getUser(),
|
|
HouseholdVoter::SEE
|
|
);
|
|
|
|
if ([] === $centers) {
|
|
return $qb
|
|
->andWhere("'FALSE' = 'TRUE'");
|
|
}
|
|
|
|
$qb
|
|
->join($alias . '.members', 'members')
|
|
->join('members.person', 'person')
|
|
->andWhere(
|
|
$qb->expr()->in('person.center', ':centers')
|
|
)
|
|
->setParameter('centers', $centers);
|
|
|
|
return $qb;
|
|
}
|
|
|
|
public function buildQueryByAddressReference(AddressReference $addressReference): QueryBuilder
|
|
{
|
|
$qb = $this->em->createQueryBuilder();
|
|
$qb
|
|
->select('h')
|
|
->from(Household::class, 'h')
|
|
->join('h.addresses', 'address')
|
|
->where(
|
|
$qb->expr()->eq('address.addressReference', ':reference')
|
|
)
|
|
->setParameter(':reference', $addressReference)
|
|
->andWhere(
|
|
$qb->expr()->andX(
|
|
$qb->expr()->lte('address.validFrom', ':today'),
|
|
$qb->expr()->orX(
|
|
$qb->expr()->isNull('address.validTo'),
|
|
$qb->expr()->gt('address.validTo', ':today')
|
|
)
|
|
)
|
|
)
|
|
->setParameter('today', new DateTime('today'));
|
|
|
|
return $qb;
|
|
}
|
|
|
|
public function countByAddressReference(AddressReference $addressReference): int
|
|
{
|
|
$qb = $this->buildQueryByAddressReference($addressReference);
|
|
$qb = $this->addACL($qb);
|
|
|
|
return $qb->select('COUNT(h)')
|
|
->getQuery()
|
|
->getSingleScalarResult();
|
|
}
|
|
|
|
public function findByAddressReference(
|
|
AddressReference $addressReference,
|
|
?int $firstResult = 0,
|
|
?int $maxResult = 50
|
|
): array {
|
|
$qb = $this->buildQueryByAddressReference($addressReference);
|
|
$qb = $this->addACL($qb);
|
|
|
|
return $qb
|
|
->select('h')
|
|
->setFirstResult($firstResult)
|
|
->setMaxResults($maxResult)
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
}
|