first
This commit is contained in:
24
app/Actions/ActivateUserAction.php
Normal file
24
app/Actions/ActivateUserAction.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\AuditLogService;
|
||||
|
||||
class ActivateUserAction
|
||||
{
|
||||
public function __construct(private readonly AuditLogService $audit) {}
|
||||
|
||||
public function execute(User $user): void
|
||||
{
|
||||
$user->update(['is_active' => true]);
|
||||
|
||||
$this->audit->log('user_reactivated', [
|
||||
'subject_type' => User::class,
|
||||
'subject_id' => $user->id,
|
||||
'target_user_id' => $user->id,
|
||||
'old_values' => ['is_active' => false],
|
||||
'new_values' => ['is_active' => true],
|
||||
]);
|
||||
}
|
||||
}
|
||||
30
app/Actions/ChangeUserEmailAction.php
Normal file
30
app/Actions/ChangeUserEmailAction.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\AuditLogService;
|
||||
|
||||
class ChangeUserEmailAction
|
||||
{
|
||||
public function __construct(private readonly AuditLogService $audit) {}
|
||||
|
||||
public function execute(User $user, string $newEmail, ?string $justification = null): void
|
||||
{
|
||||
$oldEmail = $user->email;
|
||||
|
||||
$user->update([
|
||||
'email' => $newEmail,
|
||||
'email_verified_at' => null,
|
||||
]);
|
||||
|
||||
$this->audit->log('user_email_changed', [
|
||||
'subject_type' => User::class,
|
||||
'subject_id' => $user->id,
|
||||
'target_user_id' => $user->id,
|
||||
'old_values' => ['email' => $oldEmail],
|
||||
'new_values' => ['email' => $newEmail],
|
||||
'justification' => $justification,
|
||||
]);
|
||||
}
|
||||
}
|
||||
37
app/Actions/CreateUserAction.php
Normal file
37
app/Actions/CreateUserAction.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\AuditLogService;
|
||||
|
||||
class CreateUserAction
|
||||
{
|
||||
public function __construct(private readonly AuditLogService $audit) {}
|
||||
|
||||
public function execute(array $data): User
|
||||
{
|
||||
$user = User::create([
|
||||
'name' => $data['name'],
|
||||
'email' => $data['email'],
|
||||
'password' => $data['password'],
|
||||
'role' => $data['role'] ?? 'user',
|
||||
'department_id' => $data['department_id'] ?? null,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->audit->log('user_created', [
|
||||
'subject_type' => User::class,
|
||||
'subject_id' => $user->id,
|
||||
'target_user_id' => $user->id,
|
||||
'new_values' => [
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'role' => $user->role,
|
||||
'department_id' => $user->department_id,
|
||||
],
|
||||
]);
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
28
app/Actions/DeactivateUserAction.php
Normal file
28
app/Actions/DeactivateUserAction.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\AuditLogService;
|
||||
|
||||
class DeactivateUserAction
|
||||
{
|
||||
public function __construct(private readonly AuditLogService $audit) {}
|
||||
|
||||
public function execute(User $user, ?string $justification = null): void
|
||||
{
|
||||
$user->update(['is_active' => false]);
|
||||
|
||||
// Invalidate all sessions for this user
|
||||
\DB::table('sessions')->where('user_id', $user->id)->delete();
|
||||
|
||||
$this->audit->log('user_deactivated', [
|
||||
'subject_type' => User::class,
|
||||
'subject_id' => $user->id,
|
||||
'target_user_id' => $user->id,
|
||||
'old_values' => ['is_active' => true],
|
||||
'new_values' => ['is_active' => false],
|
||||
'justification' => $justification,
|
||||
]);
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
35
app/Actions/TransferOwnershipAction.php
Normal file
35
app/Actions/TransferOwnershipAction.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions;
|
||||
|
||||
use App\Models\TranscriptionProject;
|
||||
use App\Models\User;
|
||||
use App\Services\AuditLogService;
|
||||
|
||||
class TransferOwnershipAction
|
||||
{
|
||||
public function __construct(private readonly AuditLogService $audit) {}
|
||||
|
||||
public function execute(TranscriptionProject $project, User $newOwner, string $justification): void
|
||||
{
|
||||
$oldOwner = $project->owner;
|
||||
|
||||
$project->update(['owner_user_id' => $newOwner->id]);
|
||||
|
||||
$this->audit->log('project_ownership_transferred', [
|
||||
'subject_type' => TranscriptionProject::class,
|
||||
'subject_id' => $project->id,
|
||||
'project_id' => $project->id,
|
||||
'target_user_id' => $newOwner->id,
|
||||
'old_values' => [
|
||||
'owner_user_id' => $oldOwner?->id,
|
||||
'owner_name' => $oldOwner?->name,
|
||||
],
|
||||
'new_values' => [
|
||||
'owner_user_id' => $newOwner->id,
|
||||
'owner_name' => $newOwner->name,
|
||||
],
|
||||
'justification' => $justification,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user