Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] enable/disable auditing #390

Merged
merged 2 commits into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
31 changes: 31 additions & 0 deletions src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ trait Auditable
*/
protected $auditEvent;

/**
* Is auditing disabled?
*
* @var bool
*/
public static $auditingDisabled = false;

/**
* Auditable boot logic.
*
Expand Down Expand Up @@ -202,6 +209,10 @@ protected function getRestoredEventAttributes(): array
*/
public function readyForAuditing(): bool
{
if (static::$auditingDisabled) {
return false;
}

return $this->isEventAuditable($this->auditEvent);
}

Expand Down Expand Up @@ -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.
*
Expand Down
32 changes: 32 additions & 0 deletions tests/Functional/AuditingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}