1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00
Arsse/lib/User.php

197 lines
7.9 KiB
PHP
Raw Normal View History

<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
2017-03-28 04:12:12 +00:00
namespace JKingWeb\Arsse;
class User {
2017-08-29 14:50:31 +00:00
public $id = null;
2017-02-16 20:29:42 +00:00
/**
* @var User\Driver
*/
2017-02-16 20:29:42 +00:00
protected $u;
2017-08-29 14:50:31 +00:00
public static function driverList(): array {
2017-02-16 20:29:42 +00:00
$sep = \DIRECTORY_SEPARATOR;
$path = __DIR__.$sep."User".$sep;
$classes = [];
2017-08-29 14:50:31 +00:00
foreach (glob($path."*".$sep."Driver.php") as $file) {
$name = basename(dirname($file));
$class = NS_BASE."User\\$name\\Driver";
$classes[$class] = $class::driverName();
2017-02-16 20:29:42 +00:00
}
return $classes;
}
public function __construct() {
$driver = Arsse::$conf->userDriver;
$this->u = new $driver();
2017-02-16 20:29:42 +00:00
}
public function __toString() {
return (string) $this->id;
}
// at one time there was a complicated authorization system; it exists vestigially to support a later revival if desired
public function authorize(string $affectedUser, string $action): bool {
return true;
}
public function auth(string $user, string $password): bool {
$prevUser = $this->id ?? null;
$this->id = $user;
switch ($this->u->driverFunctions("auth")) {
case User\Driver::FUNC_EXTERNAL:
if (Arsse::$conf->userPreAuth) {
$out = true;
} else {
$out = $this->u->auth($user, $password);
}
if ($out && !Arsse::$db->userExists($user)) {
$this->autoProvision($user, $password);
}
break;
case User\Driver::FUNC_INTERNAL:
if (Arsse::$conf->userPreAuth) {
if (!Arsse::$db->userExists($user)) {
2017-07-21 02:40:09 +00:00
$this->autoProvision($user, $password);
}
$out = true;
} else {
$out = $this->u->auth($user, $password);
}
break;
case User\Driver::FUNCT_NOT_IMPLEMENTED:
$out = false;
break;
2017-02-16 20:29:42 +00:00
}
if (!$out) {
$this->id = $prevUser;
2017-07-21 02:40:09 +00:00
}
return $out;
2017-02-16 20:29:42 +00:00
}
public function driverFunctions(string $function = null) {
return $this->u->driverFunctions($function);
}
public function list(): array {
$func = "userList";
2017-08-29 14:50:31 +00:00
switch ($this->u->driverFunctions($func)) {
case User\Driver::FUNC_EXTERNAL:
// we handle authorization checks for external drivers
if (!$this->authorize("", $func)) {
throw new User\ExceptionAuthz("notAuthorized", ["action" => $func, "user" => ""]);
}
2017-12-07 20:18:25 +00:00
// no break
case User\Driver::FUNC_INTERNAL:
// internal functions handle their own authorization
return $this->u->userList($domain);
case User\Driver::FUNCT_NOT_IMPLEMENTED:
throw new User\ExceptionNotImplemented("notImplemented", ["action" => $func, "user" => $domain]);
2017-02-16 20:29:42 +00:00
}
}
public function exists(string $user): bool {
$func = "userExists";
2017-08-29 14:50:31 +00:00
switch ($this->u->driverFunctions($func)) {
case User\Driver::FUNC_EXTERNAL:
// we handle authorization checks for external drivers
2017-08-29 14:50:31 +00:00
if (!$this->authorize($user, $func)) {
2017-07-21 02:40:09 +00:00
throw new User\ExceptionAuthz("notAuthorized", ["action" => $func, "user" => $user]);
}
$out = $this->u->userExists($user);
2017-08-29 14:50:31 +00:00
if ($out && !Arsse::$db->userExists($user)) {
2017-07-21 02:40:09 +00:00
$this->autoProvision($user, "");
}
return $out;
case User\Driver::FUNC_INTERNAL:
// internal functions handle their own authorization
return $this->u->userExists($user);
case User\Driver::FUNCT_NOT_IMPLEMENTED:
// throwing an exception here would break all kinds of stuff; we just report that the user exists
return true;
2017-02-16 20:29:42 +00:00
}
}
public function add($user, $password = null): string {
$func = "userAdd";
2017-08-29 14:50:31 +00:00
switch ($this->u->driverFunctions($func)) {
case User\Driver::FUNC_EXTERNAL:
// we handle authorization checks for external drivers
2017-08-29 14:50:31 +00:00
if (!$this->authorize($user, $func)) {
2017-07-21 02:40:09 +00:00
throw new User\ExceptionAuthz("notAuthorized", ["action" => $func, "user" => $user]);
}
$newPassword = $this->u->userAdd($user, $password);
// if there was no exception and we don't have the user in the internal database, add it
2017-08-29 14:50:31 +00:00
if (!Arsse::$db->userExists($user)) {
2017-07-21 02:40:09 +00:00
$this->autoProvision($user, $newPassword);
}
return $newPassword;
case User\Driver::FUNC_INTERNAL:
// internal functions handle their own authorization
return $this->u->userAdd($user, $password);
case User\Driver::FUNCT_NOT_IMPLEMENTED:
throw new User\ExceptionNotImplemented("notImplemented", ["action" => $func, "user" => $user]);
2017-02-16 20:29:42 +00:00
}
}
public function remove(string $user): bool {
$func = "userRemove";
2017-08-29 14:50:31 +00:00
switch ($this->u->driverFunctions($func)) {
case User\Driver::FUNC_EXTERNAL:
// we handle authorization checks for external drivers
2017-08-29 14:50:31 +00:00
if (!$this->authorize($user, $func)) {
2017-07-21 02:40:09 +00:00
throw new User\ExceptionAuthz("notAuthorized", ["action" => $func, "user" => $user]);
}
$out = $this->u->userRemove($user);
2017-08-29 14:50:31 +00:00
if ($out && Arsse::$db->userExists($user)) {
// if the user was removed and we have it in our data, remove it there
2017-08-29 14:50:31 +00:00
if (!Arsse::$db->userExists($user)) {
2017-07-21 02:40:09 +00:00
Arsse::$db->userRemove($user);
}
}
return $out;
case User\Driver::FUNC_INTERNAL:
// internal functions handle their own authorization
return $this->u->userRemove($user);
case User\Driver::FUNCT_NOT_IMPLEMENTED:
throw new User\ExceptionNotImplemented("notImplemented", ["action" => $func, "user" => $user]);
2017-02-16 20:29:42 +00:00
}
}
2017-02-21 00:04:08 +00:00
public function passwordSet(string $user, string $newPassword = null, $oldPassword = null): string {
$func = "userPasswordSet";
2017-08-29 14:50:31 +00:00
switch ($this->u->driverFunctions($func)) {
case User\Driver::FUNC_EXTERNAL:
// we handle authorization checks for external drivers
2017-08-29 14:50:31 +00:00
if (!$this->authorize($user, $func)) {
2017-07-21 02:40:09 +00:00
throw new User\ExceptionAuthz("notAuthorized", ["action" => $func, "user" => $user]);
}
$out = $this->u->userPasswordSet($user, $newPassword, $oldPassword);
2017-08-29 14:50:31 +00:00
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, $out);
} else {
// if the user does not exists in the internal database, create it
$this->autoProvision($user, $out);
}
return $out;
case User\Driver::FUNC_INTERNAL:
// internal functions handle their own authorization
return $this->u->userPasswordSet($user, $newPassword);
case User\Driver::FUNCT_NOT_IMPLEMENTED:
throw new User\ExceptionNotImplemented("notImplemented", ["action" => $func, "user" => $user]);
2017-02-16 20:29:42 +00:00
}
}
protected function autoProvision(string $user, string $password = null): string {
$out = Arsse::$db->userAdd($user, $password);
return $out;
2017-02-16 20:29:42 +00:00
}
2017-08-29 14:50:31 +00:00
}