mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-15 11:42:15 +01:00
Add auditing
This commit is contained in:
committed by
Constantin Graf
parent
a01e1d6b0b
commit
156d2ff1a0
71
database/factories/AuditFactory.php
Normal file
71
database/factories/AuditFactory.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Audit;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
|
||||
/**
|
||||
* @extends Factory<Audit>
|
||||
*/
|
||||
class AuditFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$morphPrefix = Config::get('audit.user.morph_prefix', 'user');
|
||||
|
||||
return [
|
||||
$morphPrefix.'_id' => function () {
|
||||
return User::factory()->create()->id;
|
||||
},
|
||||
$morphPrefix.'_type' => function () {
|
||||
return (new User())->getMorphClass();
|
||||
},
|
||||
'event' => 'updated',
|
||||
'auditable_id' => function () {
|
||||
return User::factory()->create()->getKey();
|
||||
},
|
||||
'auditable_type' => function () {
|
||||
return (new User())->getMorphClass();
|
||||
},
|
||||
'old_values' => [],
|
||||
'new_values' => [],
|
||||
'url' => $this->faker->url,
|
||||
'ip_address' => $this->faker->ipv4,
|
||||
'user_agent' => $this->faker->userAgent,
|
||||
'tags' => implode(',', $this->faker->words(4)),
|
||||
];
|
||||
}
|
||||
|
||||
public function auditUser(User $user): self
|
||||
{
|
||||
return $this->state(function (array $attributes) use ($user) {
|
||||
$morphPrefix = Config::get('audit.user.morph_prefix', 'user');
|
||||
|
||||
return [
|
||||
$morphPrefix.'_id' => $user->getKey(),
|
||||
$morphPrefix.'_type' => $user->getMorphClass(),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function auditFor(Model $model): self
|
||||
{
|
||||
return $this->state(function (array $attributes) use ($model) {
|
||||
return [
|
||||
'auditable_id' => $model->getKey(),
|
||||
'auditable_type' => $model->getMorphClass(),
|
||||
];
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user