Add TicketACLAwareRepository with ACL-based ticket retrieval features

- Implement `TicketACLAwareRepositoryInterface` for ticket queries with permission checks.
- Add methods to find and count tickets based on parameters.
- Include unit tests for repository functionality.
This commit is contained in:
Julien Fastré 2025-07-02 18:41:00 +02:00
parent 06e8264dde
commit 9b7dccac9d
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
3 changed files with 166 additions and 0 deletions

View File

@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\TicketBundle\Repository;
use Chill\TicketBundle\Entity\PersonHistory;
use Chill\TicketBundle\Entity\Ticket;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
final readonly class TicketACLAwareRepository implements TicketACLAwareRepositoryInterface
{
public function __construct(private EntityManagerInterface $em) {}
public function findTickets(array $params, int $start = 0, int $limit = 100): array
{
return $this->buildQuery($params)->select('t')->getQuery()->setFirstResult($start)
->setMaxResults($limit)->getResult();
}
public function countTickets(array $params): int
{
return $this->buildQuery($params)->select('COUNT(t)')->getQuery()->getSingleScalarResult();
}
private function buildQuery(array $params): QueryBuilder
{
$qb = $this->em->createQueryBuilder();
$qb->from(Ticket::class, 't');
// counter for all the loops
$i = 0;
if (array_key_exists('byPerson', $params)) {
$or = $qb->expr()->orX();
foreach ($params['byPerson'] as $person) {
$or->add(
$qb->expr()->exists(sprintf(
'SELECT 1 FROM %s tp_%d WHERE tp_%d.ticket = t AND tp_%d.person = :person_%d AND tp_%d.endDate IS NULL',
PersonHistory::class,
++$i,
$i,
$i,
$i,
$i,
))
);
$qb->setParameter(sprintf('person_%d', $i), $person);
}
$qb->andWhere($or);
}
return $qb;
}
}

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\TicketBundle\Repository;
use Chill\PersonBundle\Entity\Person;
use Chill\TicketBundle\Entity\Ticket;
/**
* Repository to find tickets, taking care of permissions.
*/
interface TicketACLAwareRepositoryInterface
{
/**
* Find tickets.
*
* @param array{byPerson?: list<Person>} $params
*
* @return list<Ticket>
*/
public function findTickets(array $params, int $start = 0, int $limit = 100): array;
/**
* Find tickets.
*
* @param array{byPerson?: list<Person>} $params
*/
public function countTickets(array $params): int;
}

View File

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\TicketBundle\Tests\Repository;
use Chill\PersonBundle\DataFixtures\Helper\RandomPersonHelperTrait;
use Chill\TicketBundle\Repository\TicketACLAwareRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @internal
*
* @coversNothing
*/
class TicketACLAwareRepositoryTest extends KernelTestCase
{
use RandomPersonHelperTrait;
private TicketACLAwareRepository $repository;
private EntityManagerInterface $entityManager;
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
$this->repository = new TicketACLAwareRepository($this->entityManager);
}
public function testFindNoParameters(): void
{
// Test the findTickets method with byPerson parameter
$actual = $this->repository->findTickets([]);
// Only verify that the query executes successfully without checking results
self::assertIsList($actual);
}
public function testFindTicketByPerson(): void
{
$person = $this->getRandomPerson($this->entityManager);
// Test the findTickets method with byPerson parameter
$actual = $this->repository->findTickets(['byPerson' => [$person]]);
// Only verify that the query executes successfully without checking results
self::assertIsList($actual);
}
public function testCountTicketsByPerson(): void
{
$person = $this->getRandomPerson($this->entityManager);
$result = $this->repository->countTickets(['byPerson' => [$person]]);
self::assertIsInt($result);
}
}