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