fix(pg,auth): prevent deactivated security guards from logging in and accessing pg module
This commit is contained in:
@@ -47,6 +47,18 @@ class LoginController extends Controller
|
|||||||
/** @var \App\Models\User $user */
|
/** @var \App\Models\User $user */
|
||||||
$user = Auth::user();
|
$user = Auth::user();
|
||||||
|
|
||||||
|
// Proteksi: Cek apakah akun Petugas Satpam berstatus Nonaktif
|
||||||
|
$guard = $user->securityGuard ?: \App\Models\SecurityGuard::where('user_id', $user->id)->first();
|
||||||
|
if ($guard && ! $guard->is_active && ! $user->isSuperAdmin()) {
|
||||||
|
Auth::logout();
|
||||||
|
$request->session()->invalidate();
|
||||||
|
$request->session()->regenerateToken();
|
||||||
|
|
||||||
|
return back()->withErrors([
|
||||||
|
'username' => 'Akun Petugas Satpam Anda berstatus Nonaktif. Silakan hubungi Administrator Pos Gerbang (PG) untuk mengaktifkan kembali.',
|
||||||
|
])->onlyInput('username');
|
||||||
|
}
|
||||||
|
|
||||||
if ($request->session()->has('url.intended')) {
|
if ($request->session()->has('url.intended')) {
|
||||||
return redirect()->intended()
|
return redirect()->intended()
|
||||||
->with('success', "Selamat datang kembali, {$user->name}!");
|
->with('success', "Selamat datang kembali, {$user->name}!");
|
||||||
|
|||||||
@@ -184,6 +184,10 @@ class GateInspectionController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$guard = SecurityGuard::find($guardId);
|
$guard = SecurityGuard::find($guardId);
|
||||||
|
if ($guard && ! $guard->is_active && ! ($user && $user->isSuperAdmin())) {
|
||||||
|
return back()->with('error', 'Aksi Ditolak: Petugas Satpam berstatus Nonaktif tidak diizinkan memvalidasi aktivitas pos gerbang.');
|
||||||
|
}
|
||||||
|
|
||||||
$guardName = $guard?->name ?? ($user?->name ?? 'Petugas Pos Gerbang');
|
$guardName = $guard?->name ?? ($user?->name ?? 'Petugas Pos Gerbang');
|
||||||
|
|
||||||
DB::transaction(function () use ($guardId, $guardName, $activity, $user) {
|
DB::transaction(function () use ($guardId, $guardName, $activity, $user) {
|
||||||
@@ -265,6 +269,11 @@ class GateInspectionController extends Controller
|
|||||||
'rejection_note.required' => 'Alasan penolakan wajib diisi.',
|
'rejection_note.required' => 'Alasan penolakan wajib diisi.',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$guard = SecurityGuard::find($guardId);
|
||||||
|
if ($guard && ! $guard->is_active && ! ($user && $user->isSuperAdmin())) {
|
||||||
|
return back()->with('error', 'Aksi Ditolak: Petugas Satpam berstatus Nonaktif tidak diizinkan memvalidasi aktivitas pos gerbang.');
|
||||||
|
}
|
||||||
|
|
||||||
$activity = VehicleActivity::with('vehicle')->findOrFail($id);
|
$activity = VehicleActivity::with('vehicle')->findOrFail($id);
|
||||||
|
|
||||||
if ($activity->validation_status !== 'pending') {
|
if ($activity->validation_status !== 'pending') {
|
||||||
|
|||||||
@@ -27,6 +27,12 @@ class CheckRole
|
|||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Proteksi: Cek apakah petugas satpam berstatus Nonaktif
|
||||||
|
$guard = $user->securityGuard ?: \App\Models\SecurityGuard::where('user_id', $user->id)->first();
|
||||||
|
if ($guard && ! $guard->is_active) {
|
||||||
|
abort(403, 'Akses Ditolak: Akun Petugas Satpam Anda berstatus Nonaktif. Silakan hubungi Administrator PG.');
|
||||||
|
}
|
||||||
|
|
||||||
// Cek via kolom role string atau Spatie hasAnyRole
|
// Cek via kolom role string atau Spatie hasAnyRole
|
||||||
if (in_array($userRole, $roles) || (method_exists($user, 'hasAnyRole') && $user->hasAnyRole($roles))) {
|
if (in_array($userRole, $roles) || (method_exists($user, 'hasAnyRole') && $user->hasAnyRole($roles))) {
|
||||||
return $next($request);
|
return $next($request);
|
||||||
|
|||||||
@@ -175,4 +175,30 @@ class SecurityGuardFilterTest extends TestCase
|
|||||||
$this->assertSoftDeleted('users', ['id' => $userId]);
|
$this->assertSoftDeleted('users', ['id' => $userId]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Memastikan petugas Satpam yang dinonaktifkan tidak dapat login dan ditolak middleware.
|
||||||
|
*/
|
||||||
|
public function test_deactivated_guard_cannot_login_and_blocked_by_middleware(): void
|
||||||
|
{
|
||||||
|
$guard = SecurityGuard::with('user')->where('badge_id', 'SEC-001')->first();
|
||||||
|
$this->assertNotNull($guard);
|
||||||
|
$user = $guard->user;
|
||||||
|
$this->assertNotNull($user);
|
||||||
|
|
||||||
|
// 1. Nonaktifkan petugas satpam
|
||||||
|
$guard->update(['is_active' => false]);
|
||||||
|
|
||||||
|
// Coba login -> Harus ditolak
|
||||||
|
$loginResponse = $this->post(route('login'), [
|
||||||
|
'username' => $user->username,
|
||||||
|
'password' => 'password',
|
||||||
|
]);
|
||||||
|
$loginResponse->assertSessionHasErrors('username');
|
||||||
|
$this->assertGuest();
|
||||||
|
|
||||||
|
// 2. Jika dalam sesi aktif tapi di-nonaktifkan -> Middleware tolak dengan 403
|
||||||
|
$middlewareResponse = $this->actingAs($user)->get(route('pg.dashboard'));
|
||||||
|
$middlewareResponse->assertStatus(403);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user