Auth Db: use helper methods to retrieve data instead of FPDO raw methods

This commit is contained in:
Benjamin Renard 2024-02-04 11:02:55 +01:00
parent dbf5b0a54c
commit 20461e3e47
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -86,13 +86,11 @@ class Db extends Backend {
public static function get_user($username) { public static function get_user($username) {
self :: connect(); self :: connect();
try { try {
$query = self :: $class :: $fpdo -> from(self :: $users_table) $info = self :: $class :: get_one(
-> select(null) self :: $users_table,
-> select(self :: $exposed_fields) [self :: $username_field => $username],
-> where(self :: $username_field, $username); self :: $exposed_fields
);
$result = $query -> execute();
$info = $result -> fetch();
if ($info === false) if ($info === false)
return null; return null;
return new User($username, '\\EesyPHP\\Auth\\Db', $info); return new User($username, '\\EesyPHP\\Auth\\Db', $info);
@ -112,16 +110,14 @@ class Db extends Backend {
public static function check_password($user, $password) { public static function check_password($user, $password) {
self :: connect(); self :: connect();
try { try {
$query = self :: $class :: $fpdo -> from(self :: $users_table) $info = self :: $class :: get_one(
-> select(null) self :: $users_table,
-> select(self :: $password_field) [self :: $username_field => $user->username],
-> where(self :: $username_field, $user->username); [self :: $password_field]
);
$result = $query -> execute();
$info = $result -> fetch();
if ($info === false) if ($info === false)
return false; return false;
return password_verify($password, $info['password']); return password_verify($password, $info[self :: $password_field]);
} }
catch (Exception $e) { catch (Exception $e) {
Log :: error("Error retrieving user %s password from database: %s", $user, $e->getMessage()); Log :: error("Error retrieving user %s password from database: %s", $user, $e->getMessage());