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.
This commit is contained in:
2026-02-26 14:15:03 +01:00
parent 88235c0fa2
commit 7d62d33058
3 changed files with 34 additions and 3 deletions

View File

@@ -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',

View File

@@ -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(

View File

@@ -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(),
]);