From 9b996c457b689743076697c5c050da6a32f8a561 Mon Sep 17 00:00:00 2001 From: Quentin Schmick Date: Wed, 20 Aug 2025 17:30:27 -0400 Subject: [PATCH] Adding option to turn off audit logs for certain events globally or on a model --- config/model-auditlog.php | 7 ++++++ src/Observers/AuditLogObserver.php | 38 ++++++++++++++++++------------ src/Traits/AuditLoggable.php | 28 ++++++++++++++++++++++ 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/config/model-auditlog.php b/config/model-auditlog.php index 035a1d2..c07f0de 100644 --- a/config/model-auditlog.php +++ b/config/model-auditlog.php @@ -61,4 +61,11 @@ * Precision value used in generating audit log tables */ 'log_timestamp_precision' => 0, + + 'allowed_audit_actions' => [ + 'created', + 'updated', + 'deleted', + 'restored', + ], ]; diff --git a/src/Observers/AuditLogObserver.php b/src/Observers/AuditLogObserver.php index 5574345..37ccd9e 100644 --- a/src/Observers/AuditLogObserver.php +++ b/src/Observers/AuditLogObserver.php @@ -12,8 +12,10 @@ class AuditLogObserver */ public function created(Model $model): void { - $this->getAuditLogModel($model) - ->recordChanges(EventType::CREATED, $model); + if ($model->auditEventAllowed($model::AUDIT_EVENT_CREATED)) { + $this->getAuditLogModel($model) + ->recordChanges(EventType::CREATED, $model); + } } /** @@ -21,8 +23,10 @@ public function created(Model $model): void */ public function updated(Model $model): void { - $this->getAuditLogModel($model) - ->recordChanges(EventType::UPDATED, $model); + if ($model->auditEventAllowed($model::AUDIT_EVENT_UPDATED)) { + $this->getAuditLogModel($model) + ->recordChanges(EventType::UPDATED, $model); + } } /** @@ -30,16 +34,18 @@ public function updated(Model $model): void */ public function deleted(Model $model): void { - /* - * If a model is hard deleting, either via a force delete or that model does not implement - * the SoftDeletes trait we should tag it as such so logging doesn't occur down the pipe. - */ - if ((! method_exists($model, 'isForceDeleting') || $model->isForceDeleting())) { - $event = EventType::FORCE_DELETED; - } + if ($model->auditEventAllowed($model::AUDIT_EVENT_DELETED)) { + /* + * If a model is hard deleting, either via a force delete or that model does not implement + * the SoftDeletes trait we should tag it as such so logging doesn't occur down the pipe. + */ + if ((!method_exists($model, 'isForceDeleting') || $model->isForceDeleting())) { + $event = EventType::FORCE_DELETED; + } - $this->getAuditLogModel($model) - ->recordChanges($event ?? EventType::DELETED, $model); + $this->getAuditLogModel($model) + ->recordChanges($event ?? EventType::DELETED, $model); + } } /** @@ -47,8 +53,10 @@ public function deleted(Model $model): void */ public function restored(Model $model): void { - $this->getAuditLogModel($model) - ->recordChanges(EventType::RESTORED, $model); + if ($model->auditEventAllowed($model::AUDIT_EVENT_RESTORED)) { + $this->getAuditLogModel($model) + ->recordChanges(EventType::RESTORED, $model); + } } /** diff --git a/src/Traits/AuditLoggable.php b/src/Traits/AuditLoggable.php index fe28ae6..1afa722 100644 --- a/src/Traits/AuditLoggable.php +++ b/src/Traits/AuditLoggable.php @@ -7,6 +7,11 @@ trait AuditLoggable { + public const AUDIT_EVENT_CREATED = 'created'; + public const AUDIT_EVENT_UPDATED = 'updated'; + public const AUDIT_EVENT_DELETED = 'deleted'; + public const AUDIT_EVENT_RESTORED = 'restored'; + /** * Boots the trait and sets the observer. */ @@ -120,4 +125,27 @@ public function asOf(\DateTime $date) : self return $subject; } + + /** + * Overridable but allows all events by default + * + * @return array + */ + public function allowedAuditActions() : array + { + return [ + self::AUDIT_EVENT_CREATED, + self::AUDIT_EVENT_UPDATED, + self::AUDIT_EVENT_DELETED, + self::AUDIT_EVENT_RESTORED, + ]; + } + + public function auditEventAllowed(string $event) : bool + { + return in_array( + $event, + array_intersect($this->allowedAuditActions(), config('model-auditlog.allowed_audit_actions', [])) + ); + } }