2017-02-26 17:09:44 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse\Test\User;
|
|
|
|
use JKingWeb\Arsse\Lang;
|
|
|
|
use JKingWeb\Arsse\User\Driver;
|
|
|
|
use JKingWeb\Arsse\User\Exception;
|
|
|
|
use JKingWeb\Arsse\User\ExceptionAuthz;
|
2017-02-26 17:09:44 +00:00
|
|
|
use PasswordGenerator\Generator as PassGen;
|
|
|
|
|
|
|
|
abstract class DriverSkeleton {
|
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
protected $db = [];
|
|
|
|
|
|
|
|
function userExists(string $user): bool {
|
2017-02-26 17:09:44 +00:00
|
|
|
return array_key_exists($user, $this->db);
|
|
|
|
}
|
|
|
|
|
|
|
|
function userAdd(string $user, string $password = null): string {
|
|
|
|
$u = [
|
2017-02-28 04:04:13 +00:00
|
|
|
'password' => $password ? password_hash($password, \PASSWORD_DEFAULT) : "",
|
2017-02-26 17:09:44 +00:00
|
|
|
'rights' => Driver::RIGHTS_NONE,
|
|
|
|
];
|
|
|
|
$this->db[$user] = $u;
|
|
|
|
return $password;
|
|
|
|
}
|
|
|
|
|
|
|
|
function userRemove(string $user): bool {
|
|
|
|
unset($this->db[$user]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function userList(string $domain = null): array {
|
|
|
|
$list = array_keys($this->db);
|
|
|
|
if($domain===null) {
|
|
|
|
return $list;
|
|
|
|
} else {
|
|
|
|
$suffix = '@'.$domain;
|
|
|
|
$len = -1 * strlen($suffix);
|
|
|
|
return array_filter($list, function($user) use($suffix, $len) {
|
|
|
|
return substr_compare($user, $suffix, $len);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-04-07 01:41:21 +00:00
|
|
|
|
2017-02-26 17:09:44 +00:00
|
|
|
function userPasswordSet(string $user, string $newPassword = null, string $oldPassword = null): string {
|
|
|
|
$this->db[$user]['password'] = password_hash($newPassword, \PASSWORD_DEFAULT);
|
|
|
|
return $newPassword;
|
|
|
|
}
|
|
|
|
|
|
|
|
function userPropertiesGet(string $user): array {
|
|
|
|
$out = $this->db[$user];
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
function userPropertiesSet(string $user, array $properties): array {
|
|
|
|
$this->db[$user] = array_merge($this->db[$user], $properties);
|
|
|
|
return $this->userPropertiesGet($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
function userRightsGet(string $user): int {
|
|
|
|
return $this->db[$user]['rights'];
|
|
|
|
}
|
2017-04-07 01:41:21 +00:00
|
|
|
|
2017-02-26 17:09:44 +00:00
|
|
|
function userRightsSet(string $user, int $level): bool {
|
|
|
|
$this->db[$user]['rights'] = $level;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|