From 7d62d3305866d9dd7f9e6c1e00dbe40067e0562a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 26 Feb 2026 14:15:03 +0100 Subject: [PATCH] Integrate audit functionality across `Person` and `PersonResource` controllers - Added `TriggerAuditInterface` to enable seamless audit event handling. - Implemented audit triggers for create, update, delete, and view actions. - Enhanced controllers to include translatable audit descriptions for better tracking of actions. --- .../Controller/PersonController.php | 10 +++++++-- .../Controller/PersonEditController.php | 5 +++++ .../Controller/PersonResourceController.php | 22 ++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonController.php b/src/Bundle/ChillPersonBundle/Controller/PersonController.php index 2e673daf5..545902e6f 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonController.php @@ -11,7 +11,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Controller; -use Chill\MainBundle\Audit\AuditEvent; +use Chill\MainBundle\Audit\TriggerAuditInterface; use Chill\MainBundle\Entity\AuditTrail; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper; @@ -33,6 +33,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Translation\TranslatableMessage; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Contracts\Translation\TranslatorInterface; use function hash; @@ -48,6 +49,7 @@ final class PersonController extends AbstractController private readonly ConfigPersonAltNamesHelper $configPersonAltNameHelper, private readonly ValidatorInterface $validator, private readonly EntityManagerInterface $em, + private readonly TriggerAuditInterface $triggerAudit, ) {} public function getCFGroup() @@ -78,6 +80,7 @@ final class PersonController extends AbstractController $event = new PrivacyEvent($person); $this->eventDispatcher->dispatch($event, PrivacyEvent::PERSON_PRIVACY_EVENT); + ($this->triggerAudit)(AuditTrail::AUDIT_VIEW, $person, description: new TranslatableMessage('audit.household.list_person_file')); return $this->render( '@ChillPerson/Person/household_history.html.twig', @@ -133,6 +136,8 @@ final class PersonController extends AbstractController ) { $this->em->persist($person); + ($this->triggerAudit)(AuditTrail::AUDIT_CREATE, $person); + $this->em->flush(); $this->lastPostDataReset(); @@ -151,6 +156,7 @@ final class PersonController extends AbstractController $this->em->persist($member); $this->em->persist($household); + ($this->triggerAudit)(AuditTrail::AUDIT_CREATE, $household, [$household]); $this->em->flush(); if ($form->get('createHousehold')->isClicked()) { @@ -206,7 +212,7 @@ final class PersonController extends AbstractController 'You are not allowed to see this person.' ); - $this->eventDispatcher->dispatch(new AuditEvent(AuditTrail::AUDIT_VIEW, $person)); + ($this->triggerAudit)(AuditTrail::AUDIT_VIEW, $person); return $this->render( '@ChillPerson/Person/view.html.twig', diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonEditController.php b/src/Bundle/ChillPersonBundle/Controller/PersonEditController.php index be5b87bc3..743080c17 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonEditController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonEditController.php @@ -12,6 +12,8 @@ declare(strict_types=1); namespace Chill\PersonBundle\Controller; use Chill\CustomFieldsBundle\EntityRepository\CustomFieldsDefaultGroupRepository; +use Chill\MainBundle\Audit\TriggerAuditInterface; +use Chill\MainBundle\Entity\AuditTrail; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Form\PersonType; use Chill\PersonBundle\Security\Authorization\PersonVoter; @@ -38,6 +40,7 @@ final readonly class PersonEditController private EntityManagerInterface $entityManager, private UrlGeneratorInterface $urlGenerator, private Environment $twig, + private TriggerAuditInterface $triggerAudit, ) {} /** @@ -64,6 +67,8 @@ final readonly class PersonEditController } elseif ($form->isSubmitted() && $form->isValid()) { $this->entityManager->flush(); + ($this->triggerAudit)(AuditTrail::AUDIT_UPDATE, $person); + $session->getFlashBag()->add('success', new TranslatableMessage('The person data has been updated')); return new RedirectResponse( diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonResourceController.php b/src/Bundle/ChillPersonBundle/Controller/PersonResourceController.php index 8332d468e..9e21c1493 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonResourceController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonResourceController.php @@ -11,6 +11,8 @@ declare(strict_types=1); namespace Chill\PersonBundle\Controller; +use Chill\MainBundle\Audit\TriggerAuditInterface; +use Chill\MainBundle\Entity\AuditTrail; use Chill\PersonBundle\Entity\Person\PersonResource; use Chill\PersonBundle\Form\PersonResourceType; use Chill\PersonBundle\Repository\PersonRepository; @@ -21,11 +23,18 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Translation\TranslatableMessage; use Symfony\Contracts\Translation\TranslatorInterface; final class PersonResourceController extends AbstractController { - public function __construct(private readonly PersonResourceRepository $personResourceRepository, private readonly PersonRepository $personRepository, private readonly EntityManagerInterface $em, private readonly TranslatorInterface $translator) {} + public function __construct( + private readonly PersonResourceRepository $personResourceRepository, + private readonly PersonRepository $personRepository, + private readonly EntityManagerInterface $em, + private readonly TranslatorInterface $translator, + private readonly TriggerAuditInterface $triggerAudit, + ) {} #[\Symfony\Component\Routing\Annotation\Route(path: '/{_locale}/person/{person_id}/resources/{resource_id}/delete', name: 'chill_person_resource_delete')] public function deleteAction(Request $request, mixed $person_id, mixed $resource_id): Response @@ -51,6 +60,7 @@ final class PersonResourceController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + ($this->triggerAudit)(AuditTrail::AUDIT_DELETE, $resource); $this->em->remove($resource); $this->em->flush(); @@ -90,6 +100,7 @@ final class PersonResourceController extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $this->em->persist($resource); + ($this->triggerAudit)(AuditTrail::AUDIT_UPDATE, $resource); $this->em->flush(); return $this->redirectToRoute('chill_person_resource_list', [ @@ -117,6 +128,13 @@ final class PersonResourceController extends AbstractController $personResources = []; $personResources = $this->personResourceRepository->findBy(['personOwner' => $personOwner->getId()]); + $this->triggerAudit->triggerAudit( + AuditTrail::AUDIT_VIEW, + $personOwner, + $personResources, + new TranslatableMessage('audit.person_resource.list') + ); + return $this->render( '@ChillPerson/PersonResource/list.html.twig', [ @@ -143,6 +161,8 @@ final class PersonResourceController extends AbstractController $this->em->persist($personResource); $this->em->flush(); + ($this->triggerAudit)(AuditTrail::AUDIT_CREATE, $personResource); + return $this->redirectToRoute('chill_person_resource_list', [ 'person_id' => $personOwner->getId(), ]);