Fix AuditCustom event not respecting audit.enabled and audit.console#1048
Fix AuditCustom event not respecting audit.enabled and audit.console#1048rrodrigofranco wants to merge 3 commits intoowen-it:masterfrom
Conversation
|
This is interesting, I wonder if there is actually an issue here laravel-auditing/src/Auditable.php Lines 256 to 267 in c516bdf @erikn69 presumably this needs to check |
|
@willpower232 Good point. The two flags ( For regular auditing, the config check happens at boot time in bootAuditable(), the observer simply isn't registered if auditing is disabled, so For custom events there was no equivalent gate, which is exactly the bug this PR fixes, by checking Adding |
…tingEnabled to the interface
|
|
||
| $auditCountBefore = Audit::where('auditable_type', Article::class)->count(); | ||
|
|
||
| Event::dispatch(new AuditCustom($article)); |
There was a problem hiding this comment.
presumably this needs to check
self::isAuditingEnabled()because the other two disabled flags aren't related to the config?
As I recall, that method was only for boot; it shouldn't be used at runtime. For runtime, there are $auditingDisabled and $auditingGloballyDisabled.
I can see the problem, but it would also happen in regular audits, since the event is being triggered manually.
Event::dispatch(new \OwenIt\Auditing\Events\DispatchAudit($article));I think it needs a different approach, this has never been tested and apparently has always been present
There was a problem hiding this comment.
@erikn69 you're right, the same issue exists for DispatchAudit when dispatched manually. The root cause is that readyForAuditing() doesn't check the config, it only checks the runtime flags.
Would the preferred fix be to add the config check inside readyForAuditing() itself? Something like:
public function readyForAuditing(): bool
{
if (static::$auditingDisabled || Models\Audit::$auditingGloballyDisabled) {
return false;
}
if (App::runningInConsole() && ! Config::get('audit.console', false)) {
return false;
}
if (! Config::get('audit.enabled', true)) {
return false;
}
// ...
}That would fix both AuditCustom and DispatchAudit in a single place, rather than patching each listener individually.
There was a problem hiding this comment.
As I recall, that method was only for boot; it shouldn't be used at runtime
Try #1050
Fixes #1016
The
RecordCustomAuditlistener was not checking theaudit.enabledand
audit.consoleconfig values before processing custom audit events,causing audits to be recorded even when auditing was explicitly disabled.
Changes
isAuditingEnabled()to theContracts\AuditableinterfaceRecordCustomAudit::handle()to return earlywhen auditing is disabled
audit.enabled = falseand console contextwithout
audit.console = true