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)) 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()); + } }