[Superuser] Integrasi Pertama ke Database
This commit is contained in:
@@ -3,11 +3,117 @@
|
||||
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();
|
||||
@@ -20,7 +126,7 @@ class PenggunaController extends ResourceController
|
||||
|
||||
protected function keyName(): string
|
||||
{
|
||||
return 'username';
|
||||
return $this->legacy() ? 'id' : 'username';
|
||||
}
|
||||
|
||||
protected function names(): array
|
||||
@@ -28,7 +134,9 @@ class PenggunaController extends ResourceController
|
||||
return [
|
||||
'singular' => 'Pengguna',
|
||||
'plural' => 'Pengguna',
|
||||
'description' => 'Kelola akun semua peran: tambah, ubah data, aktifkan atau nonaktifkan, dan atur ulang kata sandi.',
|
||||
'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.',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -47,6 +155,18 @@ class PenggunaController extends ResourceController
|
||||
|
||||
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'],
|
||||
@@ -73,6 +193,10 @@ class PenggunaController extends ResourceController
|
||||
|
||||
protected function rowActions(array $row): array
|
||||
{
|
||||
return [['label' => 'Kata sandi', 'href' => route('superuser.pengguna.password', $row['username']), 'icon' => 'key']];
|
||||
$href = $this->legacy()
|
||||
? route('superuser.reset-password', ['username' => $row['username']])
|
||||
: route('superuser.pengguna.password', $row['username']);
|
||||
|
||||
return [['label' => 'Kata sandi', 'href' => $href, 'icon' => 'key']];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user