226 lines
10 KiB
PHP
226 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Legacy;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Database\Query\JoinClause;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Pengguna (Superuser): akun asli t_account beserta identitas pegawai/mahasiswa dan otoritasnya. Daftar dan detail hanya
|
|
* membaca; yang ditulis hanya status akun (t_account.status: 0 aktif, 1 dibekukan, seperti f_login_er_new) dan otoritas
|
|
* pegawai (t_otoritasuser), keduanya dengan kolom audit cname/cip/uname/tanggal seperti aplikasi lama. Sandi tidak pernah dibaca.
|
|
*/
|
|
final class AccountDirectory
|
|
{
|
|
public const array PER_PAGE = [25, 50, 100];
|
|
|
|
public const array JENIS = [LegacyRole::JENIS_PEGAWAI => 'Pegawai', LegacyRole::JENIS_MAHASISWA => 'Mahasiswa'];
|
|
|
|
private function db(): ConnectionInterface
|
|
{
|
|
return DB::connection('untan');
|
|
}
|
|
|
|
/** @return Collection<int, string> idotoritas => nama */
|
|
public function authorities(): Collection
|
|
{
|
|
return $this->db()->table('t_otoritas')->orderBy('otoritas')->pluck('otoritas', 'idotoritas')
|
|
->map(fn (?string $name): string => trim((string) $name));
|
|
}
|
|
|
|
/**
|
|
* @param array{q?: ?string, jenis?: ?string, status?: ?string, otoritas?: ?string} $filter
|
|
* @return LengthAwarePaginator<int, array<string, mixed>>
|
|
*/
|
|
public function search(array $filter, int $perPage = self::PER_PAGE[0]): LengthAwarePaginator
|
|
{
|
|
$q = mb_strtolower(trim((string) ($filter['q'] ?? '')));
|
|
$jenis = $filter['jenis'] ?? null;
|
|
$status = $filter['status'] ?? null;
|
|
$otoritas = $filter['otoritas'] ?? null;
|
|
|
|
$page = $this->base()
|
|
->when($q !== '', fn (Builder $query) => $query->where(fn (Builder $w) => $w
|
|
->whereRaw('lower(p.nama) like ?', ['%'.$q.'%'])
|
|
->orWhereRaw('lower(m.nama) like ?', ['%'.$q.'%'])
|
|
->orWhereRaw('lower(p.nip) like ?', [$q.'%'])
|
|
->orWhereRaw('lower(m.nim) like ?', [$q.'%'])))
|
|
->when(isset(self::JENIS[(int) $jenis]) && $jenis !== null && $jenis !== '', fn (Builder $query) => $query->where('a.jenis', (int) $jenis))
|
|
->when(in_array($status, ['aktif', 'beku'], true), fn (Builder $query) => $status === 'aktif' ? $query->where('a.status', 0) : $query->where('a.status', '>', 0))
|
|
->when(filled($otoritas), fn (Builder $query) => $query->whereExists(fn (Builder $sub) => $sub
|
|
->selectRaw('1')->from('t_otoritasuser as o')
|
|
->whereColumn('o.iduser', 'a.iduser')->where('o.idotoritas', (int) $otoritas)))
|
|
->orderBy('a.iduser')
|
|
->paginate($perPage);
|
|
|
|
$ids = collect($page->items())->pluck('iduser')->all();
|
|
$granted = $ids === [] ? collect() : $this->granted($ids);
|
|
|
|
return $page->through(fn (object $row): array => $this->present($row, $granted->get((int) $row->iduser, collect())));
|
|
}
|
|
|
|
/**
|
|
* Satu akun lengkap: identitas, otoritas, unit, dan 10 login terakhir. Null bila tidak ada.
|
|
*
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
public function find(int $id): ?array
|
|
{
|
|
$row = $this->base()->where('a.iduser', $id)->addSelect('a.cname', 'a.uname', 'a.tanggal')->first();
|
|
if ($row === null) {
|
|
return null;
|
|
}
|
|
|
|
$granted = $this->granted([$id])->get($id, collect());
|
|
$account = $this->present($row, $granted);
|
|
|
|
$units = (int) $row->jenis === LegacyRole::JENIS_PEGAWAI
|
|
? $this->db()->table('t_unituser as u')
|
|
->join('t_bagian as b', fn (JoinClause $join) => $join->on('b.idbagian', '=', $this->db()->raw('coalesce(u.idprogdi, u.idfak)')))
|
|
->where('u.iduser', $id)->orderBy('u.idunituser')->pluck('b.namabagian')
|
|
->map(fn (?string $name): string => trim((string) $name))->unique()->values()->all()
|
|
: [];
|
|
|
|
$program = (int) $row->jenis === LegacyRole::JENIS_MAHASISWA && $row->idprogdi !== null
|
|
? $this->db()->table('t_bagian')->where('idbagian', $row->idprogdi)->value('namabagian')
|
|
: null;
|
|
|
|
$history = $this->db()->table('t_historilogin')->where('iduser', $id)->orderByDesc('idhistori')->limit(10)
|
|
->get(['logintimestamp', 'logincname', 'logincip'])
|
|
->map(fn (object $h): array => [
|
|
'waktu' => LoginHistory::time($h->logintimestamp),
|
|
'komputer' => LoginHistory::clean($h->logincname),
|
|
'ip' => LoginHistory::clean($h->logincip),
|
|
])->all();
|
|
|
|
return $account + [
|
|
'email' => filled($row->email_akun) ? trim($row->email_akun) : null,
|
|
'unit' => $units,
|
|
'program_studi' => filled($program) ? trim($program) : null,
|
|
'granted' => $granted->all(),
|
|
'diubah_oleh' => filled($row->uname) ? trim($row->uname) : null,
|
|
'diubah_pada' => $row->tanggal === null ? null : Carbon::parse($row->tanggal)->locale('id')->isoFormat('D MMM YYYY, HH.mm'),
|
|
'riwayat' => $history,
|
|
];
|
|
}
|
|
|
|
/** Bekukan (false) atau aktifkan (true) akun: t_account.status 1 atau 0. */
|
|
public function setActive(int $id, bool $active, string $actor, ?string $ip = null): void
|
|
{
|
|
$this->requireAccount($id);
|
|
|
|
$this->db()->table('t_account')->where('iduser', $id)->update([
|
|
'status' => $active ? 0 : 1,
|
|
'cname' => Str::substr((string) config('app.name'), 0, 20),
|
|
'uname' => Str::substr($actor, 0, 15),
|
|
'tanggal' => now(),
|
|
] + $this->ipColumn($ip));
|
|
}
|
|
|
|
/** Beri otoritas pada akun pegawai (mahasiswa otomatis berotoritas Mahasiswa dan tidak punya baris di t_otoritasuser). */
|
|
public function grant(int $id, int $authority, string $actor, ?string $ip = null): void
|
|
{
|
|
$account = $this->requireAccount($id);
|
|
|
|
if ((int) $account->jenis !== LegacyRole::JENIS_PEGAWAI) {
|
|
throw new PortalException('Otoritas hanya dapat diatur untuk akun pegawai.');
|
|
}
|
|
if (! $this->db()->table('t_otoritas')->where('idotoritas', $authority)->exists()) {
|
|
throw new PortalException('Otoritas tidak ditemukan.');
|
|
}
|
|
if ($this->db()->table('t_otoritasuser')->where('iduser', $id)->where('idotoritas', $authority)->exists()) {
|
|
throw new PortalException('Akun ini sudah memiliki otoritas tersebut.');
|
|
}
|
|
|
|
$this->db()->table('t_otoritasuser')->insert([
|
|
'iduser' => $id,
|
|
'idotoritas' => $authority,
|
|
'cname' => Str::substr((string) config('app.name'), 0, 20),
|
|
'uname' => Str::substr($actor, 0, 15),
|
|
'tanggal' => now(),
|
|
] + $this->ipColumn($ip));
|
|
}
|
|
|
|
public function revoke(int $id, int $authority): void
|
|
{
|
|
$this->requireAccount($id);
|
|
|
|
$deleted = $this->db()->table('t_otoritasuser')->where('iduser', $id)->where('idotoritas', $authority)->delete();
|
|
|
|
if ($deleted === 0) {
|
|
throw new PortalException('Akun ini tidak memiliki otoritas tersebut.');
|
|
}
|
|
}
|
|
|
|
private function requireAccount(int $id): object
|
|
{
|
|
return $this->db()->table('t_account')->where('iduser', $id)->first(['iduser', 'jenis', 'status'])
|
|
?? throw new PortalException('Akun tidak ditemukan.');
|
|
}
|
|
|
|
/** cip NOT NULL di DB lama: tanpa IP, nilai bawaan/lama dipertahankan. @return array<string, string> */
|
|
private function ipColumn(?string $ip): array
|
|
{
|
|
return $ip === null ? [] : ['cip' => Str::substr($ip, 0, 20)];
|
|
}
|
|
|
|
/** Kolom akun beserta identitasnya dalam satu query (tanpa kolom sandi). */
|
|
private function base(): Builder
|
|
{
|
|
return $this->db()->table('t_account as a')
|
|
->leftJoin('t_pegawai as p', fn (JoinClause $join) => $join
|
|
->on('p.idpegawai', '=', 'a.identitas')->where('a.jenis', LegacyRole::JENIS_PEGAWAI))
|
|
->leftJoin('t_mahasiswa as m', fn (JoinClause $join) => $join
|
|
->on('m.idmahasiswa', '=', 'a.identitas')->where('a.jenis', LegacyRole::JENIS_MAHASISWA))
|
|
->select(['a.iduser', 'a.jenis', 'a.status', 'a.lastlogin', 'p.nip', 'p.nama as pegawai_nama', 'p.gelardepan', 'p.gelarbelakang', 'p.email as pegawai_email', 'm.nim', 'm.nama as mahasiswa_nama', 'm.email_mhs', 'm.idprogdi'])
|
|
->selectRaw('coalesce(p.email, m.email_mhs) as email_akun')
|
|
->selectRaw('case when a.jenis = ? and exists (select 1 from t_dosen d where d.idpegawai = a.identitas) then 1 else 0 end as is_dosen', [LegacyRole::JENIS_PEGAWAI]);
|
|
}
|
|
|
|
/**
|
|
* Otoritas tiap akun, urut idotoritas.
|
|
*
|
|
* @param list<int> $ids
|
|
* @return Collection<int, Collection<int, string>> iduser => (idotoritas => nama)
|
|
*/
|
|
private function granted(array $ids): Collection
|
|
{
|
|
return $this->db()->table('t_otoritasuser as o')
|
|
->join('t_otoritas as t', 't.idotoritas', '=', 'o.idotoritas')
|
|
->whereIn('o.iduser', $ids)
|
|
->orderBy('o.idotoritas')
|
|
->get(['o.iduser', 'o.idotoritas', 't.otoritas'])
|
|
->groupBy(fn (object $r): int => (int) $r->iduser)
|
|
->map(fn (Collection $rows): Collection => $rows->mapWithKeys(fn (object $r): array => [(int) $r->idotoritas => trim((string) $r->otoritas)]));
|
|
}
|
|
|
|
/**
|
|
* @param Collection<int, string> $granted idotoritas => nama
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function present(object $row, Collection $granted): array
|
|
{
|
|
$jenis = (int) $row->jenis;
|
|
$pegawai = $jenis === LegacyRole::JENIS_PEGAWAI;
|
|
$role = LegacyRole::resolve($jenis, $granted->keys()->map(fn ($key): int => (int) $key)->all(), (bool) $row->is_dosen);
|
|
|
|
return [
|
|
'id' => (int) $row->iduser,
|
|
'nama' => $pegawai ? LegacyFormat::pegawaiName($row->gelardepan, $row->pegawai_nama, $row->gelarbelakang) : trim((string) $row->mahasiswa_nama),
|
|
'username' => $pegawai ? trim((string) $row->nip) : trim((string) $row->nim),
|
|
'jenis' => self::JENIS[$jenis] ?? '-',
|
|
'otoritas' => $pegawai ? ($granted->isEmpty() ? '-' : $granted->implode(', ')) : 'Mahasiswa',
|
|
'peran' => $role?->label() ?? 'Tanpa peran',
|
|
'aktif' => (int) $row->status === 0,
|
|
'status' => (int) $row->status === 0 ? 'Aktif' : 'Dibekukan',
|
|
'terakhir' => $row->lastlogin === null ? null : Carbon::parse($row->lastlogin)->locale('id')->isoFormat('D MMM YYYY, HH.mm'),
|
|
];
|
|
}
|
|
}
|