|
| 1 | +# Audit trail |
| 2 | + |
| 3 | +## Introduction |
| 4 | +The audit trail is a feature that allows you to track changes to your data. |
| 5 | +It is useful for tracking changes to sensitive data, such as user accounts, or for tracking changes to data that is important for compliance, such as financial records. |
| 6 | + |
| 7 | +## Usage |
| 8 | +The audit trail is enabled by default for all entities. |
| 9 | +For every action the following data is tracked: |
| 10 | +* The date and time of the action |
| 11 | +* The source of the action (e.g. the url of the request, the command that was run, etc.) |
| 12 | +* The entity that was changed |
| 13 | +* The identifier of the entity that was changed |
| 14 | +* The action that was performed |
| 15 | +* The user that performed the action (and the user impersonating them, if applicable) |
| 16 | +* The roles of the user that performed the action |
| 17 | +* The IP address of the user that performed the action |
| 18 | +* The fields that were changed |
| 19 | +* The data that was changed |
| 20 | + |
| 21 | +To track the data of a changed field add the `DisplayAllEntityFieldWithDataInLog` attribute to the class. |
| 22 | +```php |
| 23 | +#[AuditTrail\DisplayAllEntityFieldWithDataInLog] |
| 24 | +#[ORM\Entity] |
| 25 | +class Test |
| 26 | +{ |
| 27 | + public function __construct( |
| 28 | + #[ORM\Column] |
| 29 | + private string $secret, |
| 30 | + #[ORM\Column] |
| 31 | + private string $name, |
| 32 | + #[ORM\Id] |
| 33 | + #[ORM\GeneratedValue] |
| 34 | + #[ORM\Column] |
| 35 | + private ?int $id = null, |
| 36 | + ) { |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +To track the data of a single changed field add `AuditTrailLoggedField` attribute to the property. |
| 42 | +```php |
| 43 | +#[ORM\Entity] |
| 44 | +class Test |
| 45 | +{ |
| 46 | + public function __construct( |
| 47 | + #[ORM\Column] |
| 48 | + private string $secret, |
| 49 | + #[AuditTrail\AuditTrailLoggedField] |
| 50 | + #[ORM\Column] |
| 51 | + private string $name, |
| 52 | + #[ORM\Id] |
| 53 | + #[ORM\GeneratedValue] |
| 54 | + #[ORM\Column] |
| 55 | + private ?int $id = null, |
| 56 | + ) { |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +To identify which entity is being tracked a `AuditTrailIdentifier` attribute can be used. When the attribute is present the value of the property or method will be used. |
| 62 | +If the attribute is not present an educated guess will be made. |
| 63 | +* `__toString` method |
| 64 | +* `getName` method |
| 65 | +* `getTitle` method |
| 66 | +* `getId` method |
| 67 | +* `getUuid` method |
| 68 | + |
| 69 | +```php |
| 70 | +#[ORM\Entity] |
| 71 | +class Test |
| 72 | +{ |
| 73 | + #[AuditTrail\AuditTrailIdentifier] |
| 74 | + public function displayName(): string |
| 75 | + { |
| 76 | + return $this->displayName; |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +You can hide secure data from the audit trail by adding the `AuditTrailSensitiveData` attribute to the property. |
| 82 | +This will transform the data to `****` in the audit trail. |
| 83 | +```php |
| 84 | +#[AuditTrail\AuditTrailDisplayData] |
| 85 | +#[ORM\Entity] |
| 86 | +class Test |
| 87 | +{ |
| 88 | + public function __construct( |
| 89 | + #[AuditTrail\AuditTrailSensitiveData] |
| 90 | + #[ORM\Column] |
| 91 | + private string $secret, |
| 92 | + #[ORM\Column] |
| 93 | + private string $name, |
| 94 | + #[ORM\Id] |
| 95 | + #[ORM\GeneratedValue] |
| 96 | + #[ORM\Column] |
| 97 | + private ?int $id = null, |
| 98 | + ) { |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +### Manually tracking changes |
| 104 | + |
| 105 | +You can manually track changes by using the `AuditLogger` service. |
| 106 | +```php |
| 107 | +class TestController extends AbstractController |
| 108 | +{ |
| 109 | + #[Route('/test', name: 'test')] |
| 110 | + public function __invoke( |
| 111 | + AuditLogger $auditLogger, |
| 112 | + ): ResponseAlias { |
| 113 | + |
| 114 | + $auditLogger->log( |
| 115 | + data: ['test' => 'test'], |
| 116 | + ); |
| 117 | + |
| 118 | + return $this->render( |
| 119 | + 'test/index.html.twig', |
| 120 | + [] |
| 121 | + ); |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
0 commit comments