78 lines
2.5 KiB
PHP
78 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Auth;
|
|
|
|
use App\Models\Legacy\Account;
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
use Illuminate\Contracts\Auth\UserProvider;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\QueryException;
|
|
use RuntimeException;
|
|
|
|
/**
|
|
* Login ke akun SIAKAD lama, meniru f_login_er_new: username dan sandi disandikan dengan
|
|
* fungsi DB f_encr() lalu dibandingkan dengan t_account. Sandi hanya dibandingkan di SQL.
|
|
*/
|
|
class LegacyUserProvider implements UserProvider
|
|
{
|
|
public function retrieveById($identifier): ?Account
|
|
{
|
|
return $this->resolve(Account::query()->whereKey($identifier));
|
|
}
|
|
|
|
/** Tanpa kolom remember_token, token "ingat saya" tidak bisa dipulihkan. */
|
|
public function retrieveByToken($identifier, #[\SensitiveParameter] $token): ?Account
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function updateRememberToken(Authenticatable $user, #[\SensitiveParameter] $token): void
|
|
{
|
|
//
|
|
}
|
|
|
|
public function retrieveByCredentials(#[\SensitiveParameter] array $credentials): ?Account
|
|
{
|
|
$username = $credentials['username'] ?? null;
|
|
|
|
if (! is_string($username) || trim($username) === '') {
|
|
return null;
|
|
}
|
|
|
|
// Disandikan dulu agar pencarian memakai indeks unik username (f_encr volatile).
|
|
$encoded = (new Account)->getConnection()->scalar('select f_encr(?)', [trim($username)]);
|
|
|
|
return $this->resolve(Account::query()->where('t_account.username', $encoded));
|
|
}
|
|
|
|
public function validateCredentials(Authenticatable $user, #[\SensitiveParameter] array $credentials): bool
|
|
{
|
|
$password = $credentials['password'] ?? null;
|
|
|
|
if (! is_string($password) || $password === '') {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
return Account::query()
|
|
->whereKey($user->getAuthIdentifier())
|
|
->whereRaw('passwd = f_encr(?)', [$password])
|
|
->exists();
|
|
} catch (QueryException $e) {
|
|
// Pesan asli memuat binding (sandi polos); jangan diteruskan ke layar atau log.
|
|
throw new RuntimeException("Gagal memeriksa kata sandi di t_account (SQLSTATE {$e->getCode()}).");
|
|
}
|
|
}
|
|
|
|
public function rehashPasswordIfRequired(Authenticatable $user, #[\SensitiveParameter] array $credentials, bool $force = false): void
|
|
{
|
|
//
|
|
}
|
|
|
|
/** @param Builder<Account> $query */
|
|
private function resolve(Builder $query): ?Account
|
|
{
|
|
return $query->withIdentity()->first()?->hydrateLegacy();
|
|
}
|
|
}
|