228 lines
9.9 KiB
PHP
228 lines
9.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\User;
|
|
use App\Models\Verifikator\Periode;
|
|
use App\Models\Verifikator\Team;
|
|
use App\Models\Verifikator\View\Pegawai;
|
|
use App\Services\DawaiService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
use Throwable;
|
|
|
|
class OauthController extends Controller
|
|
{
|
|
private $dawaiService, $periode;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->dawaiService = new DawaiService();
|
|
$this->periode = Periode::where('status', true)->first()->id_periode;
|
|
}
|
|
|
|
public function google(Request $request)
|
|
{
|
|
session()->flush();
|
|
session()->regenerate();
|
|
// try {
|
|
// if(!$this->recaptchaService->verify($request->input('g-recaptcha-response'))) {
|
|
// return redirect()->back()->withInput()->withErrors(['g-recaptcha-response' => 'Captcha tidak valid']);
|
|
// }
|
|
// } catch (\Throwable $th) {
|
|
// return redirect()->back()->with('error', 'Kode Captcha Salah');
|
|
// }
|
|
return Socialite::driver('google')->redirect();
|
|
}
|
|
|
|
public function callback(Request $request)
|
|
{
|
|
try {
|
|
$allowLogin = false;
|
|
$dawaiService = new DawaiService();
|
|
$account = Socialite::driver('google')->user();
|
|
Log::info('Login Google: ' . $account->email);
|
|
$user = User::where('email', $account->email)
|
|
->where(function ($query) {
|
|
return $query->where('periode', $this->periode)->orWhereNull('periode');
|
|
})
|
|
->first();
|
|
|
|
$nipResponse = $this->dawaiService->listPegawai(email: $account->email);
|
|
$nip = $nipResponse['data'][0]['nip'] ?? null;
|
|
|
|
Log::info('DawaiService response for ' . $account->email . ': NIP = ' . ($nip ?? 'NULL'));
|
|
|
|
if ($nip) {
|
|
$pegawai = Pegawai::where('nip', $nip)->orWhere('nip_siakad', $nip)->first();
|
|
|
|
// Jika pegawai tidak ditemukan di view, cek apakah user sudah pernah terdaftar
|
|
if (!$pegawai) {
|
|
Log::info('Pegawai tidak ditemukan di view untuk NIP: ' . $nip);
|
|
// Cek apakah user dengan email dan NIP ini sudah ada
|
|
$existingUser = User::where('email', $account->email)
|
|
->where(function ($query) use ($nip) {
|
|
$query->where('nip', $nip)->orWhere('username', $nip);
|
|
})
|
|
->where('periode', '>', 386)
|
|
->first();
|
|
|
|
// Jika user sudah terdaftar sebelumnya, izinkan login
|
|
if ($existingUser && $existingUser->teams()->count() > 0) {
|
|
$user = $existingUser;
|
|
$allowLogin = true;
|
|
Log::info('User login dengan data existing (pegawai view tidak ditemukan): ' . $account->email);
|
|
} else {
|
|
// Jika user belum pernah terdaftar dan pegawai tidak ditemukan, return error
|
|
return redirect()->route('login')->with('error', 'Data pegawai tidak ditemukan di sistem. Silakan hubungi administrator.');
|
|
}
|
|
}
|
|
|
|
// Proses normal jika pegawai ditemukan
|
|
if ($pegawai) {
|
|
$verifikatorTriDharma = $dawaiService->isVerifikatorTriDharma($pegawai?->nip);
|
|
$verifikatorPresensi = $dawaiService->isVerifikatorTendik($pegawai?->nip);
|
|
|
|
if ($verifikatorTriDharma || $verifikatorPresensi) {
|
|
$user = User::updateOrCreate(
|
|
[
|
|
'nip' => $pegawai->nip,
|
|
'periode' => $this->periode,
|
|
],
|
|
[
|
|
'username' => $pegawai->nip,
|
|
'name' => $pegawai->nama,
|
|
'email' => $account->email,
|
|
],
|
|
);
|
|
|
|
$unit = $pegawai->unit_kerja_id > 9 ? 0 : $pegawai->unit_kerja_id;
|
|
$team = Team::where('unit_kerja_id', $unit)->first();
|
|
|
|
if (!$team) {
|
|
return redirect()->route('login')->with('error', 'Anda tidak terdaftar di tim manapun');
|
|
}
|
|
|
|
if ($user->teams->where('id', $team->id)->count() === 0) {
|
|
$user->teams()->attach($team->id);
|
|
}
|
|
$teamId = $team->id;
|
|
session(['team_id' => $teamId]);
|
|
setPermissionsTeamId($teamId);
|
|
if ($verifikatorTriDharma) {
|
|
$user->givePermissionTo(['Baca Tri Dharma', 'Verifikasi Tri Dharma', 'Update Bawahan']);
|
|
} else {
|
|
$user->revokePermissionTo(['Baca Tri Dharma', 'Verifikasi Tri Dharma', 'Update Bawahan']);
|
|
}
|
|
|
|
if ($verifikatorPresensi) {
|
|
$user->givePermissionTo(['Baca Presensi']);
|
|
$user->givePermissionTo(['Baca Tri Dharma', 'Verifikasi Tri Dharma']);
|
|
}
|
|
$allowLogin = true;
|
|
}
|
|
|
|
$user = User::where('email', $account->email)
|
|
->where(function ($query) use ($nip) {
|
|
$query->orWhere('nip', $nip)->orWhere('username', $nip);
|
|
})
|
|
->where('periode', '>', 386)
|
|
->first();
|
|
|
|
if ($user?->teams?->count() > 0) {
|
|
$allowLogin = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Jika NIP tidak ditemukan dari Dawai, cek apakah user sudah ada di database
|
|
if (!$nip) {
|
|
Log::info('NIP tidak ditemukan dari DawaiService untuk: ' . $account->email);
|
|
$existingUser = User::where('email', $account->email)
|
|
->where('periode', '>', 386)
|
|
->first();
|
|
|
|
if ($existingUser && $existingUser->teams()->count() > 0) {
|
|
$user = $existingUser;
|
|
$allowLogin = true;
|
|
Log::info('User login dengan data existing (NIP tidak ditemukan dari Dawai): ' . $account->email);
|
|
} else {
|
|
// Jika user tidak ada dan NIP juga tidak ditemukan
|
|
if (!$user) {
|
|
return redirect()->route('login')->with('error', 'Email Anda tidak terdaftar sebagai pegawai. Silakan hubungi bagian kepegawaian.');
|
|
}
|
|
}
|
|
}
|
|
|
|
$user?->load('teams');
|
|
|
|
if ($user?->teams?->count() === 0) {
|
|
Log::info('Anda bukan verifikator - 1: ' . $account->email);
|
|
return redirect()->route('login')->with('error', 'Anda tidak terdaftar dalam tim verifikator manapun. Silakan hubungi administrator.');
|
|
}
|
|
|
|
if (!$allowLogin) {
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!$allowLogin) {
|
|
Log::info('Anda bukan verifikator - 2: ' . $account->email);
|
|
return redirect()->route('login')->with('error', 'Anda tidak memiliki hak akses sebagai verifikator. Silakan hubungi administrator.');
|
|
}
|
|
|
|
DB::table('sessions')->where('user_id', $user->id)->delete();
|
|
if ($user->teams->count() === 1) {
|
|
setPermissionsTeamId($user->teams->first()->id);
|
|
session(['team_id' => $user->teams->first()->id]);
|
|
}
|
|
|
|
// $timRemunerasi = config('backdoor.tim_remunerasi', []);
|
|
// if (!in_array($user->id, $timRemunerasi) && env('LOGIN_BLOCK_USER') == true) {
|
|
// Auth::logout();
|
|
// session()->flush();
|
|
// return redirect()->route('login')->with('error', 'Sistem sedang melakukan rekapitulasi perhitungan remunerasi. Akses sementara dibatasi.');
|
|
// }
|
|
if (!$user?->hasRole('Superuser')) {
|
|
Auth::logout();
|
|
session()->flush();
|
|
return redirect()->route('login')->with('error', 'Sedang dalam proses perhitungan');
|
|
}
|
|
Auth::login($user);
|
|
|
|
// Maintenance mode check
|
|
if (Auth::user()->id != 1) {
|
|
Auth::logout();
|
|
session()->flush();
|
|
// return redirect()->route('login')->with('error', 'Aplikasi sedang dalam Maintenance. Silakan coba beberapa saat lagi.');
|
|
return redirect()->route('login')->with('info', 'Sistem sedang melakukan rekapitulasi perhitungan remunerasi. Akses sementara dibatasi.');
|
|
}
|
|
|
|
session(['change_password_at_next_login' => 0]);
|
|
return redirect()->route('dashboard.index');
|
|
} catch (Throwable $th) {
|
|
Log::info('OAuth Google');
|
|
Log::info($th->getMessage());
|
|
return redirect()->route('login')->with('error', 'Terjadi Kesalahan di Server');
|
|
}
|
|
}
|
|
}
|