From 3a705b5c1d62b293d888bd1ed5012d31f957fcb8 Mon Sep 17 00:00:00 2001 From: Quetzy Garcia Date: Thu, 15 Feb 2018 15:56:24 +0000 Subject: [PATCH 1/2] feat(Auditable): add ability to enable/disable Auditing in a simple way --- src/Auditable.php | 31 ++++++++++++++++++++++++++++++ tests/Functional/AuditingTest.php | 32 +++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/Auditable.php b/src/Auditable.php index 535e452c..cfc14b31 100644 --- a/src/Auditable.php +++ b/src/Auditable.php @@ -40,6 +40,13 @@ trait Auditable */ protected $auditEvent; + /** + * Is auditing disabled? + * + * @var bool + */ + public static $auditingDisabled = false; + /** * Auditable boot logic. * @@ -202,6 +209,10 @@ protected function getRestoredEventAttributes(): array */ public function readyForAuditing(): bool { + if (static::$auditingDisabled) { + return false; + } + return $this->isEventAuditable($this->auditEvent); } @@ -410,6 +421,26 @@ public function getAuditEvents(): array ]); } + /** + * Disable Auditing. + * + * @return void + */ + public static function disableAuditing() + { + static::$auditingDisabled = true; + } + + /** + * Enable Auditing. + * + * @return void + */ + public static function enableAuditing() + { + static::$auditingDisabled = false; + } + /** * Determine whether auditing is enabled. * diff --git a/tests/Functional/AuditingTest.php b/tests/Functional/AuditingTest.php index 089454cc..32a49dcd 100644 --- a/tests/Functional/AuditingTest.php +++ b/tests/Functional/AuditingTest.php @@ -378,4 +378,36 @@ public function itWillCancelTheAuditFromAnEventListener() $this->assertNull(Audit::first()); } + + /** + * @test + */ + public function itDisablesAndEnablesAuditingBackAgain() + { + // Auditing is enabled by default + $this->assertFalse(Article::$auditingDisabled); + + factory(Article::class)->create(); + + $this->assertSame(1, Article::count()); + $this->assertSame(1, Audit::count()); + + // Disable Auditing + Article::disableAuditing(); + $this->assertTrue(Article::$auditingDisabled); + + factory(Article::class)->create(); + + $this->assertSame(2, Article::count()); + $this->assertSame(1, Audit::count()); + + // Re-enable Auditing + Article::enableAuditing(); + $this->assertFalse(Article::$auditingDisabled); + + factory(Article::class)->create(); + + $this->assertSame(2, Audit::count()); + $this->assertSame(3, Article::count()); + } } From 953f175307532cfd61d447229fd3fbb95d5a5e0d Mon Sep 17 00:00:00 2001 From: Quetzy Garcia Date: Thu, 15 Feb 2018 16:23:28 +0000 Subject: [PATCH 2/2] chore(CHANGELOG): add v6.0.1 entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca462f9a..2553a7b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v6.0.1 (2018-02-15) +### Added +- Ability to quickly enable/disable auditing ([#387](https://github.com/owen-it/laravel-auditing/issues/387)) + ## v6.0.0 (2018-02-09) ### Added - Resolver classes & interfaces for _IP Address_, _URL_, _User Agent_ and _User_ ([#369](https://github.com/owen-it/laravel-auditing/issues/369))