mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 13:12:41 +00:00
Abstract synthetic User backend for later tests
This commit is contained in:
parent
1834cb9963
commit
b6a47fc366
3 changed files with 143 additions and 80 deletions
73
tests/lib/User/Database.php
Normal file
73
tests/lib/User/Database.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace JKingWeb\NewsSync\Test\User;
|
||||
use JKingWeb\NewsSync\User\Driver;
|
||||
use JKingWeb\NewsSync\User\Exception;
|
||||
use JKingWeb\NewsSync\User\ExceptionAuthz;
|
||||
use PasswordGenerator\Generator as PassGen;
|
||||
|
||||
class Database extends DriverSkeleton {
|
||||
|
||||
function userExists(string $user): bool {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
return parent::userExists($user);
|
||||
}
|
||||
|
||||
function userAdd(string $user, string $password = null): string {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if($this->userExists($user)) throw new Exception("alreadyExists", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if($password===null) $password = (new PassGen)->length($this->data->conf->userTempPasswordLength)->get();
|
||||
return parent::userAdd($user, $password);
|
||||
}
|
||||
|
||||
function userRemove(string $user): bool {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if(!$this->userExists($user)) throw new Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
|
||||
return parent::userRemove($user);
|
||||
}
|
||||
|
||||
function userList(string $domain = null): array {
|
||||
if($domain===null) {
|
||||
if(!$this->data->user->authorize("", __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => "global"]);
|
||||
return parent::userList();
|
||||
} else {
|
||||
$suffix = '@'.$domain;
|
||||
if(!$this->data->user->authorize($suffix, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $domain]);
|
||||
return parent::userList($domain);
|
||||
}
|
||||
}
|
||||
|
||||
function userPasswordSet(string $user, string $newPassword = null, string $oldPassword = null): string {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if(!$this->userExists($user)) throw new Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if($newPassword===null) $newPassword = (new PassGen)->length($this->data->conf->userTempPasswordLength)->get();
|
||||
return parent::userPasswordSet($user, $newPassword);
|
||||
}
|
||||
|
||||
function userPropertiesGet(string $user): array {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if(!$this->userExists($user)) throw new Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
|
||||
$out = parent::userPropertiesGet($user);
|
||||
unset($out['password']);
|
||||
return $out;
|
||||
}
|
||||
|
||||
function userPropertiesSet(string $user, array $properties): array {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if(!$this->userExists($user)) throw new Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
|
||||
parent::userPropertiesSet($user, $properties);
|
||||
return $this->userPropertiesGet($user);
|
||||
}
|
||||
|
||||
function userRightsGet(string $user): int {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if(!$this->userExists($user)) throw new Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
|
||||
return parent::userRightsGet($user);
|
||||
}
|
||||
|
||||
function userRightsSet(string $user, int $level): bool {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if(!$this->userExists($user)) throw new Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
|
||||
return parent::userRightsSet($user, $level);
|
||||
}
|
||||
}
|
|
@ -1,13 +1,9 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace JKingWeb\NewsSync\Test\User;
|
||||
use JKingWeb\NewsSync\Lang;
|
||||
use JKingWeb\NewsSync\User\Driver;
|
||||
use JKingWeb\NewsSync\User\Exception;
|
||||
use JKingWeb\NewsSync\User\ExceptionAuthz;
|
||||
use PasswordGenerator\Generator as PassGen;
|
||||
|
||||
class DriverInternalMock implements Driver {
|
||||
class DriverInternalMock extends Database implements Driver {
|
||||
|
||||
protected $db = [];
|
||||
protected $data;
|
||||
|
@ -50,79 +46,4 @@ class DriverInternalMock implements Driver {
|
|||
if(password_verify($password, $this->db[$user]['password'])) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function userExists(string $user): bool {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
return array_key_exists($user, $this->db);
|
||||
}
|
||||
|
||||
function userAdd(string $user, string $password = null): string {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if($this->userExists($user)) throw new Exception("alreadyExists", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if($password===null) $password = (new PassGen)->length($this->data->conf->userTempPasswordLength)->get();
|
||||
$u = [
|
||||
'password' => $password ? password_hash($password, \PASSWORD_DEFAULT) : null,
|
||||
'rights' => Driver::RIGHTS_NONE,
|
||||
];
|
||||
$this->db[$user] = $u;
|
||||
return $password;
|
||||
}
|
||||
|
||||
function userRemove(string $user): bool {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if(!$this->userExists($user)) throw new Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
|
||||
unset($this->db[$user]);
|
||||
return true;
|
||||
}
|
||||
|
||||
function userList(string $domain = null): array {
|
||||
$list = array_keys($this->db);
|
||||
if($domain===null) {
|
||||
if(!$this->data->user->authorize("", __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => "global"]);
|
||||
return $list;
|
||||
} else {
|
||||
$suffix = '@'.$domain;
|
||||
$len = -1 * strlen($suffix);
|
||||
if(!$this->data->user->authorize($suffix, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $domain]);
|
||||
return array_filter($list, function($user) use($suffix, $len) {
|
||||
return substr_compare($user, $suffix, $len);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function userPasswordSet(string $user, string $newPassword = null, string $oldPassword = null): string {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if(!$this->userExists($user)) throw new Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if($newPassword===null) $newPassword = (new PassGen)->length($this->data->conf->userTempPasswordLength)->get();
|
||||
$this->db[$user]['password'] = password_hash($newPassword, \PASSWORD_DEFAULT);
|
||||
return $newPassword;
|
||||
}
|
||||
|
||||
function userPropertiesGet(string $user): array {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if(!$this->userExists($user)) throw new Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
|
||||
$out = $this->db[$user];
|
||||
unset($out['password']);
|
||||
return $out;
|
||||
}
|
||||
|
||||
function userPropertiesSet(string $user, array $properties): array {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if(!$this->userExists($user)) throw new Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
|
||||
$this->db[$user] = array_merge($this->db[$user], $properties);
|
||||
return $this->userPropertiesGet($user);
|
||||
}
|
||||
|
||||
function userRightsGet(string $user): int {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if(!$this->userExists($user)) throw new Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
|
||||
return $this->db[$user]['rights'];
|
||||
}
|
||||
|
||||
function userRightsSet(string $user, int $level): bool {
|
||||
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
|
||||
if(!$this->userExists($user)) throw new Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
|
||||
$this->db[$user]['rights'] = $level;
|
||||
return true;
|
||||
}
|
||||
}
|
69
tests/lib/User/DriverSkeleton.php
Normal file
69
tests/lib/User/DriverSkeleton.php
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace JKingWeb\NewsSync\Test\User;
|
||||
use JKingWeb\NewsSync\Lang;
|
||||
use JKingWeb\NewsSync\User\Driver;
|
||||
use JKingWeb\NewsSync\User\Exception;
|
||||
use JKingWeb\NewsSync\User\ExceptionAuthz;
|
||||
use PasswordGenerator\Generator as PassGen;
|
||||
|
||||
abstract class DriverSkeleton {
|
||||
|
||||
protected $db = [];
|
||||
protected $data;
|
||||
|
||||
function userExists(string $user): bool {
|
||||
return array_key_exists($user, $this->db);
|
||||
}
|
||||
|
||||
function userAdd(string $user, string $password = null): string {
|
||||
$u = [
|
||||
'password' => $password ? password_hash($password, \PASSWORD_DEFAULT) : null,
|
||||
'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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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'];
|
||||
}
|
||||
|
||||
function userRightsSet(string $user, int $level): bool {
|
||||
$this->db[$user]['rights'] = $level;
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue