Files
Coltura/migrations/Version20260420202749.php
matthieu de39fe6a3e feat : add audit log (table, writer, listener, API, admin UI, timeline)
Implemente le journal d'audit append-only sur toutes les mutations Doctrine
des entites portant #[Auditable]. Couvre les 5 tickets de doc/audit-log.md :

1. Table PG audit_log (uuid PK, jsonb changes, index entity/time/performer)
   + AuditLogWriter (DBAL connexion dediee audit, blacklist defense-in-depth
   sur password/plainPassword/token/secret) + RequestIdProvider (UUID v4 par
   requete HTTP principale).
2. Attributs Auditable / AuditIgnore dans Shared/Domain/Attribute/
   + AuditListener (onFlush capture + postFlush ecriture hors transaction ORM,
   pattern swap-and-clear, erreurs loguees jamais propagees). User annote.
3. API Platform read-only /api/audit-logs (permission core.audit_log.view)
   avec filtres entity_type / entity_id / action / performed_by / plage
   performed_at + DbalPaginator implementant PaginatorInterface (hydra:view
   genere automatiquement).
4. Page admin /admin/audit-log : tableau pagine, filtres persistes en query
   params, row expandable (diff + timeline de l'entite), entree sidebar avec
   permission. Composable useAuditLog avec resetAuditLog() auto-enregistre
   sur onAuthSessionCleared.
5. Composant AuditTimeline reutilisable : garde permission, lazy loading,
   dates relatives FR, skeleton loader.

Fix connexe : phpunit.dist.xml forcait APP_ENV=dev via <env> ce qui cablait
framework.test=false et rendait test.service_container indisponible ; le
JWT_PASSPHRASE ne matchait pas non plus les cles dev. Corrige en meme temps
pour debloquer la suite de tests.
2026-04-20 20:51:10 +02:00

64 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Audit log — Ticket 1 : table append-only `audit_log`.
*
* Table non geree par Doctrine ORM (aucune entite associee). Ecriture via
* DBAL uniquement par l'AuditLogWriter pour eviter la recursion du listener
* Doctrine (flush re-entrant). Colonnes en minuscules snake_case comme
* partout dans le projet.
*
* Type natif PostgreSQL `uuid` (16 octets) plutot que varchar(36) : index
* 40% plus petit sur une table append-only a croissance infinie.
*
* Migration placee au namespace racine `DoctrineMigrations` a cause du bug
* de tri FQCN alphabetique de Doctrine Migrations 3.x documente dans
* CLAUDE.md.
*/
final class Version20260420202749 extends AbstractMigration
{
public function getDescription(): string
{
return 'Audit log : creation de la table append-only audit_log + index.';
}
public function up(Schema $schema): void
{
$this->addSql(<<<'SQL'
CREATE TABLE audit_log (
id uuid NOT NULL,
entity_type VARCHAR(100) NOT NULL,
entity_id VARCHAR(64) NOT NULL,
action VARCHAR(10) NOT NULL,
changes JSONB NOT NULL DEFAULT '{}'::jsonb,
performed_by VARCHAR(100) NOT NULL,
performed_at TIMESTAMP(0) WITH TIME ZONE NOT NULL,
ip_address VARCHAR(45) DEFAULT NULL,
request_id VARCHAR(36) DEFAULT NULL,
PRIMARY KEY(id)
)
SQL);
// Index pour recherche par entite (detail d'historique d'un objet).
$this->addSql('CREATE INDEX idx_audit_entity_time ON audit_log (entity_type, entity_id, performed_at)');
// Index pour recherche par utilisateur (qui a fait quoi).
$this->addSql('CREATE INDEX idx_audit_performer ON audit_log (performed_by, performed_at)');
// Index pour tri chronologique global (listing pagine DESC).
$this->addSql('CREATE INDEX idx_audit_time ON audit_log (performed_at)');
}
public function down(Schema $schema): void
{
$this->addSql('DROP TABLE audit_log');
}
}