From 56069deaf55655fb0bf5b61b65114d29559a4436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 27 Feb 2026 17:40:00 +0100 Subject: [PATCH] Document audit triggers in `ApiController` actions - Added examples to demonstrate how to override the `onAfterFlush` method for triggering audits based on HTTP methods. - Updated developer documentation (`audit.md`) with detailed instructions and code snippets. --- docs/source/development/audit.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/source/development/audit.md b/docs/source/development/audit.md index 31c71f7c9..d94ae96bd 100644 --- a/docs/source/development/audit.md +++ b/docs/source/development/audit.md @@ -90,6 +90,36 @@ Examples: ); ``` +### Audits in ApiController + +When using `Chill\MainBundle\CRUD\Controller\ApiController`, the `onAfterFlush` method can be overridden to trigger audits. This method is called after the database operations (flush) are completed. + +Example (based on `AccompanyingCourseWorkApiController`): + +```php +protected function onAfterFlush( + string $action, + Request $request, + string $_format, + $entity, + ConstraintViolationListInterface $errors, + array $more = [] +): ?Response { + if (Request::METHOD_GET === $request->getMethod()) { + $this->triggerAudit->triggerAudit(AuditTrail::AUDIT_VIEW, $entity); + } elseif (Request::METHOD_PUT === $request->getMethod() || Request::METHOD_PATCH === $request->getMethod()) { + $this->triggerAudit->triggerAudit(AuditTrail::AUDIT_UPDATE, $entity); + } elseif (Request::METHOD_POST === $request->getMethod()) { + $this->triggerAudit->triggerAudit(AuditTrail::AUDIT_CREATE, $entity); + } elseif (Request::METHOD_DELETE === $request->getMethod()) { + $this->triggerAudit->triggerAudit(AuditTrail::AUDIT_DELETE, $entity); + } else { + throw new \RuntimeException(sprintf('Unsupported HTTP method "%s" for audit trail.', $request->getMethod())); + } + + return parent::onAfterFlush($action, $request, $_format, $entity, $errors, $more); +} +``` ## Use new entities in Audits