feat: add user impersonation service
This commit is contained in:
95
database/factories/ImpersonationLogFactory.php
Normal file
95
database/factories/ImpersonationLogFactory.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\ImpersonationLog>
|
||||
*/
|
||||
final class ImpersonationLogFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$startTime = $this->faker->dateTimeBetween('-1 month', 'now');
|
||||
$endTime = $this->faker->optional(0.3)->dateTimeBetween($startTime, '+30 minutes');
|
||||
|
||||
return [
|
||||
'admin_id' => User::factory()->superAdmin(),
|
||||
'target_user_id' => User::factory()->normalUser(),
|
||||
'start_time' => $startTime,
|
||||
'end_time' => $endTime,
|
||||
'ip_address' => $this->faker->ipv4(),
|
||||
'user_agent' => $this->faker->userAgent(),
|
||||
'status' => $endTime ? 'completed' : 'active',
|
||||
'pages_visited' => $this->faker->optional(0.7)->randomElements([
|
||||
'/dashboard',
|
||||
'/profile',
|
||||
'/settings',
|
||||
'/emails',
|
||||
'/reports',
|
||||
], $this->faker->numberBetween(1, 5)),
|
||||
'actions_taken' => $this->faker->optional(0.5)->randomElements([
|
||||
'viewed_profile',
|
||||
'updated_settings',
|
||||
'downloaded_report',
|
||||
'sent_email',
|
||||
'deleted_item',
|
||||
], $this->faker->numberBetween(1, 3)),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the impersonation is currently active.
|
||||
*/
|
||||
public function active(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'start_time' => now()->subMinutes($this->faker->numberBetween(1, 25)),
|
||||
'end_time' => null,
|
||||
'status' => 'active',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the impersonation was completed.
|
||||
*/
|
||||
public function completed(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'end_time' => $this->faker->dateTimeBetween($attributes['start_time'], '+30 minutes'),
|
||||
'status' => 'completed',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the impersonation was force terminated.
|
||||
*/
|
||||
public function forceTerminated(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'end_time' => $this->faker->dateTimeBetween($attributes['start_time'], '+30 minutes'),
|
||||
'status' => 'force_terminated',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the impersonation expired.
|
||||
*/
|
||||
public function expired(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'start_time' => now()->subMinutes(35),
|
||||
'end_time' => now()->subMinutes(5),
|
||||
'status' => 'expired',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,7 @@ class UserFactory extends Factory
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'level' => UserLevel::SUPERADMIN->value,
|
||||
'email' => 'admin@zemail.me',
|
||||
'email' => fake()->unique()->safeEmail(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('impersonation_logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('admin_id')->constrained('users')->onDelete('cascade');
|
||||
$table->foreignId('target_user_id')->constrained('users')->onDelete('cascade');
|
||||
$table->timestamp('start_time');
|
||||
$table->timestamp('end_time')->nullable();
|
||||
$table->string('ip_address', 45)->nullable();
|
||||
$table->text('user_agent')->nullable();
|
||||
$table->enum('status', ['active', 'completed', 'force_terminated', 'expired'])->default('active');
|
||||
$table->json('pages_visited')->nullable();
|
||||
$table->json('actions_taken')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
// Indexes for performance
|
||||
$table->index(['admin_id', 'start_time']);
|
||||
$table->index(['target_user_id', 'start_time']);
|
||||
$table->index(['status', 'start_time']);
|
||||
$table->index(['ip_address']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('impersonation_logs');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user