- Inject Security service into all 3 audit subscribers to resolve
actor profile from authenticated user (fixes "Par Inconnu" issue)
- Add CustomFieldValue tracking: insertions, updates, and deletions
on custom field values now produce audit log entries on the parent
entity (composant, piece, product) with field name prefix
"customField:{name}"
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
393 lines
13 KiB
PHP
393 lines
13 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\EventSubscriber;
|
|
|
|
use App\Entity\AuditLog;
|
|
use App\Entity\Composant;
|
|
use App\Entity\CustomFieldValue;
|
|
use App\Entity\ModelType;
|
|
use App\Entity\Product;
|
|
use App\Entity\Profile;
|
|
use DateTimeInterface;
|
|
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Doctrine\Common\EventSubscriber;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\ORM\Event\OnFlushEventArgs;
|
|
use Doctrine\ORM\Events;
|
|
use Doctrine\ORM\PersistentCollection;
|
|
use Doctrine\ORM\UnitOfWork;
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
|
use Throwable;
|
|
|
|
use function is_array;
|
|
use function is_object;
|
|
use function is_scalar;
|
|
use function method_exists;
|
|
|
|
#[AsDoctrineListener(event: Events::onFlush)]
|
|
final class ComposantAuditSubscriber implements EventSubscriber
|
|
{
|
|
public function __construct(
|
|
private readonly RequestStack $requestStack,
|
|
private readonly Security $security,
|
|
) {}
|
|
|
|
public function getSubscribedEvents(): array
|
|
{
|
|
return [
|
|
Events::onFlush,
|
|
];
|
|
}
|
|
|
|
public function onFlush(OnFlushEventArgs $args): void
|
|
{
|
|
$em = $args->getObjectManager();
|
|
if (!$em instanceof EntityManagerInterface) {
|
|
return;
|
|
}
|
|
|
|
$uow = $em->getUnitOfWork();
|
|
$actorProfileId = $this->resolveActorProfileId();
|
|
$pendingUpdates = [];
|
|
$pendingSnapshots = [];
|
|
$pendingComponents = [];
|
|
|
|
foreach ($uow->getScheduledEntityInsertions() as $entity) {
|
|
if (!$entity instanceof Composant) {
|
|
continue;
|
|
}
|
|
|
|
$diff = $this->buildDiffFromChangeSet($uow->getEntityChangeSet($entity));
|
|
$snapshot = $this->snapshotComposant($entity);
|
|
$this->persistAuditLog($em, new AuditLog('composant', (string) $entity->getId(), 'create', $diff, $snapshot, $actorProfileId));
|
|
}
|
|
|
|
foreach ($uow->getScheduledEntityUpdates() as $entity) {
|
|
if (!$entity instanceof Composant) {
|
|
continue;
|
|
}
|
|
|
|
$componentId = (string) $entity->getId();
|
|
if ('' === $componentId) {
|
|
continue;
|
|
}
|
|
|
|
$diff = $this->buildDiffFromChangeSet($uow->getEntityChangeSet($entity));
|
|
if ([] !== $diff) {
|
|
$pendingUpdates[$componentId] = $this->mergeDiffs($pendingUpdates[$componentId] ?? [], $diff);
|
|
$pendingSnapshots[$componentId] = $this->snapshotComposant($entity);
|
|
$pendingComponents[$componentId] = $entity;
|
|
}
|
|
}
|
|
|
|
foreach ($uow->getScheduledEntityDeletions() as $entity) {
|
|
if (!$entity instanceof Composant) {
|
|
continue;
|
|
}
|
|
|
|
$snapshot = $this->snapshotComposant($entity);
|
|
$this->persistAuditLog($em, new AuditLog('composant', (string) $entity->getId(), 'delete', null, $snapshot, $actorProfileId));
|
|
}
|
|
|
|
foreach ($uow->getScheduledCollectionUpdates() as $collection) {
|
|
$this->collectCollectionUpdate($collection, $pendingUpdates, $pendingSnapshots, $pendingComponents);
|
|
}
|
|
foreach ($uow->getScheduledCollectionDeletions() as $collection) {
|
|
$this->collectCollectionUpdate($collection, $pendingUpdates, $pendingSnapshots, $pendingComponents);
|
|
}
|
|
|
|
$this->collectCustomFieldValueChanges($uow, $pendingUpdates, $pendingSnapshots, $pendingComponents);
|
|
|
|
foreach ($pendingUpdates as $componentId => $diff) {
|
|
if ([] === $diff) {
|
|
continue;
|
|
}
|
|
|
|
$component = $pendingComponents[$componentId] ?? null;
|
|
if (!$component instanceof Composant) {
|
|
continue;
|
|
}
|
|
|
|
$snapshot = $pendingSnapshots[$componentId] ?? $this->snapshotComposant($component);
|
|
$this->persistAuditLog($em, new AuditLog('composant', $componentId, 'update', $diff, $snapshot, $actorProfileId));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array<string, array<string, array{from:mixed, to:mixed}>> $pendingUpdates
|
|
* @param array<string, array<string, mixed>> $pendingSnapshots
|
|
* @param array<string, Composant> $pendingComponents
|
|
*/
|
|
private function collectCollectionUpdate(
|
|
object $collection,
|
|
array &$pendingUpdates,
|
|
array &$pendingSnapshots,
|
|
array &$pendingComponents,
|
|
): void {
|
|
if (!$collection instanceof PersistentCollection) {
|
|
return;
|
|
}
|
|
|
|
$owner = $collection->getOwner();
|
|
if (!$owner instanceof Composant) {
|
|
return;
|
|
}
|
|
|
|
$componentId = (string) $owner->getId();
|
|
if ('' === $componentId) {
|
|
return;
|
|
}
|
|
|
|
$mapping = $collection->getMapping();
|
|
$fieldName = $mapping['fieldName'] ?? null;
|
|
if ('constructeurs' !== $fieldName) {
|
|
return;
|
|
}
|
|
|
|
$before = $this->normalizeCollection($collection->getSnapshot());
|
|
$after = $this->normalizeCollection($collection->toArray());
|
|
|
|
if ($before === $after) {
|
|
return;
|
|
}
|
|
|
|
$diff = [
|
|
'constructeurIds' => [
|
|
'from' => $before,
|
|
'to' => $after,
|
|
],
|
|
];
|
|
|
|
$pendingUpdates[$componentId] = $this->mergeDiffs($pendingUpdates[$componentId] ?? [], $diff);
|
|
$pendingSnapshots[$componentId] = $this->snapshotComposant($owner);
|
|
$pendingComponents[$componentId] = $owner;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, array<string, array{from:mixed, to:mixed}>> $pendingUpdates
|
|
* @param array<string, array<string, mixed>> $pendingSnapshots
|
|
* @param array<string, Composant> $pendingComponents
|
|
*/
|
|
private function collectCustomFieldValueChanges(
|
|
UnitOfWork $uow,
|
|
array &$pendingUpdates,
|
|
array &$pendingSnapshots,
|
|
array &$pendingComponents,
|
|
): void {
|
|
foreach ($uow->getScheduledEntityInsertions() as $entity) {
|
|
if ($entity instanceof CustomFieldValue) {
|
|
$this->trackCustomFieldValueChange($entity, null, $entity->getValue(), $pendingUpdates, $pendingSnapshots, $pendingComponents);
|
|
}
|
|
}
|
|
|
|
foreach ($uow->getScheduledEntityUpdates() as $entity) {
|
|
if (!$entity instanceof CustomFieldValue) {
|
|
continue;
|
|
}
|
|
$changeSet = $uow->getEntityChangeSet($entity);
|
|
if (!isset($changeSet['value'])) {
|
|
continue;
|
|
}
|
|
[$oldVal, $newVal] = $changeSet['value'];
|
|
if ($oldVal !== $newVal) {
|
|
$this->trackCustomFieldValueChange($entity, $oldVal, $newVal, $pendingUpdates, $pendingSnapshots, $pendingComponents);
|
|
}
|
|
}
|
|
|
|
foreach ($uow->getScheduledEntityDeletions() as $entity) {
|
|
if ($entity instanceof CustomFieldValue) {
|
|
$this->trackCustomFieldValueChange($entity, $entity->getValue(), null, $pendingUpdates, $pendingSnapshots, $pendingComponents);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array<string, array<string, array{from:mixed, to:mixed}>> $pendingUpdates
|
|
* @param array<string, array<string, mixed>> $pendingSnapshots
|
|
* @param array<string, Composant> $pendingComponents
|
|
*/
|
|
private function trackCustomFieldValueChange(
|
|
CustomFieldValue $cfv,
|
|
mixed $from,
|
|
mixed $to,
|
|
array &$pendingUpdates,
|
|
array &$pendingSnapshots,
|
|
array &$pendingComponents,
|
|
): void {
|
|
$owner = $cfv->getComposant();
|
|
if (!$owner instanceof Composant) {
|
|
return;
|
|
}
|
|
|
|
$ownerId = (string) $owner->getId();
|
|
if ('' === $ownerId) {
|
|
return;
|
|
}
|
|
|
|
$fieldName = 'customField:'.$cfv->getCustomField()->getName();
|
|
$diff = [$fieldName => ['from' => $from, 'to' => $to]];
|
|
|
|
$pendingUpdates[$ownerId] = $this->mergeDiffs($pendingUpdates[$ownerId] ?? [], $diff);
|
|
$pendingSnapshots[$ownerId] = $this->snapshotComposant($owner);
|
|
$pendingComponents[$ownerId] = $owner;
|
|
}
|
|
|
|
private function persistAuditLog(EntityManagerInterface $em, AuditLog $log): void
|
|
{
|
|
$uow = $em->getUnitOfWork();
|
|
$log->initializeAuditLog();
|
|
$em->persist($log);
|
|
|
|
$meta = $em->getClassMetadata(AuditLog::class);
|
|
$uow->computeChangeSet($meta, $log);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, array{0:mixed, 1:mixed}> $changeSet
|
|
*
|
|
* @return array<string, array{from:mixed, to:mixed}>
|
|
*/
|
|
private function buildDiffFromChangeSet(array $changeSet): array
|
|
{
|
|
$diff = [];
|
|
foreach ($changeSet as $field => [$oldValue, $newValue]) {
|
|
if ('updatedAt' === $field || 'createdAt' === $field) {
|
|
continue;
|
|
}
|
|
|
|
$normalizedOld = $this->normalizeValue($oldValue);
|
|
$normalizedNew = $this->normalizeValue($newValue);
|
|
|
|
if ($normalizedOld === $normalizedNew) {
|
|
continue;
|
|
}
|
|
|
|
$diff[$field] = [
|
|
'from' => $normalizedOld,
|
|
'to' => $normalizedNew,
|
|
];
|
|
}
|
|
|
|
return $diff;
|
|
}
|
|
|
|
private function snapshotComposant(Composant $component): array
|
|
{
|
|
return [
|
|
'id' => $component->getId(),
|
|
'name' => $component->getName(),
|
|
'reference' => $component->getReference(),
|
|
'prix' => $component->getPrix(),
|
|
'structure' => $component->getStructure(),
|
|
'typeComposant' => $this->normalizeValue($component->getTypeComposant()),
|
|
'product' => $this->normalizeValue($component->getProduct()),
|
|
'constructeurIds' => $this->normalizeCollection($component->getConstructeurs()),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param iterable<mixed> $items
|
|
*
|
|
* @return list<string>
|
|
*/
|
|
private function normalizeCollection(iterable $items): array
|
|
{
|
|
$ids = [];
|
|
foreach ($items as $item) {
|
|
if (is_object($item) && method_exists($item, 'getId')) {
|
|
$id = $item->getId();
|
|
if (null !== $id && '' !== $id) {
|
|
$ids[] = (string) $id;
|
|
}
|
|
}
|
|
}
|
|
|
|
sort($ids);
|
|
|
|
return array_values(array_unique($ids));
|
|
}
|
|
|
|
private function normalizeValue(mixed $value): mixed
|
|
{
|
|
if (null === $value || is_scalar($value)) {
|
|
return $value;
|
|
}
|
|
|
|
if ($value instanceof DateTimeInterface) {
|
|
return $value->format(DateTimeInterface::ATOM);
|
|
}
|
|
|
|
if ($value instanceof ModelType) {
|
|
return [
|
|
'id' => $value->getId(),
|
|
'name' => $value->getName(),
|
|
'code' => $value->getCode(),
|
|
];
|
|
}
|
|
|
|
if ($value instanceof Product) {
|
|
return [
|
|
'id' => $value->getId(),
|
|
'name' => $value->getName(),
|
|
'reference' => $value->getReference(),
|
|
];
|
|
}
|
|
|
|
if ($value instanceof Collection) {
|
|
return $this->normalizeCollection($value);
|
|
}
|
|
|
|
if (is_object($value) && method_exists($value, 'getId')) {
|
|
return (string) $value->getId();
|
|
}
|
|
|
|
if (is_array($value)) {
|
|
return $value;
|
|
}
|
|
|
|
return (string) $value;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, array{from:mixed, to:mixed}> $base
|
|
* @param array<string, array{from:mixed, to:mixed}> $extra
|
|
*
|
|
* @return array<string, array{from:mixed, to:mixed}>
|
|
*/
|
|
private function mergeDiffs(array $base, array $extra): array
|
|
{
|
|
foreach ($extra as $field => $change) {
|
|
$base[$field] = $change;
|
|
}
|
|
|
|
return $base;
|
|
}
|
|
|
|
private function resolveActorProfileId(): ?string
|
|
{
|
|
try {
|
|
$session = $this->requestStack->getSession();
|
|
if ($session instanceof SessionInterface) {
|
|
$profileId = $session->get('profileId');
|
|
if ($profileId) {
|
|
return (string) $profileId;
|
|
}
|
|
}
|
|
} catch (Throwable) {
|
|
// No session available (CLI context, etc.)
|
|
}
|
|
|
|
$user = $this->security->getUser();
|
|
if ($user instanceof Profile) {
|
|
return $user->getId();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|