38 lines
1008 B
PHP
38 lines
1008 B
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Models\User;
|
|
use App\Services\AuditLogService;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class DeleteUserAction
|
|
{
|
|
public function __construct(private readonly AuditLogService $audit) {}
|
|
|
|
public function execute(User $user, ?string $justification = null): void
|
|
{
|
|
if ($user->hasUsage()) {
|
|
throw ValidationException::withMessages([
|
|
'user' => 'Pengguna ini tidak boleh dipadam kerana telah menggunakan sistem. Gunakan deactivate.',
|
|
]);
|
|
}
|
|
|
|
$userData = [
|
|
'name' => $user->name,
|
|
'email' => $user->email,
|
|
'role' => $user->role,
|
|
];
|
|
|
|
$this->audit->log('user_deleted', [
|
|
'subject_type' => User::class,
|
|
'subject_id' => $user->id,
|
|
'target_user_id' => $user->id,
|
|
'old_values' => $userData,
|
|
'justification' => $justification,
|
|
]);
|
|
|
|
$user->forceDelete();
|
|
}
|
|
}
|