2016-10-28 08:27:35 -04:00
|
|
|
<?php
|
2017-11-16 20:23:18 -05:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2016-10-28 08:27:35 -04:00
|
|
|
declare(strict_types=1);
|
2017-03-27 23:12:12 -05:00
|
|
|
namespace JKingWeb\Arsse;
|
2016-10-28 08:27:35 -04:00
|
|
|
|
2020-11-09 13:43:07 -05:00
|
|
|
use JKingWeb\Arsse\Misc\ValueInfo as V;
|
2020-11-16 00:11:19 -05:00
|
|
|
use JKingWeb\Arsse\User\ExceptionConflict as Conflict;
|
2018-11-02 11:52:55 -04:00
|
|
|
use PasswordGenerator\Generator as PassGen;
|
2018-10-28 13:50:57 -04:00
|
|
|
|
2016-10-28 08:27:35 -04:00
|
|
|
class User {
|
2020-03-01 18:32:01 -05:00
|
|
|
public const DRIVER_NAMES = [
|
2019-01-20 22:40:49 -05:00
|
|
|
'internal' => \JKingWeb\Arsse\User\Internal\Driver::class,
|
|
|
|
];
|
2020-12-05 11:01:44 -05:00
|
|
|
public const PROPERTIES = [
|
|
|
|
'admin' => V::T_BOOL,
|
|
|
|
'lang' => V::T_STRING,
|
|
|
|
'tz' => V::T_STRING,
|
|
|
|
'sort_asc' => V::T_BOOL,
|
|
|
|
'theme' => V::T_STRING,
|
|
|
|
'page_size' => V::T_INT, // greater than zero
|
|
|
|
'shortcuts' => V::T_BOOL,
|
|
|
|
'gestures' => V::T_BOOL,
|
|
|
|
'stylesheet' => V::T_STRING,
|
|
|
|
'reading_time' => V::T_BOOL,
|
|
|
|
];
|
2020-12-05 22:13:48 -05:00
|
|
|
public const PROPERTIES_LARGE = ["stylesheet"];
|
2019-01-20 22:40:49 -05:00
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public $id = null;
|
2017-02-16 14:29:42 -06:00
|
|
|
|
2020-03-01 15:16:50 -05:00
|
|
|
/** @var User\Driver */
|
2017-02-16 14:29:42 -06:00
|
|
|
protected $u;
|
2017-04-06 21:41:21 -04:00
|
|
|
|
2018-11-02 10:01:49 -04:00
|
|
|
public function __construct(\JKingWeb\Arsse\User\Driver $driver = null) {
|
|
|
|
$this->u = $driver ?? new Arsse::$conf->userDriver;
|
2017-02-16 14:29:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString() {
|
|
|
|
return (string) $this->id;
|
|
|
|
}
|
|
|
|
|
2018-10-28 10:59:17 -04:00
|
|
|
public function auth(string $user, string $password): bool {
|
2018-11-02 10:01:49 -04:00
|
|
|
$prevUser = $this->id;
|
2018-10-28 10:59:17 -04:00
|
|
|
$this->id = $user;
|
2018-10-28 13:50:57 -04:00
|
|
|
if (Arsse::$conf->userPreAuth) {
|
|
|
|
$out = true;
|
|
|
|
} else {
|
|
|
|
$out = $this->u->auth($user, $password);
|
|
|
|
}
|
2018-11-02 10:01:49 -04:00
|
|
|
// if authentication was successful and we don't have the user in the internal database, add it
|
|
|
|
// users must be in the internal database to preserve referential integrity
|
2018-10-28 13:50:57 -04:00
|
|
|
if ($out && !Arsse::$db->userExists($user)) {
|
2018-11-02 10:01:49 -04:00
|
|
|
Arsse::$db->userAdd($user, $password);
|
2017-07-20 22:40:09 -04:00
|
|
|
}
|
2018-11-02 10:01:49 -04:00
|
|
|
$this->id = $prevUser;
|
2018-10-28 10:59:17 -04:00
|
|
|
return $out;
|
2017-02-16 14:29:42 -06:00
|
|
|
}
|
|
|
|
|
2018-10-28 10:59:17 -04:00
|
|
|
public function list(): array {
|
2018-11-02 10:01:49 -04:00
|
|
|
return $this->u->userList();
|
2017-02-16 14:29:42 -06:00
|
|
|
}
|
|
|
|
|
2020-11-15 16:24:26 -05:00
|
|
|
public function add(string $user, ?string $password = null): string {
|
2020-11-16 00:11:19 -05:00
|
|
|
// ensure the user name does not contain any U+003A COLON characters, as
|
|
|
|
// this is incompatible with HTTP Basic authentication
|
|
|
|
if (strpos($user, ":") !== false) {
|
|
|
|
throw new User\ExceptionInput("invalidUsername", "U+003A COLON");
|
|
|
|
}
|
2020-11-15 16:24:26 -05:00
|
|
|
try {
|
|
|
|
$out = $this->u->userAdd($user, $password) ?? $this->u->userAdd($user, $this->generatePassword());
|
2020-11-16 00:11:19 -05:00
|
|
|
} catch (Conflict $e) {
|
2020-11-15 16:24:26 -05:00
|
|
|
if (!Arsse::$db->userExists($user)) {
|
2020-11-16 00:11:19 -05:00
|
|
|
Arsse::$db->userAdd($user, null);
|
2020-11-15 16:24:26 -05:00
|
|
|
}
|
2020-11-16 00:11:19 -05:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
// synchronize the internal database
|
|
|
|
if (!Arsse::$db->userExists($user)) {
|
|
|
|
Arsse::$db->userAdd($user, $out);
|
2020-11-11 18:50:27 -05:00
|
|
|
}
|
|
|
|
return $out;
|
2017-02-16 14:29:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public function remove(string $user): bool {
|
2018-11-02 10:01:49 -04:00
|
|
|
try {
|
2020-11-16 00:11:19 -05:00
|
|
|
$out = $this->u->userRemove($user);
|
|
|
|
} catch (Conflict $e) {
|
2018-11-02 10:01:49 -04:00
|
|
|
if (Arsse::$db->userExists($user)) {
|
|
|
|
Arsse::$db->userRemove($user);
|
|
|
|
}
|
2020-11-16 00:11:19 -05:00
|
|
|
throw $e;
|
2017-02-16 14:29:42 -06:00
|
|
|
}
|
2020-11-16 00:11:19 -05:00
|
|
|
if (Arsse::$db->userExists($user)) {
|
|
|
|
// if the user was removed and we (still) have it in the internal database, remove it there
|
|
|
|
Arsse::$db->userRemove($user);
|
|
|
|
}
|
|
|
|
return $out;
|
2017-02-16 14:29:42 -06:00
|
|
|
}
|
|
|
|
|
2020-11-15 16:24:26 -05:00
|
|
|
public function passwordSet(string $user, ?string $newPassword, $oldPassword = null): string {
|
2018-11-02 11:52:55 -04:00
|
|
|
$out = $this->u->userPasswordSet($user, $newPassword, $oldPassword) ?? $this->u->userPasswordSet($user, $this->generatePassword(), $oldPassword);
|
|
|
|
if (Arsse::$db->userExists($user)) {
|
2018-10-28 13:50:57 -04:00
|
|
|
// if the password change was successful and the user exists, set the internal password to the same value
|
|
|
|
Arsse::$db->userPasswordSet($user, $out);
|
2019-07-25 22:34:58 -04:00
|
|
|
// also invalidate any current sessions for the user
|
|
|
|
Arsse::$db->sessionDestroy($user);
|
2020-11-11 18:50:27 -05:00
|
|
|
} else {
|
|
|
|
// if the user does not exist, add it with the new password
|
|
|
|
Arsse::$db->userAdd($user, $out);
|
2018-10-28 13:50:57 -04:00
|
|
|
}
|
|
|
|
return $out;
|
2017-02-16 14:29:42 -06:00
|
|
|
}
|
2018-11-02 11:52:55 -04:00
|
|
|
|
2019-03-24 14:42:23 -04:00
|
|
|
public function passwordUnset(string $user, $oldPassword = null): bool {
|
|
|
|
$out = $this->u->userPasswordUnset($user, $oldPassword);
|
|
|
|
if (Arsse::$db->userExists($user)) {
|
|
|
|
// if the password change was successful and the user exists, set the internal password to the same value
|
|
|
|
Arsse::$db->userPasswordSet($user, null);
|
2019-07-25 22:34:58 -04:00
|
|
|
// also invalidate any current sessions for the user
|
|
|
|
Arsse::$db->sessionDestroy($user);
|
2019-03-24 14:42:23 -04:00
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2019-03-09 22:44:59 -05:00
|
|
|
public function generatePassword(): string {
|
2018-11-02 11:52:55 -04:00
|
|
|
return (new PassGen)->length(Arsse::$conf->userTempPasswordLength)->get();
|
|
|
|
}
|
2020-11-16 10:24:06 -05:00
|
|
|
|
2020-12-05 11:01:44 -05:00
|
|
|
public function propertiesGet(string $user, bool $includeLarge = true): array {
|
|
|
|
$extra = $this->u->userPropertiesGet($user, $includeLarge);
|
2020-11-11 18:50:27 -05:00
|
|
|
// synchronize the internal database
|
|
|
|
if (!Arsse::$db->userExists($user)) {
|
2020-11-16 10:24:06 -05:00
|
|
|
Arsse::$db->userAdd($user, null);
|
|
|
|
Arsse::$db->userPropertiesSet($user, $extra);
|
2020-11-11 18:50:27 -05:00
|
|
|
}
|
2020-11-16 10:24:06 -05:00
|
|
|
// retrieve from the database to get at least the user number, and anything else the driver does not provide
|
2020-12-07 00:07:10 -05:00
|
|
|
$meta = Arsse::$db->userPropertiesGet($user, $includeLarge);
|
2020-12-05 11:01:44 -05:00
|
|
|
// combine all the data
|
|
|
|
$out = ['num' => $meta['num']];
|
|
|
|
foreach (self::PROPERTIES as $k => $t) {
|
2020-11-09 13:43:07 -05:00
|
|
|
if (array_key_exists($k, $extra)) {
|
2020-12-05 11:01:44 -05:00
|
|
|
$v = $extra[$k];
|
|
|
|
} elseif (array_key_exists($k, $meta)) {
|
|
|
|
$v = $meta[$k];
|
|
|
|
} else {
|
|
|
|
$v = null;
|
2020-11-09 13:43:07 -05:00
|
|
|
}
|
2020-12-05 11:01:44 -05:00
|
|
|
$out[$k] = V::normalize($v, $t | V::M_NULL);
|
2020-11-16 10:24:06 -05:00
|
|
|
}
|
2020-11-09 13:43:07 -05:00
|
|
|
return $out;
|
|
|
|
}
|
2020-11-16 10:24:06 -05:00
|
|
|
|
2020-11-10 17:09:59 -05:00
|
|
|
public function propertiesSet(string $user, array $data): array {
|
2020-11-09 13:43:07 -05:00
|
|
|
$in = [];
|
2020-12-05 11:01:44 -05:00
|
|
|
foreach (self::PROPERTIES as $k => $t) {
|
2020-11-09 13:43:07 -05:00
|
|
|
if (array_key_exists($k, $data)) {
|
2020-12-07 00:07:10 -05:00
|
|
|
try {
|
|
|
|
$in[$k] = V::normalize($data[$k], $t | V::M_NULL | V::M_STRICT);
|
|
|
|
} catch (\JKingWeb\Arsse\ExceptionType $e) {
|
|
|
|
throw new User\ExceptionInput("invalidValue", ['field' => $k, 'type' => $t], $e);
|
|
|
|
}
|
2020-11-09 13:43:07 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-05 11:01:44 -05:00
|
|
|
if (isset($in['tz']) && !@timezone_open($in['tz'])) {
|
|
|
|
throw new User\ExceptionInput("invalidTimezone", ['field' => "tz", 'value' => $in['tz']]);
|
|
|
|
} elseif (isset($in['page_size']) && $in['page_size'] < 1) {
|
|
|
|
throw new User\ExceptionInput("invalidNonZeroInteger", ['field' => "page_size"]);
|
2020-11-09 13:43:07 -05:00
|
|
|
}
|
|
|
|
$out = $this->u->userPropertiesSet($user, $in);
|
|
|
|
// synchronize the internal database
|
2020-11-11 18:50:27 -05:00
|
|
|
if (!Arsse::$db->userExists($user)) {
|
2020-11-17 16:23:36 -05:00
|
|
|
Arsse::$db->userAdd($user, null);
|
2020-11-11 18:50:27 -05:00
|
|
|
}
|
2020-11-10 17:09:59 -05:00
|
|
|
Arsse::$db->userPropertiesSet($user, $out);
|
2020-11-09 13:43:07 -05:00
|
|
|
return $out;
|
|
|
|
}
|
2017-08-29 10:50:31 -04:00
|
|
|
}
|