first
This commit is contained in:
37
app/Actions/DeleteUserAction.php
Normal file
37
app/Actions/DeleteUserAction.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user