2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2023-01-10 22:44:09 +00:00
|
|
|
|
|
|
|
namespace Shimmie2;
|
|
|
|
|
2023-02-07 13:21:37 +00:00
|
|
|
use GQLA\Type;
|
|
|
|
use GQLA\Field;
|
2012-02-14 21:11:23 +00:00
|
|
|
|
2014-04-29 05:33:03 +00:00
|
|
|
/**
|
|
|
|
* Class UserClass
|
|
|
|
*/
|
2023-02-07 13:21:37 +00:00
|
|
|
#[Type(name: "UserClass")]
|
2019-05-28 16:59:38 +00:00
|
|
|
class UserClass
|
|
|
|
{
|
2024-01-20 19:52:18 +00:00
|
|
|
/** @var array<string, UserClass> */
|
|
|
|
public static array $known_classes = [];
|
|
|
|
|
2023-02-07 13:21:37 +00:00
|
|
|
#[Field]
|
2021-03-14 23:43:50 +00:00
|
|
|
public ?string $name = null;
|
|
|
|
public ?UserClass $parent = null;
|
2024-01-20 14:10:59 +00:00
|
|
|
|
|
|
|
/** @var array<string, bool> */
|
2021-03-14 23:43:50 +00:00
|
|
|
public array $abilities = [];
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @param array<string, bool> $abilities
|
|
|
|
*/
|
2020-03-18 22:15:25 +00:00
|
|
|
public function __construct(string $name, string $parent = null, array $abilities = [])
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
$this->abilities = $abilities;
|
|
|
|
|
|
|
|
if (!is_null($parent)) {
|
2024-01-20 19:52:18 +00:00
|
|
|
$this->parent = static::$known_classes[$parent];
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 19:52:18 +00:00
|
|
|
static::$known_classes[$name] = $this;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2023-02-15 22:26:34 +00:00
|
|
|
#[Field(type: "[Permission!]!")]
|
2023-02-07 13:21:37 +00:00
|
|
|
public function permissions(): array
|
|
|
|
{
|
|
|
|
global $_all_false;
|
|
|
|
$perms = [];
|
2024-01-20 01:03:01 +00:00
|
|
|
foreach ((new \ReflectionClass(Permissions::class))->getConstants() as $k => $v) {
|
2023-02-07 13:21:37 +00:00
|
|
|
if ($this->can($v)) {
|
|
|
|
$perms[] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $perms;
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Determine if this class of user can perform an action or has ability.
|
|
|
|
*/
|
|
|
|
public function can(string $ability): bool
|
|
|
|
{
|
|
|
|
if (array_key_exists($ability, $this->abilities)) {
|
2020-01-26 13:19:35 +00:00
|
|
|
return $this->abilities[$ability];
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif (!is_null($this->parent)) {
|
|
|
|
return $this->parent->can($ability);
|
|
|
|
} else {
|
|
|
|
$min_dist = 9999;
|
|
|
|
$min_ability = null;
|
2024-01-20 19:52:18 +00:00
|
|
|
foreach (UserClass::$known_classes['base']->abilities as $a => $cando) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$v = levenshtein($ability, $a);
|
|
|
|
if ($v < $min_dist) {
|
|
|
|
$min_dist = $v;
|
|
|
|
$min_ability = $a;
|
|
|
|
}
|
|
|
|
}
|
2024-02-11 15:47:40 +00:00
|
|
|
throw new ServerError("Unknown ability '$ability'. Did the developer mean '$min_ability'?");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-14 21:11:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 13:37:14 +00:00
|
|
|
$_all_false = [];
|
2024-02-21 11:12:04 +00:00
|
|
|
$_all_true = [];
|
2024-01-20 01:03:01 +00:00
|
|
|
foreach ((new \ReflectionClass(Permissions::class))->getConstants() as $k => $v) {
|
2024-01-20 19:47:26 +00:00
|
|
|
assert(is_string($v));
|
2020-03-19 13:37:14 +00:00
|
|
|
$_all_false[$v] = false;
|
2024-02-21 11:12:04 +00:00
|
|
|
$_all_true[$v] = true;
|
2020-03-19 13:37:14 +00:00
|
|
|
}
|
2024-02-21 11:12:04 +00:00
|
|
|
// hellbanned is a snowflake, it isn't really a "permission" so much as
|
|
|
|
// "a special behaviour which applies to one particular user class"
|
|
|
|
$_all_true[Permissions::HELLBANNED] = false;
|
2020-03-19 13:37:14 +00:00
|
|
|
new UserClass("base", null, $_all_false);
|
2024-02-21 11:12:04 +00:00
|
|
|
new UserClass("admin", null, $_all_true);
|
2020-03-19 13:37:14 +00:00
|
|
|
unset($_all_false);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-11-28 17:20:23 +00:00
|
|
|
// Ghost users can't do anything
|
|
|
|
new UserClass("ghost", "base", [
|
2023-12-21 21:09:49 +00:00
|
|
|
Permissions::READ_PM => true,
|
2019-11-28 17:20:23 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Anonymous users can't do anything by default, but
|
|
|
|
// the admin might grant them some permissions
|
2019-05-28 16:59:38 +00:00
|
|
|
new UserClass("anonymous", "base", [
|
2019-11-28 21:32:18 +00:00
|
|
|
Permissions::CREATE_USER => true,
|
2019-05-28 16:59:38 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
new UserClass("user", "base", [
|
2019-07-09 14:10:21 +00:00
|
|
|
Permissions::BIG_SEARCH => true,
|
|
|
|
Permissions::CREATE_IMAGE => true,
|
|
|
|
Permissions::CREATE_COMMENT => true,
|
|
|
|
Permissions::EDIT_IMAGE_TAG => true,
|
|
|
|
Permissions::EDIT_IMAGE_SOURCE => true,
|
2019-07-09 14:23:46 +00:00
|
|
|
Permissions::EDIT_IMAGE_TITLE => true,
|
2020-02-08 00:24:13 +00:00
|
|
|
Permissions::EDIT_IMAGE_RELATIONSHIPS => true,
|
|
|
|
Permissions::EDIT_IMAGE_ARTIST => true,
|
2019-07-09 14:10:21 +00:00
|
|
|
Permissions::CREATE_IMAGE_REPORT => true,
|
|
|
|
Permissions::EDIT_IMAGE_RATING => true,
|
2020-02-08 00:24:13 +00:00
|
|
|
Permissions::EDIT_FAVOURITES => true,
|
2023-02-24 05:32:23 +00:00
|
|
|
Permissions::CREATE_VOTE => true,
|
2019-12-15 20:40:05 +00:00
|
|
|
Permissions::SEND_PM => true,
|
|
|
|
Permissions::READ_PM => true,
|
2020-06-02 23:08:24 +00:00
|
|
|
Permissions::SET_PRIVATE_IMAGE => true,
|
2022-07-09 22:37:43 +00:00
|
|
|
Permissions::PERFORM_BULK_ACTIONS => true,
|
2020-06-22 00:17:58 +00:00
|
|
|
Permissions::BULK_DOWNLOAD => true,
|
2024-02-10 23:32:50 +00:00
|
|
|
Permissions::CHANGE_USER_SETTING => true,
|
2024-02-21 11:12:51 +00:00
|
|
|
Permissions::FORUM_CREATE => true,
|
2024-02-10 23:32:50 +00:00
|
|
|
Permissions::NOTES_CREATE => true,
|
|
|
|
Permissions::NOTES_EDIT => true,
|
2024-02-21 11:13:09 +00:00
|
|
|
Permissions::NOTES_REQUEST => true,
|
2024-02-10 23:32:50 +00:00
|
|
|
Permissions::POOLS_CREATE => true,
|
|
|
|
Permissions::POOLS_UPDATE => true,
|
2019-05-28 16:59:38 +00:00
|
|
|
]);
|
|
|
|
|
2020-03-19 13:37:14 +00:00
|
|
|
new UserClass("hellbanned", "user", [
|
|
|
|
Permissions::HELLBANNED => true,
|
|
|
|
]);
|
|
|
|
|
2012-06-18 00:06:36 +00:00
|
|
|
@include_once "data/config/user-classes.conf.php";
|