Files
siakad/app/Http/Controllers/Superuser/PenggunaController.php
T
2026-11-11 18:26:23 +07:00

203 lines
8.6 KiB
PHP

<?php
namespace App\Http\Controllers\Superuser;
use App\Http\Controllers\Prototype\ResourceController;
use App\Models\Legacy\Account;
use App\Support\Legacy\AccountDirectory;
use App\Support\Legacy\PortalException;
use App\Support\Prototype\SuperuserData;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
/**
* Pengguna. Akun DB lama: daftar dan detail asli t_account (dicari, disaring, dipaginasi di server); yang bisa diubah hanya
* status akun dan otoritas pegawai, tanpa menyentuh akun sendiri. Akun demo: CRUD prototipe dari SuperuserData.
*/
class PenggunaController extends ResourceController
{
public function __construct(private readonly AccountDirectory $accounts) {}
public function index(Request $request): View
{
if (! $this->legacy()) {
return parent::index($request);
}
$filter = $request->only(['q', 'jenis', 'status', 'otoritas']);
$per = in_array($request->integer('per'), AccountDirectory::PER_PAGE, true) ? $request->integer('per') : AccountDirectory::PER_PAGE[0];
$rows = $this->accounts->search($filter, $per)->withQueryString();
return view('crud.index', $this->share([
'rows' => $rows,
'columns' => $this->columns(),
'filters' => [
['name' => 'jenis', 'label' => 'Jenis akun', 'options' => AccountDirectory::JENIS],
['name' => 'status', 'label' => 'Status', 'options' => ['aktif' => 'Aktif', 'beku' => 'Dibekukan']],
['name' => 'otoritas', 'label' => 'Otoritas', 'options' => $this->accounts->authorities()->all()],
],
'searchable' => true,
'searchPlaceholder' => 'Cari nama, NIP, atau NIM',
'rowActions' => fn (array $row): array => $this->rowActions($row),
]));
}
public function show(string $record): View
{
abort_unless($this->legacy(), 404);
$account = $this->accounts->find((int) $record);
abort_if($account === null, 404);
$own = (int) request()->user()->getKey() === $account['id'];
return view('superuser.pengguna.show', [
'account' => $account,
'own' => $own,
'authorities' => $this->accounts->authorities()->except(array_keys($account['granted']))->all(),
]);
}
/** Bekukan atau aktifkan akun. */
public function status(Request $request, string $record): RedirectResponse
{
abort_unless($this->legacy(), 404);
$active = $request->validate(['aktif' => ['required', 'boolean']], [], ['aktif' => 'status'])['aktif'];
return $this->change($request, (int) $record, fn (int $id) => $this->accounts->setActive($id, (bool) $active, (string) $request->user()->username, $request->ip()), $active ? 'Akun diaktifkan kembali.' : 'Akun dibekukan.');
}
public function grant(Request $request, string $record): RedirectResponse
{
abort_unless($this->legacy(), 404);
$authority = $request->validate(['otoritas' => ['required', 'integer']], [], ['otoritas' => 'otoritas'])['otoritas'];
return $this->change($request, (int) $record, fn (int $id) => $this->accounts->grant($id, (int) $authority, (string) $request->user()->username, $request->ip()), 'Otoritas ditambahkan.');
}
public function revoke(Request $request, string $record, string $otoritas): RedirectResponse
{
abort_unless($this->legacy(), 404);
return $this->change($request, (int) $record, fn (int $id) => $this->accounts->revoke($id, (int) $otoritas), 'Otoritas dicabut.');
}
/** Akun sendiri tidak boleh dibekukan atau diubah otoritasnya agar Superuser tidak mengunci diri. */
private function change(Request $request, int $id, \Closure $action, string $done): RedirectResponse
{
$back = redirect()->route('superuser.pengguna.show', $id);
if ((int) $request->user()->getKey() === $id) {
return $back->with('error', 'Akun Anda sendiri tidak dapat diubah dari sini.');
}
try {
$action($id);
} catch (PortalException $e) {
return $back->with('error', $e->getMessage());
}
return $back->with('status', $done);
}
private function legacy(): bool
{
return request()->user() instanceof Account;
}
/** Akun DB lama hanya bisa dilihat; pengelolaan lewat halaman detail. */
protected function abilities(): array
{
return $this->legacy()
? ['create' => false, 'show' => true, 'edit' => false, 'delete' => false]
: parent::abilities();
}
protected function rows(): Collection
{
return SuperuserData::pengguna();
}
protected function routeBase(): string
{
return 'superuser.pengguna';
}
protected function keyName(): string
{
return $this->legacy() ? 'id' : 'username';
}
protected function names(): array
{
return [
'singular' => 'Pengguna',
'plural' => 'Pengguna',
'description' => $this->legacy()
? 'Akun asli SIAKAD: pegawai dan mahasiswa beserta otoritasnya. Buka sebuah akun untuk membekukan atau mengaktifkannya dan mengatur otoritas pegawai; kata sandi diatur ulang lewat Atur ulang kata sandi.'
: 'Kelola akun semua peran: tambah, ubah data, aktifkan atau nonaktifkan, dan atur ulang kata sandi.',
];
}
protected function search(): array
{
return ['nama', 'username', 'email'];
}
protected function filters(): array
{
return [
['name' => 'role', 'label' => 'Peran', 'options' => array_combine(SuperuserData::ROLES, SuperuserData::ROLES)],
['name' => 'status', 'label' => 'Status', 'options' => ['Aktif' => 'Aktif', 'Nonaktif' => 'Nonaktif']],
];
}
protected function columns(): array
{
if ($this->legacy()) {
return [
['label' => 'Nama', 'key' => 'nama'],
['label' => 'NIP/NIM', 'key' => 'username'],
['label' => 'Jenis', 'key' => 'jenis'],
['label' => 'Otoritas', 'key' => 'otoritas'],
['label' => 'Peran', 'key' => 'peran', 'badge' => fn (array $r): string => SuperuserData::ROLE_TONE[$r['peran']] ?? 'neutral'],
['label' => 'Status', 'key' => 'status', 'badge' => fn (array $r): string => $r['aktif'] ? 'success' : 'danger'],
['label' => 'Masuk terakhir', 'key' => 'terakhir'],
];
}
return [
['label' => 'Nama', 'key' => 'nama'],
['label' => 'Username', 'key' => 'username'],
['label' => 'Peran', 'key' => 'role', 'badge' => fn (array $r): string => SuperuserData::ROLE_TONE[$r['role']] ?? 'neutral'],
['label' => 'Email', 'key' => 'email'],
['label' => 'Status', 'key' => 'status', 'badge' => fn (array $r): string => $r['aktif'] ? 'success' : 'danger'],
['label' => 'Masuk terakhir', 'key' => 'terakhir'],
];
}
protected function fields(?array $row): array
{
$editing = $row !== null;
return [
['name' => 'nama', 'label' => 'Nama lengkap', 'section' => 'Identitas', 'span' => 'full', 'rules' => ['required', 'string', 'max:120']],
['name' => 'username', 'label' => 'Username', 'section' => 'Identitas', 'hint' => 'Huruf, angka, titik, garis bawah, atau tanda hubung.', 'rules' => ['required', 'string', 'max:40', 'regex:/^[A-Za-z0-9._-]+$/']],
['name' => 'email', 'label' => 'Email', 'type' => 'email', 'section' => 'Identitas', 'rules' => ['required', 'email', 'max:120']],
['name' => 'role', 'label' => 'Peran', 'type' => 'select', 'section' => 'Akun', 'options' => array_combine(SuperuserData::ROLES, SuperuserData::ROLES), 'rules' => ['required', 'in:'.implode(',', SuperuserData::ROLES)]],
['name' => 'password', 'label' => 'Kata sandi', 'type' => 'password', 'section' => 'Akun', 'optional' => $editing, 'hint' => $editing ? 'Kosongkan bila tidak diubah.' : 'Minimal 8 karakter, berisi huruf dan angka.', 'rules' => ['required', 'string', 'min:8']],
['name' => 'aktif', 'label' => 'Akun aktif dan boleh masuk', 'type' => 'checkbox', 'section' => 'Akun', 'default' => true, 'span' => 'full'],
];
}
protected function rowActions(array $row): array
{
$href = $this->legacy()
? route('superuser.reset-password', ['username' => $row['username']])
: route('superuser.pengguna.password', $row['username']);
return [['label' => 'Kata sandi', 'href' => $href, 'icon' => 'key']];
}
}