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
7 changes: 7 additions & 0 deletions config/model-auditlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@
* Precision value used in generating audit log tables
*/
'log_timestamp_precision' => 0,

'allowed_audit_actions' => [
'created',
'updated',
'deleted',
'restored',
],
];
38 changes: 23 additions & 15 deletions src/Observers/AuditLogObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,51 @@ class AuditLogObserver
*/
public function created(Model $model): void
{
$this->getAuditLogModel($model)
->recordChanges(EventType::CREATED, $model);
if ($model->auditEventAllowed($model::AUDIT_EVENT_CREATED)) {
$this->getAuditLogModel($model)
->recordChanges(EventType::CREATED, $model);
}
}

/**
* @param Model $model
*/
public function updated(Model $model): void
{
$this->getAuditLogModel($model)
->recordChanges(EventType::UPDATED, $model);
if ($model->auditEventAllowed($model::AUDIT_EVENT_UPDATED)) {
$this->getAuditLogModel($model)
->recordChanges(EventType::UPDATED, $model);
}
}

/**
* @param Model $model
*/
public function deleted(Model $model): void
{
/*
* If a model is hard deleting, either via a force delete or that model does not implement
* the SoftDeletes trait we should tag it as such so logging doesn't occur down the pipe.
*/
if ((! method_exists($model, 'isForceDeleting') || $model->isForceDeleting())) {
$event = EventType::FORCE_DELETED;
}
if ($model->auditEventAllowed($model::AUDIT_EVENT_DELETED)) {
/*
* If a model is hard deleting, either via a force delete or that model does not implement
* the SoftDeletes trait we should tag it as such so logging doesn't occur down the pipe.
*/
if ((!method_exists($model, 'isForceDeleting') || $model->isForceDeleting())) {
$event = EventType::FORCE_DELETED;
}

$this->getAuditLogModel($model)
->recordChanges($event ?? EventType::DELETED, $model);
$this->getAuditLogModel($model)
->recordChanges($event ?? EventType::DELETED, $model);
}
}

/**
* @param Model $model
*/
public function restored(Model $model): void
{
$this->getAuditLogModel($model)
->recordChanges(EventType::RESTORED, $model);
if ($model->auditEventAllowed($model::AUDIT_EVENT_RESTORED)) {
$this->getAuditLogModel($model)
->recordChanges(EventType::RESTORED, $model);
}
}

/**
Expand Down
28 changes: 28 additions & 0 deletions src/Traits/AuditLoggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

trait AuditLoggable
{
public const AUDIT_EVENT_CREATED = 'created';
public const AUDIT_EVENT_UPDATED = 'updated';
public const AUDIT_EVENT_DELETED = 'deleted';
public const AUDIT_EVENT_RESTORED = 'restored';

/**
* Boots the trait and sets the observer.
*/
Expand Down Expand Up @@ -120,4 +125,27 @@ public function asOf(\DateTime $date) : self

return $subject;
}

/**
* Overridable but allows all events by default
*
* @return array
*/
public function allowedAuditActions() : array
{
return [
self::AUDIT_EVENT_CREATED,
self::AUDIT_EVENT_UPDATED,
self::AUDIT_EVENT_DELETED,
self::AUDIT_EVENT_RESTORED,
];
}

public function auditEventAllowed(string $event) : bool
{
return in_array(
$event,
array_intersect($this->allowedAuditActions(), config('model-auditlog.allowed_audit_actions', []))
);
}
}