Skip to content

Commit

Permalink
Add config to allow records array values (#915)
Browse files Browse the repository at this point in the history
* Add config to allow records array values

* test: test config allowed_array_values
  • Loading branch information
rizkypujiraharja authored Apr 29, 2024
1 parent 2be6a9c commit e0cc534
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 11 deletions.
13 changes: 13 additions & 0 deletions config/audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@
'retrieved'
],

/*
|--------------------------------------------------------------------------
| Allowed Array Values
|--------------------------------------------------------------------------
|
| Should the array values be audited?
|
| By default, array values are not allowed. This is to prevent performance
| issues when storing large amounts of data. You can override this by
| setting allow_array_values to true.
*/
'allowed_array_values' => false,

/*
|--------------------------------------------------------------------------
| Audit Timestamps
Expand Down
18 changes: 9 additions & 9 deletions src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected function resolveAuditExclusions()
foreach ($attributes as $attribute => $value) {
// Apart from null, non scalar values will be excluded
if (
is_array($value) ||
(is_array($value) && !Config::get('audit.allowed_array_values', false)) ||
(is_object($value) &&
!method_exists($value, '__toString') &&
!($value instanceof \UnitEnum))
Expand Down Expand Up @@ -365,8 +365,8 @@ public function transformAudit(array $data): array
*
*/
protected function resolveUser()
{
if (! empty($this->preloadedResolverData['user'] ?? null)) {
{
if (!empty($this->preloadedResolverData['user'] ?? null)) {
return $this->preloadedResolverData['user'];
}

Expand Down Expand Up @@ -417,7 +417,7 @@ public function preloadResolverData()
$this->preloadedResolverData = $this->runResolvers();

$user = $this->resolveUser();
if (!empty ($user)) {
if (!empty($user)) {
$this->preloadedResolverData['user'] = $user;
}

Expand Down Expand Up @@ -509,11 +509,11 @@ public function getAuditEvent()
public function getAuditEvents(): array
{
return $this->auditEvents ?? Config::get('audit.events', [
'created',
'updated',
'deleted',
'restored',
]);
'created',
'updated',
'deleted',
'restored',
]);
}

/**
Expand Down
89 changes: 87 additions & 2 deletions tests/Unit/AuditableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,12 +968,11 @@ public function itWorksOnTimesRestoredCorrectly()
]);

$model = Article::first();

$this->assertEquals($model->published_at, $originalStart);

$model->published_at = new Carbon('2022-01-01 12:30:00');
$model->save();

$audit = $model->audits->last();
$audit->auditable_id = $model->id;

Expand Down Expand Up @@ -1310,4 +1309,90 @@ public static function auditableTransitionTestProvider(): array
],
];
}

/**
* @test
*/
public function itWorksWhenConfigAllowedArrayValueIsTrue()
{
$this->app['config']->set('audit.allowed_array_values', true);

$model = factory(Article::class)->make([
'title' => 'How To Audit Eloquent Models',
'content' => 'First step: install the laravel-auditing package.',
'reviewed' => 1,
'images' => [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
]
]);

$model->setAuditEvent('created');

$auditData = $model->toAudit();

$morphPrefix = config('audit.user.morph_prefix', 'user');
self::Assert()::assertArraySubset([
'old_values' => [],
'new_values' => [
'title' => 'How To Audit Eloquent Models',
'content' => Article::contentMutate('First step: install the laravel-auditing package.'),
'reviewed' => 1,
'images' => [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
],
],
'event' => 'created',
'auditable_id' => null,
'auditable_type' => Article::class,
$morphPrefix . '_id' => null,
$morphPrefix . '_type' => null,
'url' => UrlResolver::resolveCommandLine(),
'ip_address' => '127.0.0.1',
'user_agent' => 'Symfony',
'tags' => null,
], $auditData, true);
}

/**
* @test
*/
public function itWorksWhenConfigAllowedArrayValueIsFalse()
{
$this->app['config']->set('audit.allowed_array_values', false);

$model = factory(Article::class)->make([
'title' => 'How To Audit Eloquent Models',
'content' => 'First step: install the laravel-auditing package.',
'reviewed' => 1,
'images' => [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
]
]);

$model->setAuditEvent('created');

$auditData = $model->toAudit();

$morphPrefix = config('audit.user.morph_prefix', 'user');
self::Assert()::assertArraySubset([
'old_values' => [],
'new_values' => [
'title' => 'How To Audit Eloquent Models',
'content' => Article::contentMutate('First step: install the laravel-auditing package.'),
'reviewed' => 1,
],
'event' => 'created',
'auditable_id' => null,
'auditable_type' => Article::class,
$morphPrefix . '_id' => null,
$morphPrefix . '_type' => null,
'url' => UrlResolver::resolveCommandLine(),
'ip_address' => '127.0.0.1',
'user_agent' => 'Symfony',
'tags' => null,
], $auditData, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function up()
$table->timestamp('published_at')->nullable();
$table->json('config')->nullable();
$table->string('price')->nullable();
$table->json('images')->nullable();
$table->timestamps();
$table->softDeletes();
});
Expand Down

0 comments on commit e0cc534

Please sign in to comment.