Skip to content

Commit 249b2b5

Browse files
authored
Adding option to turn off audit logs for certain events globally or on a model (#8)
1 parent 82afc80 commit 249b2b5

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

config/model-auditlog.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,11 @@
6161
* Precision value used in generating audit log tables
6262
*/
6363
'log_timestamp_precision' => 0,
64+
65+
'allowed_audit_actions' => [
66+
'created',
67+
'updated',
68+
'deleted',
69+
'restored',
70+
],
6471
];

src/Observers/AuditLogObserver.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,51 @@ class AuditLogObserver
1212
*/
1313
public function created(Model $model): void
1414
{
15-
$this->getAuditLogModel($model)
16-
->recordChanges(EventType::CREATED, $model);
15+
if ($model->auditEventAllowed($model::AUDIT_EVENT_CREATED)) {
16+
$this->getAuditLogModel($model)
17+
->recordChanges(EventType::CREATED, $model);
18+
}
1719
}
1820

1921
/**
2022
* @param Model $model
2123
*/
2224
public function updated(Model $model): void
2325
{
24-
$this->getAuditLogModel($model)
25-
->recordChanges(EventType::UPDATED, $model);
26+
if ($model->auditEventAllowed($model::AUDIT_EVENT_UPDATED)) {
27+
$this->getAuditLogModel($model)
28+
->recordChanges(EventType::UPDATED, $model);
29+
}
2630
}
2731

2832
/**
2933
* @param Model $model
3034
*/
3135
public function deleted(Model $model): void
3236
{
33-
/*
34-
* If a model is hard deleting, either via a force delete or that model does not implement
35-
* the SoftDeletes trait we should tag it as such so logging doesn't occur down the pipe.
36-
*/
37-
if ((! method_exists($model, 'isForceDeleting') || $model->isForceDeleting())) {
38-
$event = EventType::FORCE_DELETED;
39-
}
37+
if ($model->auditEventAllowed($model::AUDIT_EVENT_DELETED)) {
38+
/*
39+
* If a model is hard deleting, either via a force delete or that model does not implement
40+
* the SoftDeletes trait we should tag it as such so logging doesn't occur down the pipe.
41+
*/
42+
if ((!method_exists($model, 'isForceDeleting') || $model->isForceDeleting())) {
43+
$event = EventType::FORCE_DELETED;
44+
}
4045

41-
$this->getAuditLogModel($model)
42-
->recordChanges($event ?? EventType::DELETED, $model);
46+
$this->getAuditLogModel($model)
47+
->recordChanges($event ?? EventType::DELETED, $model);
48+
}
4349
}
4450

4551
/**
4652
* @param Model $model
4753
*/
4854
public function restored(Model $model): void
4955
{
50-
$this->getAuditLogModel($model)
51-
->recordChanges(EventType::RESTORED, $model);
56+
if ($model->auditEventAllowed($model::AUDIT_EVENT_RESTORED)) {
57+
$this->getAuditLogModel($model)
58+
->recordChanges(EventType::RESTORED, $model);
59+
}
5260
}
5361

5462
/**

src/Traits/AuditLoggable.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
trait AuditLoggable
99
{
10+
public const AUDIT_EVENT_CREATED = 'created';
11+
public const AUDIT_EVENT_UPDATED = 'updated';
12+
public const AUDIT_EVENT_DELETED = 'deleted';
13+
public const AUDIT_EVENT_RESTORED = 'restored';
14+
1015
/**
1116
* Boots the trait and sets the observer.
1217
*/
@@ -120,4 +125,27 @@ public function asOf(\DateTime $date) : self
120125

121126
return $subject;
122127
}
128+
129+
/**
130+
* Overridable but allows all events by default
131+
*
132+
* @return array
133+
*/
134+
public function allowedAuditActions() : array
135+
{
136+
return [
137+
self::AUDIT_EVENT_CREATED,
138+
self::AUDIT_EVENT_UPDATED,
139+
self::AUDIT_EVENT_DELETED,
140+
self::AUDIT_EVENT_RESTORED,
141+
];
142+
}
143+
144+
public function auditEventAllowed(string $event) : bool
145+
{
146+
return in_array(
147+
$event,
148+
array_intersect($this->allowedAuditActions(), config('model-auditlog.allowed_audit_actions', []))
149+
);
150+
}
123151
}

0 commit comments

Comments
 (0)