Skip to content
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
2 changes: 1 addition & 1 deletion src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ trait Auditable
*/
public static function bootAuditable()
{
if (App::getFacadeRoot() && static::isAuditingEnabled()) {
Comment thread
erikn69 marked this conversation as resolved.
if (App::getFacadeRoot() && Config::get('audit.enabled', true)) {
$registerObserver = fn() => static::observe(AuditableObserver::class);

if (method_exists(static::class, 'whenBooted')) {
Expand Down
4 changes: 4 additions & 0 deletions src/AuditableObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ protected function dispatchAudit(Auditable $model): void
if (! $model->readyForAuditing()) {
return;
}
$modelClass = $model::class;
if (method_exists($modelClass, 'isAuditingEnabled') && ! $modelClass::isAuditingEnabled()) {
Comment thread
erikn69 marked this conversation as resolved.
return;
}

// @phpstan-ignore method.notFound
$model->preloadResolverData();
Expand Down
4 changes: 4 additions & 0 deletions src/Listeners/ProcessDispatchAudit.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function withDelay(DispatchAudit $event): int

public function handle(DispatchAudit $event): void
{
$modelClass = $event->model::class;
if (method_exists($modelClass, 'isAuditingEnabled') && ! $modelClass::isAuditingEnabled()) {
return;
}
Auditor::execute($event->model);
}
}
23 changes: 23 additions & 0 deletions tests/Unit/AuditableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1267,4 +1267,27 @@ public function test_it_works_when_config_allowed_array_value_is_false(): void
'tags' => null,
], $auditData, true);
}

public function test_it_works_when_enabling_after_model_boot(): void
{
$this->app['config']->set('audit.console', false);
Article::clearBootedModels(); // Make sure the model is booted again

$this->assertFalse(Article::getEventDispatcher()->hasListeners(\sprintf('eloquent.created: %s', Article::class)));

$model = Article::factory()->create();// will boot trait

$this->assertCount(0, $model->audits, 'No audits when audit.console false');
$this->assertTrue(Article::getEventDispatcher()->hasListeners(\sprintf('eloquent.created: %s', Article::class)));

$this->app['config']->set('audit.console', true);
$model = Article::factory()->create(); // will not boot again, because booting only happens once

$this->assertCount(1, $model->audits);

Article::disableAuditing();
$model = Article::factory()->create();

$this->assertCount(0, $model->audits);
}
}
Loading