Commit awal: prototipe UI SIAKAD Untan
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Auth;
|
||||
|
||||
use App\Enums\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Events\Lockout;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class LoginRequest extends FormRequest
|
||||
{
|
||||
private const int MAX_ATTEMPTS = 5;
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<int, string>>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'username' => ['required', 'string'],
|
||||
'password' => ['required', 'string'],
|
||||
'remember' => ['nullable', 'boolean'],
|
||||
// Peran halaman masuk (kolom tersembunyi). Nilai tak dikenal diperlakukan sebagai kegagalan biasa di authenticate().
|
||||
'role' => ['nullable', 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Coba masuk. Akun tidak aktif atau berperan lain dari halaman masuk gagal dengan
|
||||
* pesan yang sama persis dengan kredensial salah agar keberadaan akun tidak bocor.
|
||||
*
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function authenticate(): void
|
||||
{
|
||||
$this->ensureIsNotRateLimited();
|
||||
|
||||
$authenticated = Auth::attemptWhen(
|
||||
$this->only('username', 'password'),
|
||||
fn (User $user): bool => $user->is_active && $this->matchesRequestedRole($user),
|
||||
$this->boolean('remember'),
|
||||
);
|
||||
|
||||
if (! $authenticated) {
|
||||
RateLimiter::hit($this->throttleKey());
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'username' => trans('auth.failed'),
|
||||
])->redirectTo($this->getRedirectUrl());
|
||||
}
|
||||
|
||||
RateLimiter::clear($this->throttleKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function ensureIsNotRateLimited(): void
|
||||
{
|
||||
if (! RateLimiter::tooManyAttempts($this->throttleKey(), self::MAX_ATTEMPTS)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event(new Lockout($this));
|
||||
|
||||
$seconds = RateLimiter::availableIn($this->throttleKey());
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'username' => trans('auth.throttle', [
|
||||
'seconds' => $seconds,
|
||||
'minutes' => ceil($seconds / 60),
|
||||
]),
|
||||
])->redirectTo($this->getRedirectUrl());
|
||||
}
|
||||
|
||||
/** Halaman masuk portal tidak membatasi peran; halaman peran hanya menerima akun berperan itu. */
|
||||
private function matchesRequestedRole(User $user): bool
|
||||
{
|
||||
$requested = $this->input('role');
|
||||
|
||||
if ($requested === null || $requested === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return is_string($requested) && $user->role->value === $requested;
|
||||
}
|
||||
|
||||
/** Gagal masuk dari halaman peran kembali ke halaman itu (bukan portal). */
|
||||
protected function getRedirectUrl(): string
|
||||
{
|
||||
$role = is_string($this->input('role')) ? Role::tryFrom($this->input('role')) : null;
|
||||
|
||||
return $role ? route('login.role', $role) : parent::getRedirectUrl();
|
||||
}
|
||||
|
||||
public function throttleKey(): string
|
||||
{
|
||||
return Str::transliterate(Str::lower($this->string('username')->toString()).'|'.$this->ip());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user