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
21 changes: 15 additions & 6 deletions src/Traits/LogsActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,15 @@ public function attributesToBeLogged(): array

// Determine if unguarded attributes will be logged.
if ($this->shouldLogUnguarded()) {

// Get only attribute names, not intrested in the values here then guarded
// attributes. get only keys than not present in guarded array, because
// we are logging the unguarded attributes and we cant have both!

$attributes = array_merge($attributes, array_diff(array_keys($this->getAttributes()), $this->getGuarded()));
// If globally unguarded, log all attributes
if (static::isUnguarded()) {
$attributes = array_merge($attributes, array_keys($this->getAttributes()));
} else {
// Get only attribute names, not interested in the values here then guarded
// attributes. get only keys than not present in guarded array, because
// we are logging the unguarded attributes and we can't have both!
$attributes = array_merge($attributes, array_diff(array_keys($this->getAttributes()), $this->getGuarded()));
}
}

if (! empty($this->activitylogOptions->logAttributes)) {
Expand Down Expand Up @@ -247,6 +250,12 @@ public function shouldLogUnguarded(): bool
return false;
}

// If the model is globally unguarded via Model::unguard(),
// all attributes should be considered unguarded.
if (static::isUnguarded()) {
return true;
}

// This case means all of the attributes are guarded
// so we'll not have any unguarded anyway.
if (in_array('*', $this->getGuarded())) {
Expand Down
42 changes: 42 additions & 0 deletions tests/DetectsChangesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Carbon\Carbon;
use Carbon\CarbonInterval;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;
use Spatie\Activitylog\Contracts\LoggablePipe;
Expand Down Expand Up @@ -1300,6 +1301,47 @@ public function getActivitylogOptions(): LogOptions
$this->assertEquals([], $this->getLastActivity()->changes()->toArray());
});

it('can log unguarded attributes when Model::unguard() is called globally', function () {
// Model without explicit $fillable or $guarded
$articleClass = new class() extends Article {
use LogsActivity;

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logUnguarded()
->logExcept(['id', 'created_at', 'updated_at', 'deleted_at']);
}
};

// Globally unguard all models
Model::unguard();

try {
$article = new $articleClass();
$article->name = 'my name';
$article->text = 'my text';
$article->save();

$expectedChanges = [
'attributes' => [
'name' => 'my name',
'text' => 'my text',
'user_id' => null,
'json' => null,
'price' => null,
'interval' => null,
'status' => null,
],
];

$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
} finally {
// Re-guard models
Model::reguard();
}
});

it('can use hidden as loggable attributes', function () {
$articleClass = new class() extends Article {
use LogsActivity;
Expand Down