2026-10-12 23:47:41 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
use Illuminate\View\View;
|
|
|
|
|
|
|
|
|
|
class LoginController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Tampilkan halaman login sistem.
|
|
|
|
|
*/
|
|
|
|
|
public function showLoginForm(): View|RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
if (Auth::check()) {
|
|
|
|
|
return $this->redirectBasedOnRole(Auth::user());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return view('auth.login');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Proses autentikasi pengguna menggunakan username & password.
|
|
|
|
|
*/
|
|
|
|
|
public function login(Request $request): RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
$credentials = $request->validate([
|
|
|
|
|
'username' => ['required', 'string'],
|
|
|
|
|
'password' => ['required', 'string'],
|
|
|
|
|
], [
|
|
|
|
|
'username.required' => 'Username atau email wajib diisi.',
|
|
|
|
|
'password.required' => 'Password wajib diisi.',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$remember = $request->boolean('remember');
|
2026-10-19 23:21:55 +07:00
|
|
|
if ($remember) {
|
|
|
|
|
Auth::guard('web')->setRememberDuration(43200); // 30 hari (30 * 24 * 60 menit)
|
|
|
|
|
}
|
2026-10-12 23:47:41 +07:00
|
|
|
$loginField = filter_var($credentials['username'], FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
|
|
|
|
|
|
|
|
|
|
if (Auth::attempt([$loginField => $credentials['username'], 'password' => $credentials['password']], $remember)) {
|
|
|
|
|
$request->session()->regenerate();
|
|
|
|
|
|
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
|
$user = Auth::user();
|
|
|
|
|
|
2026-11-03 12:03:16 +07:00
|
|
|
// 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');
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 23:21:55 +07:00
|
|
|
if ($request->session()->has('url.intended')) {
|
|
|
|
|
return redirect()->intended()
|
|
|
|
|
->with('success', "Selamat datang kembali, {$user->name}!");
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-12 23:47:41 +07:00
|
|
|
return $this->redirectBasedOnRole($user)
|
|
|
|
|
->with('success', "Selamat datang kembali, {$user->name}!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return back()->withErrors([
|
|
|
|
|
'username' => 'Username atau password yang Anda masukkan salah.',
|
|
|
|
|
])->onlyInput('username');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Logout pengguna dari sistem.
|
|
|
|
|
*/
|
|
|
|
|
public function logout(Request $request): RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
Auth::logout();
|
|
|
|
|
|
|
|
|
|
$request->session()->invalidate();
|
|
|
|
|
$request->session()->regenerateToken();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('login')->with('success', 'Anda telah berhasil logout.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-20 14:41:57 +07:00
|
|
|
* Redirect cerdas berdasarkan wewenang hak akses (permissions) dan role pengguna setelah login.
|
2026-10-12 23:47:41 +07:00
|
|
|
*/
|
|
|
|
|
private function redirectBasedOnRole($user): RedirectResponse
|
|
|
|
|
{
|
2026-10-15 15:03:24 +07:00
|
|
|
if ($user->isSuperAdmin()) {
|
|
|
|
|
return redirect()->route('admin.dashboard');
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 14:41:57 +07:00
|
|
|
// 1. Cek Wewenang Super Admin / Manajemen RBAC
|
|
|
|
|
if ($user->can('admin.dashboard') || $user->can('roles.view') || $user->can('users.view')) {
|
|
|
|
|
return redirect()->route('admin.dashboard');
|
2026-10-19 23:21:55 +07:00
|
|
|
}
|
|
|
|
|
|
2026-10-20 14:41:57 +07:00
|
|
|
// 2. Cek Wewenang QMA
|
|
|
|
|
if ($user->can('qma.vehicles.view') || $user->isQmaAdmin()) {
|
|
|
|
|
return redirect()->route('qma.dashboard');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Cek Wewenang HSE
|
|
|
|
|
if ($user->can('hse.employees.view') || $user->isHseAdmin()) {
|
|
|
|
|
return redirect()->route('hse.dashboard');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. Cek Wewenang Private Guard (PG Admin)
|
|
|
|
|
if ($user->can('pg.guards.view') || $user->isPgAdmin()) {
|
|
|
|
|
return redirect()->route('pg.dashboard');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. Cek Wewenang Pos Gerbang Satpam
|
|
|
|
|
if ($user->can('gate.inspect') || $user->isGateOfficer()) {
|
|
|
|
|
return redirect()->route('pg.gate.inspection');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 6. Cek Wewenang Operasional Karyawan (Termasuk Role Baru Custom seperti Sukon, Vendor, Operator)
|
|
|
|
|
if ($user->can('employee.dashboard')) {
|
|
|
|
|
return redirect()->route('employee.dashboard');
|
|
|
|
|
}
|
|
|
|
|
if ($user->can('employee.p2h')) {
|
|
|
|
|
return redirect()->route('p2h.index');
|
|
|
|
|
}
|
|
|
|
|
if ($user->can('employee.timesheet')) {
|
|
|
|
|
return redirect()->route('timesheet.index');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->route('profile.index');
|
2026-10-12 23:47:41 +07:00
|
|
|
}
|
|
|
|
|
}
|