Penambahan Sementara Pengajuan Absen
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Models\Verifikator\PengajuanPresensi;
|
||||
use App\Models\Verifikator\View\Pegawai;
|
||||
use App\Services\DawaiService;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class BawahanHelper
|
||||
{
|
||||
/**
|
||||
* TTL cache daftar bawahan (menit).
|
||||
*/
|
||||
private const CACHE_TTL = 30;
|
||||
|
||||
/**
|
||||
* Daftar NIP bawahan pegawai (dari DAWAI) untuk NIP atasan tertentu.
|
||||
* Default: user yang sedang login. Hasil di-cache 30 menit.
|
||||
*/
|
||||
public static function nips(?string $userNip = null): array
|
||||
{
|
||||
$userNip = $userNip ?: (Auth::user()->nip ?? null);
|
||||
|
||||
if (! $userNip) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Cache::remember("bawahan_nips:{$userNip}", now()->addMinutes(self::CACHE_TTL), function () use ($userNip) {
|
||||
$pegawai = Pegawai::withoutGlobalScopes()
|
||||
->where('nip', $userNip)
|
||||
->orWhere('nip_siakad', $userNip)
|
||||
->first();
|
||||
|
||||
if (! $pegawai) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$dawai = app(DawaiService::class);
|
||||
|
||||
$bawahan = $dawai->getListBawahan($pegawai->nip)['data']['bawahan'] ?? [];
|
||||
|
||||
if (empty($bawahan) && $pegawai->nip !== $pegawai->nip_siakad && $pegawai->nip_siakad) {
|
||||
$bawahan = $dawai->getListBawahan($pegawai->nip_siakad)['data']['bawahan'] ?? [];
|
||||
}
|
||||
|
||||
return array_values(array_unique(array_filter(array_column($bawahan, 'nip'))));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Apakah user (default: login) punya minimal satu bawahan pegawai.
|
||||
*/
|
||||
public static function has(?string $userNip = null): bool
|
||||
{
|
||||
return count(self::nips($userNip)) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apakah NIP tertentu termasuk bawahan user yang sedang login.
|
||||
*/
|
||||
public static function contains(string $nip): bool
|
||||
{
|
||||
return in_array($nip, self::nips(), true);
|
||||
}
|
||||
|
||||
public static function isSuperuser(): bool
|
||||
{
|
||||
return Auth::check() && Auth::user()->hasRole('Superuser');
|
||||
}
|
||||
|
||||
/**
|
||||
* Pastikan user boleh mengakses data pegawai dengan NIP tsb (Superuser selalu boleh).
|
||||
*/
|
||||
public static function authorizeNip(?string $nip): void
|
||||
{
|
||||
abort_unless(self::isSuperuser() || ($nip && self::contains($nip)), 403, 'Pegawai ini bukan bawahan Anda.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Pastikan semua pengajuan (by id) milik bawahan user (Superuser selalu boleh).
|
||||
*
|
||||
* @param array<int|string>|int|string $ids
|
||||
*/
|
||||
public static function authorizePengajuan($ids): void
|
||||
{
|
||||
if (self::isSuperuser()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$ids = array_filter((array) $ids);
|
||||
$nips = PengajuanPresensi::whereIn('id', $ids)->pluck('nip')->unique();
|
||||
|
||||
abort_unless(
|
||||
$nips->isNotEmpty() && $nips->every(fn ($nip) => self::contains($nip)),
|
||||
403,
|
||||
'Pengajuan ini bukan milik bawahan Anda.'
|
||||
);
|
||||
}
|
||||
|
||||
public static function forget(?string $userNip = null): void
|
||||
{
|
||||
$userNip = $userNip ?: (Auth::user()->nip ?? null);
|
||||
|
||||
if ($userNip) {
|
||||
Cache::forget("bawahan_nips:{$userNip}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user