mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-08 00:02: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(),
|
||||
];
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateAuditsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$connection = config('audit.drivers.database.connection', config('database.default'));
|
||||
$table = config('audit.drivers.database.table', 'audits');
|
||||
|
||||
Schema::connection($connection)->create($table, function (Blueprint $table) {
|
||||
|
||||
$morphPrefix = config('audit.user.morph_prefix', 'user');
|
||||
|
||||
$table->bigIncrements('id');
|
||||
$table->string($morphPrefix.'_type')->nullable();
|
||||
$table->uuid($morphPrefix.'_id')->nullable();
|
||||
$table->string('event');
|
||||
$table->uuidMorphs('auditable');
|
||||
$table->json('old_values')->nullable();
|
||||
$table->json('new_values')->nullable();
|
||||
$table->text('url')->nullable();
|
||||
$table->ipAddress('ip_address')->nullable();
|
||||
$table->string('user_agent', 1023)->nullable();
|
||||
$table->string('tags')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->index([$morphPrefix.'_id', $morphPrefix.'_type']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
$connection = config('audit.drivers.database.connection', config('database.default'));
|
||||
$table = config('audit.drivers.database.table', 'audits');
|
||||
|
||||
Schema::connection($connection)->drop($table);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Enums\Role;
|
||||
use App\Models\Audit;
|
||||
use App\Models\Client;
|
||||
use App\Models\Member;
|
||||
use App\Models\Organization;
|
||||
@@ -153,6 +154,7 @@ class DatabaseSeeder extends Seeder
|
||||
|
||||
private function deleteAll(): void
|
||||
{
|
||||
DB::table((new Audit())->getTable())->delete();
|
||||
DB::table((new TimeEntry())->getTable())->delete();
|
||||
DB::table((new Task())->getTable())->delete();
|
||||
DB::table((new Tag())->getTable())->delete();
|
||||
|
||||
Reference in New Issue
Block a user