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-28 00:12:12 -04:00
|
|
|
namespace JKingWeb\Arsse;
|
2016-10-28 08:27:35 -04:00
|
|
|
|
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 {
|
2019-01-20 22:40:49 -05:00
|
|
|
const DRIVER_NAMES = [
|
|
|
|
'internal' => \JKingWeb\Arsse\User\Internal\Driver::class,
|
|
|
|
];
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public $id = null;
|
2017-02-16 15:29:42 -05:00
|
|
|
|
2017-07-14 10:16:16 -04:00
|
|
|
/**
|
|
|
|
* @var User\Driver
|
|
|
|
*/
|
2017-02-16 15:29:42 -05: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 15:29:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString() {
|
|
|
|
return (string) $this->id;
|
|
|
|
}
|
|
|
|
|
2018-10-28 10:59:17 -04:00
|
|
|
public function authorize(string $affectedUser, string $action): bool {
|
2018-11-02 10:01:49 -04:00
|
|
|
// at one time there was a complicated authorization system; it exists vestigially to support a later revival if desired
|
|
|
|
return $this->u->authorize($affectedUser, $action);
|
2017-02-22 23:22:45 -05:00
|
|
|
}
|
2017-04-06 21:41:21 -04:00
|
|
|
|
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 15:29:42 -05:00
|
|
|
}
|
|
|
|
|
2018-10-28 10:59:17 -04:00
|
|
|
public function list(): array {
|
2017-02-20 17:04:13 -05:00
|
|
|
$func = "userList";
|
2018-10-28 13:50:57 -04:00
|
|
|
if (!$this->authorize("", $func)) {
|
|
|
|
throw new User\ExceptionAuthz("notAuthorized", ["action" => $func, "user" => ""]);
|
2017-02-16 15:29:42 -05:00
|
|
|
}
|
2018-11-02 10:01:49 -04:00
|
|
|
return $this->u->userList();
|
2017-02-16 15:29:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function exists(string $user): bool {
|
2017-02-20 17:04:13 -05:00
|
|
|
$func = "userExists";
|
2018-10-28 13:50:57 -04:00
|
|
|
if (!$this->authorize($user, $func)) {
|
|
|
|
throw new User\ExceptionAuthz("notAuthorized", ["action" => $func, "user" => $user]);
|
|
|
|
}
|
2018-11-02 10:01:49 -04:00
|
|
|
return $this->u->userExists($user);
|
2017-02-16 15:29:42 -05:00
|
|
|
}
|
|
|
|
|
2017-02-20 17:04:13 -05:00
|
|
|
public function add($user, $password = null): string {
|
|
|
|
$func = "userAdd";
|
2018-10-28 13:50:57 -04:00
|
|
|
if (!$this->authorize($user, $func)) {
|
|
|
|
throw new User\ExceptionAuthz("notAuthorized", ["action" => $func, "user" => $user]);
|
2017-02-16 15:29:42 -05:00
|
|
|
}
|
2018-11-02 11:52:55 -04:00
|
|
|
return $this->u->userAdd($user, $password) ?? $this->u->userAdd($user, $this->generatePassword());
|
2017-02-16 15:29:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function remove(string $user): bool {
|
2017-02-20 17:04:13 -05:00
|
|
|
$func = "userRemove";
|
2018-10-28 13:50:57 -04:00
|
|
|
if (!$this->authorize($user, $func)) {
|
|
|
|
throw new User\ExceptionAuthz("notAuthorized", ["action" => $func, "user" => $user]);
|
|
|
|
}
|
2018-11-02 10:01:49 -04:00
|
|
|
try {
|
|
|
|
return $this->u->userRemove($user);
|
2018-11-03 13:49:02 -04:00
|
|
|
} finally { // @codeCoverageIgnore
|
2018-11-02 10:01:49 -04: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);
|
|
|
|
}
|
2017-02-16 15:29:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 19:04:08 -05:00
|
|
|
public function passwordSet(string $user, string $newPassword = null, $oldPassword = null): string {
|
2017-02-20 17:04:13 -05:00
|
|
|
$func = "userPasswordSet";
|
2018-10-28 13:50:57 -04:00
|
|
|
if (!$this->authorize($user, $func)) {
|
|
|
|
throw new User\ExceptionAuthz("notAuthorized", ["action" => $func, "user" => $user]);
|
2017-02-16 15:29:42 -05:00
|
|
|
}
|
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);
|
2018-10-28 13:50:57 -04:00
|
|
|
}
|
|
|
|
return $out;
|
2017-02-16 15:29:42 -05: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 {
|
|
|
|
$func = "userPasswordUnset";
|
|
|
|
if (!$this->authorize($user, $func)) {
|
|
|
|
throw new User\ExceptionAuthz("notAuthorized", ["action" => $func, "user" => $user]);
|
|
|
|
}
|
|
|
|
$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();
|
|
|
|
}
|
2017-08-29 10:50:31 -04:00
|
|
|
}
|