first commit
This commit is contained in:
@@ -0,0 +1,416 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\CaptchaController;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use App\Models\Verifikator\Team;
|
||||
use App\Models\Verifikator\TeamUser;
|
||||
use App\Models\Verifikator\View\Pegawai;
|
||||
use App\Services\DawaiService;
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Throwable;
|
||||
|
||||
class LoginController extends Controller
|
||||
{
|
||||
function index()
|
||||
{
|
||||
if (Auth::check()) {
|
||||
return redirect('dashboard');
|
||||
}
|
||||
|
||||
if (config('app.login_mode') === 2) {
|
||||
return view('auth.login-sso');
|
||||
}
|
||||
|
||||
return view('auth.login');
|
||||
}
|
||||
|
||||
function authenticated(Request $request)
|
||||
{
|
||||
// Validate captcha first
|
||||
$captchaToken = $request->input('captcha_token');
|
||||
|
||||
if (!$captchaToken) {
|
||||
return response()->json([
|
||||
'status' => 400,
|
||||
'message' => 'Captcha tidak valid. Silakan selesaikan verifikasi captcha.'
|
||||
], 400);
|
||||
}
|
||||
|
||||
// Store captcha token in session
|
||||
Session::put('captcha_token', $captchaToken);
|
||||
Session::put('captcha_validated_at', now());
|
||||
|
||||
$username = $request->username;
|
||||
$password = $request->password;
|
||||
|
||||
$allowLogin = false;
|
||||
$dawaiService = new DawaiService();
|
||||
$periode = 389;
|
||||
$pegawai = Pegawai::with('jabatan.detail')->where('nip', $username)->orWhere('nip_siakad', $username)->first();
|
||||
$user = User::with('teams')
|
||||
->where('username', $username)
|
||||
->where(function ($query) use ($periode) {
|
||||
$query->where('periode', $periode)->orWhereNull('periode');
|
||||
})
|
||||
->first();
|
||||
|
||||
// Get user with periode filter
|
||||
$user = User::where('periode', $periode)
|
||||
->where(function ($query) use ($username) {
|
||||
$query->where('username', $username)->orWhere('nip', $username);
|
||||
})
|
||||
->with('teams')
|
||||
->first();
|
||||
|
||||
// Check verifikator status
|
||||
$verifikatorTriDharma = $dawaiService->isVerifikatorTriDharma($pegawai?->nip ?? $username);
|
||||
if (!$verifikatorTriDharma && $pegawai?->nip !== $pegawai?->nip_siakad) {
|
||||
$verifikatorTriDharma = $dawaiService->isVerifikatorTriDharma($pegawai?->nip_siakad ?? $username);
|
||||
}
|
||||
$verifikatorPresensi = $dawaiService->isVerifikatorTendik($pegawai?->nip ?? $username);
|
||||
|
||||
// Handle pegawai verification
|
||||
if ($pegawai && ($verifikatorTriDharma || $verifikatorPresensi)) {
|
||||
if (!$user) {
|
||||
$user = self::register($username, $password);
|
||||
}
|
||||
|
||||
$pegawai = Pegawai::where('nip_siakad', $username)
|
||||
->orWhere('nip', $username)
|
||||
->first();
|
||||
|
||||
if (!$pegawai) {
|
||||
Log::error('Pegawai tidak ditemukan :' . $username);
|
||||
return response()->json([
|
||||
'status' => 400,
|
||||
'message' => 'Pegawai tidak ditemukan'
|
||||
], 400);
|
||||
}
|
||||
|
||||
$unit = $pegawai->unit_kerja_id > 9 ? 0 : $pegawai->unit_kerja_id;
|
||||
$team = Team::where('unit_kerja_id', $unit)
|
||||
->where('periode', $periode)
|
||||
->first();
|
||||
|
||||
if (!$team) {
|
||||
return response()->json([
|
||||
'status' => 400,
|
||||
'message' => 'Anda tidak terdaftar di tim manapun'
|
||||
], 400);
|
||||
}
|
||||
|
||||
if ($user->teams->where('id', $team->id)->count() === 0) {
|
||||
$user->teams()->attach($team->id);
|
||||
}
|
||||
$teamId = $team->id;
|
||||
session(['team_id' => $teamId]);
|
||||
setPermissionsTeamId($teamId);
|
||||
$allowLogin = true;
|
||||
}
|
||||
|
||||
// Refresh user data
|
||||
$user?->refresh();
|
||||
|
||||
// Check if user has teams when they are verifikator
|
||||
if (($user?->teams?->count() === 0) && ($verifikatorTriDharma || $verifikatorPresensi)) {
|
||||
$user->load('teams');
|
||||
if (($user?->teams?->count()) === 0) {
|
||||
return response()->json([
|
||||
'status' => 400,
|
||||
'message' => 'Anda Bukan Verifikator'
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
|
||||
// Set allowLogin based on verifikator status or team membership
|
||||
if ($verifikatorPresensi || $verifikatorTriDharma || ($user?->teams?->count() > 0)) {
|
||||
$allowLogin = true;
|
||||
}
|
||||
|
||||
// Additional permission checks if not already allowed
|
||||
if (!$allowLogin && $user) {
|
||||
if (isset($teamId)) {
|
||||
setPermissionsTeamId($teamId);
|
||||
session(['team_id' => $teamId]);
|
||||
$allowLogin = $user->getDirectPermissions()->count() > 1;
|
||||
}
|
||||
|
||||
if (!$allowLogin) {
|
||||
foreach ($user?->teams ?? [] as $team) {
|
||||
setPermissionsTeamId($team->id);
|
||||
session(['team_id' => $team->id]);
|
||||
$allowLogin = $user->getDirectPermissions()->count() > 1;
|
||||
if ($allowLogin) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Final check for allowLogin
|
||||
if (!$allowLogin) {
|
||||
Log::info("Login denied for username: " . $username);
|
||||
return response()->json([
|
||||
'status' => 400,
|
||||
'message' => 'Anda Bukan Verifikator'
|
||||
], 400);
|
||||
}
|
||||
|
||||
// Authenticate user
|
||||
if ($user?->password) {
|
||||
$login = Auth::attempt([
|
||||
'username' => $user->username,
|
||||
'password' => $password,
|
||||
'periode' => 389
|
||||
]);
|
||||
|
||||
if (!$login) {
|
||||
Log::info("Authentication failed for NIP: $username");
|
||||
return response()->json([
|
||||
'status' => 400,
|
||||
'message' => 'NIP atau Password Salah'
|
||||
], 400);
|
||||
}
|
||||
} else {
|
||||
// Register new user if no password exists
|
||||
$user = self::register($username, $password);
|
||||
if (!$user) {
|
||||
return response()->json([
|
||||
'status' => 400,
|
||||
'message' => 'Gagal mendaftarkan user baru'
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
|
||||
// Set permissions based on verifikator status
|
||||
try {
|
||||
if ($verifikatorTriDharma) {
|
||||
$user->givePermissionTo(['Baca Tri Dharma', 'Verifikasi Tri Dharma', 'Update Bawahan', 'Batalkan Pengajuan Tri Dharma']);
|
||||
} else {
|
||||
$user->revokePermissionTo(['Baca Tri Dharma', 'Verifikasi Tri Dharma', 'Update Bawahan', 'Batalkan Pengajuan Tri Dharma']);
|
||||
}
|
||||
|
||||
if ($verifikatorPresensi) {
|
||||
$user->givePermissionTo(['Baca Presensi']);
|
||||
$user->givePermissionTo(['Baca Tri Dharma', 'Verifikasi Tri Dharma', 'Batalkan Pengajuan Tri Dharma']);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Log::error('Permission error: ' . $e->getMessage());
|
||||
return response()->json([
|
||||
'status' => 403,
|
||||
'message' => 'Gagal mengatur permission: ' . $e->getMessage()
|
||||
], 403);
|
||||
}
|
||||
|
||||
// Clear other sessions for this user BEFORE setting new session
|
||||
if ($user->id > 1) {
|
||||
DB::table('sessions')->where('user_id', $user->id)->delete();
|
||||
}
|
||||
|
||||
// Load teams and handle multiple teams
|
||||
$user->load('teams');
|
||||
|
||||
// Handle team session for all users (not just multiple teams)
|
||||
if ($user->teams->count() > 0) {
|
||||
if ($user->teams->count() > 1) {
|
||||
// Get active team from TeamUser table
|
||||
$activeTeamUser = TeamUser::where('user_id', $user->id)
|
||||
->where('active', true)
|
||||
->with('team')
|
||||
->first();
|
||||
|
||||
if ($activeTeamUser) {
|
||||
session(['team_id' => $activeTeamUser->team_id]);
|
||||
setPermissionsTeamId($activeTeamUser->team_id);
|
||||
} else {
|
||||
// If no active team, use the first team
|
||||
$firstTeam = $user->teams->first();
|
||||
if ($firstTeam) {
|
||||
session(['team_id' => $firstTeam->id]);
|
||||
setPermissionsTeamId($firstTeam->id);
|
||||
|
||||
// Set this team as active in TeamUser
|
||||
TeamUser::where('user_id', $user->id)
|
||||
->where('team_id', $firstTeam->id)
|
||||
->update(['active' => true]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// User has only one team
|
||||
$singleTeam = $user->teams->first();
|
||||
session(['team_id' => $singleTeam->id]);
|
||||
setPermissionsTeamId($singleTeam->id);
|
||||
|
||||
// Ensure this team is set as active in TeamUser
|
||||
TeamUser::where('user_id', $user->id)
|
||||
->where('team_id', $singleTeam->id)
|
||||
->update(['active' => true]);
|
||||
}
|
||||
}
|
||||
|
||||
// Login user and redirect
|
||||
Auth::login($user);
|
||||
|
||||
// Clear captcha session after successful login
|
||||
CaptchaController::clear();
|
||||
|
||||
// Maintenance mode check
|
||||
// if (Auth::user()->id != 1) {
|
||||
// Auth::logout();
|
||||
// session()->flush();
|
||||
// return response()->json([
|
||||
// 'status' => 503,
|
||||
// // 'message' => 'Aplikasi sedang dalam Maintenance. Silakan coba beberapa saat lagi.'
|
||||
// 'message' => 'Sistem sedang melakukan rekapitulasi perhitungan remunerasi. Akses sementara dibatasi.'
|
||||
// ], 503);
|
||||
// }
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Selamat datang! Anda akan diarahkan ke dashboard.',
|
||||
'redirect' => url('/dashboard')
|
||||
]);
|
||||
}
|
||||
|
||||
function register(string $username, string $password)
|
||||
{
|
||||
$isDosen = self::isDosen($username, $password);
|
||||
$isTendik = self::isTendik($username, $password);
|
||||
|
||||
if (!($isDosen || $isTendik)) {
|
||||
return response()->json([
|
||||
'status' => 400,
|
||||
'message' => 'NIP atau Password Salah.'
|
||||
], 400);
|
||||
}
|
||||
$pegawai = Pegawai::where('nip', $username)->orWhere('nip_siakad', $username)->first();
|
||||
$user = User::where(function ($query) use ($username) {
|
||||
$query->where('username', $username)->orWhere('nip', $username);
|
||||
})->where('periode', 389)->first();
|
||||
if ($user) {
|
||||
$user->update([
|
||||
'username' => $username,
|
||||
'name' => $pegawai->nama,
|
||||
'nip' => $pegawai->nip,
|
||||
'password' => bcrypt($password),
|
||||
'periode' => 389,
|
||||
]);
|
||||
} else {
|
||||
$user = User::create(
|
||||
[
|
||||
'username' => $username,
|
||||
'name' => $pegawai->nama,
|
||||
'nip' => $pegawai->nip,
|
||||
'password' => bcrypt($password),
|
||||
'periode' => 389,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
if ($user->password === null) $user->update(['password' => bcrypt($password)]);
|
||||
return $user;
|
||||
}
|
||||
|
||||
function isDosen(string $username, string $password)
|
||||
{
|
||||
$url_login_dosen = env('LOGIN_DOSEN_BASE_URI') . 'datasnap/rest/tservermethods1/logindosen/';
|
||||
$url = $url_login_dosen . 'X' . $username . '/X' . rawurlencode($password);
|
||||
try {
|
||||
$client = new Client();
|
||||
$response = $client->get($url);
|
||||
} catch (Throwable $th) {
|
||||
Log::error("Service Siakad: " . $th->getMessage());
|
||||
return response()->json([
|
||||
'status' => 500,
|
||||
'message' => 'Server SIAKAD tidak dapat diakses.'
|
||||
], 500);
|
||||
}
|
||||
$data = json_decode($response->getBody()->getContents())->result[0];
|
||||
if ($data->stat == 'gagal') {
|
||||
return false;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
function isTendik(string $username, string $password)
|
||||
{
|
||||
try {
|
||||
$urlPresensi = env('LOGIN_TENDIK_BASE_URI');
|
||||
$clientPresensi = new Client();
|
||||
$formDataPresensi = ['form_params' => [
|
||||
'nip' => $username,
|
||||
'password' => $password
|
||||
]];
|
||||
$clientPresensi->post($urlPresensi, $formDataPresensi);
|
||||
return true;
|
||||
} catch (Throwable $th) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function isDekan(int $id_jabatan_dtt)
|
||||
{
|
||||
return $id_jabatan_dtt === 3;
|
||||
}
|
||||
|
||||
function changePassword($username, $oldPassword, $newPassword)
|
||||
{
|
||||
$user = User::where('username', $username)->first();
|
||||
|
||||
$passwordCheck = Hash::check($oldPassword, $user->password);
|
||||
if (!$passwordCheck) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$user->update([
|
||||
'password' => bcrypt($newPassword),
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
|
||||
function logout()
|
||||
{
|
||||
$viaSso = config('app.login_mode') === 2 || session('auth_via_sso');
|
||||
|
||||
Auth::logout();
|
||||
session()->flush();
|
||||
|
||||
if ($viaSso) {
|
||||
return view('auth.sso-logout');
|
||||
}
|
||||
|
||||
Session::flash('message-success', 'Anda berhasil logout');
|
||||
return redirect()->to('/?status=logout&message=logout');
|
||||
}
|
||||
|
||||
function loginAsUser(string $id)
|
||||
{
|
||||
$userId = Auth::user()->id;
|
||||
$user = User::where('id', $id)->first();
|
||||
Auth::logout();
|
||||
Auth::login($user);
|
||||
|
||||
// Maintenance mode check
|
||||
// if (Auth::user()->id != 1) {
|
||||
// Auth::logout();
|
||||
// session()->flush();
|
||||
// return redirect()->route('login');
|
||||
// }
|
||||
|
||||
session(['team_id' => null]);
|
||||
if ($user->teams->count() === 1) {
|
||||
session(['team_id' => $user->teams->first()->id]);
|
||||
}
|
||||
session(['change_password_at_next_login' => 0]);
|
||||
session(['login_as_user' => $userId]);
|
||||
return redirect()->route('dashboard.index');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user