2017-03-07 23:01:13 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-03-07 23:01:13 +00:00
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse\User\Internal;
|
2017-03-07 23:01:13 +00:00
|
|
|
|
2018-11-03 17:26:22 +00:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
2020-11-15 21:24:26 +00:00
|
|
|
use JKingWeb\Arsse\User\ExceptionConflict;
|
2018-11-03 17:26:22 +00:00
|
|
|
|
2018-11-02 14:01:49 +00:00
|
|
|
class Driver implements \JKingWeb\Arsse\User\Driver {
|
2018-10-28 17:50:57 +00:00
|
|
|
public function __construct() {
|
|
|
|
}
|
2017-03-07 23:01:13 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public static function driverName(): string {
|
2017-07-17 11:47:57 +00:00
|
|
|
return Arsse::$lang->msg("Driver.User.Internal.Name");
|
2017-03-07 23:01:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-28 17:50:57 +00:00
|
|
|
public function auth(string $user, string $password): bool {
|
|
|
|
try {
|
2018-11-04 17:06:30 +00:00
|
|
|
$hash = $this->userPasswordGet($user);
|
2019-03-24 18:42:23 +00:00
|
|
|
if (is_null($hash)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-15 21:24:26 +00:00
|
|
|
} catch (ExceptionConflict $e) {
|
2018-10-28 17:50:57 +00:00
|
|
|
return false;
|
2017-07-21 02:40:09 +00:00
|
|
|
}
|
2020-03-01 20:16:50 +00:00
|
|
|
if ($password === "" && $hash === "") {
|
2018-10-28 17:50:57 +00:00
|
|
|
return true;
|
2017-03-07 23:01:13 +00:00
|
|
|
}
|
2018-10-28 17:50:57 +00:00
|
|
|
return password_verify($password, $hash);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:34:03 +00:00
|
|
|
public function userAdd(string $user, string $password = null): ?string {
|
2018-11-02 15:52:55 +00:00
|
|
|
if (isset($password)) {
|
|
|
|
// only add the user if the password is not null; the user manager will retry with a generated password if null is returned
|
|
|
|
Arsse::$db->userAdd($user, $password);
|
|
|
|
}
|
|
|
|
return $password;
|
2017-03-07 23:01:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 22:47:36 +00:00
|
|
|
public function userRename(string $user, string $newName): bool {
|
|
|
|
// do nothing: the internal database is updated regardless of what the driver does (assuming it does not throw an exception)
|
|
|
|
// throw an exception if the user does not exist
|
|
|
|
if (!$this->userExists($user)) {
|
|
|
|
throw new ExceptionConflict("doesNotExist", ['action' => __FUNCTION__, 'user' => $user]);
|
|
|
|
} else {
|
|
|
|
return !($user === $newName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 17:50:57 +00:00
|
|
|
public function userRemove(string $user): bool {
|
|
|
|
return Arsse::$db->userRemove($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function userList(): array {
|
|
|
|
return Arsse::$db->userList();
|
|
|
|
}
|
|
|
|
|
2020-11-09 23:14:03 +00:00
|
|
|
public function userPasswordSet(string $user, ?string $newPassword, string $oldPassword = null): ?string {
|
2018-11-02 15:52:55 +00:00
|
|
|
// do nothing: the internal database is updated regardless of what the driver does (assuming it does not throw an exception)
|
2020-12-25 22:47:36 +00:00
|
|
|
// throw an exception if the user does not exist
|
|
|
|
if (!$this->userExists($user)) {
|
|
|
|
throw new ExceptionConflict("doesNotExist", ['action' => __FUNCTION__, 'user' => $user]);
|
|
|
|
} else {
|
|
|
|
return $newPassword;
|
|
|
|
}
|
2018-10-28 17:50:57 +00:00
|
|
|
}
|
2018-11-04 17:06:30 +00:00
|
|
|
|
2019-03-24 18:42:23 +00:00
|
|
|
public function userPasswordUnset(string $user, string $oldPassword = null): bool {
|
|
|
|
// do nothing: the internal database is updated regardless of what the driver does (assuming it does not throw an exception)
|
|
|
|
// throw an exception if the user does not exist
|
|
|
|
if (!$this->userExists($user)) {
|
2020-12-25 22:47:36 +00:00
|
|
|
throw new ExceptionConflict("doesNotExist", ['action' => __FUNCTION__, 'user' => $user]);
|
2019-03-24 18:42:23 +00:00
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:34:03 +00:00
|
|
|
protected function userPasswordGet(string $user): ?string {
|
2018-11-04 17:06:30 +00:00
|
|
|
return Arsse::$db->userPasswordGet($user);
|
|
|
|
}
|
2020-11-09 23:14:03 +00:00
|
|
|
|
|
|
|
protected function userExists(string $user): bool {
|
|
|
|
return Arsse::$db->userExists($user);
|
|
|
|
}
|
2020-11-10 22:09:59 +00:00
|
|
|
|
2020-12-06 03:13:48 +00:00
|
|
|
public function userPropertiesGet(string $user, bool $includeLarge = true): array {
|
2020-11-10 22:09:59 +00:00
|
|
|
// do nothing: the internal database will retrieve everything for us
|
|
|
|
if (!$this->userExists($user)) {
|
2020-12-25 22:47:36 +00:00
|
|
|
throw new ExceptionConflict("doesNotExist", ['action' => __FUNCTION__, 'user' => $user]);
|
2020-11-10 22:09:59 +00:00
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function userPropertiesSet(string $user, array $data): array {
|
|
|
|
// do nothing: the internal database will set everything for us
|
|
|
|
if (!$this->userExists($user)) {
|
2020-12-25 22:47:36 +00:00
|
|
|
throw new ExceptionConflict("doesNotExist", ['action' => __FUNCTION__, 'user' => $user]);
|
2020-11-10 22:09:59 +00:00
|
|
|
} else {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|