diff --git a/src/AuditableTrait.php b/src/AuditableTrait.php index 49cf3aa..5789347 100644 --- a/src/AuditableTrait.php +++ b/src/AuditableTrait.php @@ -9,9 +9,12 @@ /** * @property Model $creator * @property Model $updater + * @property bool $auditing */ trait AuditableTrait { + protected static bool $auditing = true; + /** * Boot the audit trait for a model. */ @@ -20,6 +23,29 @@ public static function bootAuditableTrait(): void static::observe(new AuditableTraitObserver); } + /** + * Disable auditing. + */ + public static function withoutAudits(callable $callback) + { + $previousState = static::$auditing; + static::$auditing = false; + + try { + return $callback(); + } finally { + static::$auditing = $previousState; + } + } + + /** + * Check is auditing is enabled. + */ + public function isAuditable(): bool + { + return static::$auditing; + } + /** * Get user model who created the record. */ diff --git a/src/AuditableTraitObserver.php b/src/AuditableTraitObserver.php index 08613b8..26b670a 100644 --- a/src/AuditableTraitObserver.php +++ b/src/AuditableTraitObserver.php @@ -11,7 +11,7 @@ class AuditableTraitObserver */ public function creating(Model $model): void { - if (method_exists($model, 'getCreatedByColumn')) { + if (method_exists($model, 'getCreatedByColumn') && $model->isAuditable()) { $createdBy = $model->getCreatedByColumn(); if (! $model->$createdBy) { @@ -19,7 +19,7 @@ public function creating(Model $model): void } } - if (method_exists($model, 'getUpdatedByColumn')) { + if (method_exists($model, 'getUpdatedByColumn') && $model->isAuditable()) { $updatedBy = $model->getUpdatedByColumn(); if (! $model->$updatedBy) { @@ -41,7 +41,7 @@ protected function getAuthenticatedUserId(): int|string|null */ public function updating(Model $model): void { - if (method_exists($model, 'getUpdatedByColumn')) { + if (method_exists($model, 'getUpdatedByColumn') && $model->isAuditable()) { $updatedBy = $model->getUpdatedByColumn(); if (! $model->isDirty($updatedBy)) { @@ -55,7 +55,7 @@ public function updating(Model $model): void */ public function saved(Model $model): void { - if (method_exists($model, 'getUpdatedByColumn')) { + if (method_exists($model, 'getUpdatedByColumn') && $model->isAuditable()) { $updatedBy = $model->getUpdatedByColumn(); if ($this->getAuthenticatedUserId() && $this->getAuthenticatedUserId() != $model->$updatedBy && $model->isDirty()) { diff --git a/src/AuditableWithDeletesTraitObserver.php b/src/AuditableWithDeletesTraitObserver.php index 7598a9e..a0bb916 100644 --- a/src/AuditableWithDeletesTraitObserver.php +++ b/src/AuditableWithDeletesTraitObserver.php @@ -11,7 +11,7 @@ class AuditableWithDeletesTraitObserver */ public function deleting(Model $model): void { - if (method_exists($model, 'getDeletedByColumn')) { + if (method_exists($model, 'getDeletedByColumn') && $model->isAuditable()) { $deletedBy = $model->getDeletedByColumn(); $model->$deletedBy = $this->getAuthenticatedUserId(); @@ -32,7 +32,7 @@ protected function getAuthenticatedUserId(): int|string|null */ public function restoring(Model $model): void { - if (method_exists($model, 'getDeletedByColumn')) { + if (method_exists($model, 'getDeletedByColumn') && $model->isAuditable()) { $deletedBy = $model->getDeletedByColumn(); $model->$deletedBy = null; diff --git a/tests/Feature/AuditableModelTest.php b/tests/Feature/AuditableModelTest.php index 89f5c3d..acb7c2e 100644 --- a/tests/Feature/AuditableModelTest.php +++ b/tests/Feature/AuditableModelTest.php @@ -77,3 +77,29 @@ expect($model->created_by)->toBe($anotherUser->id); expect($model->updated_by)->toBe($anotherUser->id); }); + +test('a post can be created without audit', function () { + $user = User::forceCreate([ + 'name' => 'John Doe', + 'email' => 'john@example.com', + ]); + + actingAs($user); + + Post::withoutAudits(function () { + $post = new Post; + $post->title = 'Hello World'; + $post->save(); + + expect($post->created_by)->toBe(null); + expect($post->updated_by)->toBe(null); + expect($post->deleted_by)->toBe(null); + }); + + $post = Post::first(); + $post->title = 'Hello World 2'; + $post->save(); + + expect($post->updated_by)->toBe($user->id); + expect($post->deleted_by)->toBe(null); +});