Skip to content

Commit 3ad0693

Browse files
author
Bjorn Van Acker
authored
Merge pull request #142 from sumocoders/294-automatic-logging-audit-trail
Add entity audit trail log
2 parents eeca137 + 6834ea8 commit 3ad0693

File tree

9 files changed

+682
-2
lines changed

9 files changed

+682
-2
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"minimum-stability": "dev",
2222
"prefer-stable": true,
2323
"require": {
24-
"php": "^8.0",
24+
"php": "^8.1",
2525
"gedmo/doctrine-extensions": "^3",
2626
"friendsofsymfony/jsrouting-bundle": "^3",
2727
"knplabs/knp-menu-bundle": "^3.0",
@@ -34,7 +34,9 @@
3434
"twig/inky-extra": "^3.3",
3535
"twig/cssinliner-extra": "^3.3",
3636
"twig/extra-bundle": "^3.3",
37-
"symfony/ux-autocomplete": "^2.8"
37+
"symfony/ux-autocomplete": "^2.8",
38+
"doctrine/doctrine-bundle": "^2.11",
39+
"doctrine/orm": "^3.0"
3840
},
3941
"require-dev": {
4042
"roave/security-advisories": "dev-master",

docs/development/audit-trail.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace SumoCoders\FrameworkCoreBundle\Attribute\AuditTrail;
4+
5+
use Attribute;
6+
7+
#[Attribute]
8+
class AuditTrailIdentifier
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace SumoCoders\FrameworkCoreBundle\Attribute\AuditTrail;
4+
5+
use Attribute;
6+
7+
#[Attribute]
8+
class AuditTrailLoggedField
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace SumoCoders\FrameworkCoreBundle\Attribute\AuditTrail;
4+
5+
use Attribute;
6+
7+
#[Attribute]
8+
class AuditTrailSensitiveData
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace SumoCoders\FrameworkCoreBundle\Attribute\AuditTrail;
4+
5+
use Attribute;
6+
7+
#[Attribute]
8+
class DisplayAllEntityFieldWithDataInLog
9+
{
10+
}

0 commit comments

Comments
 (0)