mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
Link between address and reference, and api endpoint to find household
by address reference * add a one-to-many link between address and address reference; * update AddAddress.vue to add information on picked address reference; * add an HouseholdACLAwareRepository, with a method to find household by current address reference * add an endpoint to retrieve household by address reference * + tests
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
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 Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
final class HouseholdACLAwareRepository implements HouseholdACLAwareRepositoryInterface
|
||||
{
|
||||
|
||||
private EntityManagerInterface $em;
|
||||
|
||||
private AuthorizationHelper $authorizationHelper;
|
||||
|
||||
private Security $security;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, AuthorizationHelper $authorizationHelper, Security $security)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->authorizationHelper = $authorizationHelper;
|
||||
$this->security = $security;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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 addACL(QueryBuilder $qb, string $alias = 'h'): QueryBuilder
|
||||
{
|
||||
$centers = $this->authorizationHelper->getReachableCenters(
|
||||
$this->security->getUser(),
|
||||
HouseholdVoter::SHOW
|
||||
);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user