[USER] Add UserRoles

This commit is contained in:
Hugo Sales 2020-07-22 11:49:56 +00:00 committed by Hugo Sales
parent 5a74354703
commit f3ccdf8017
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
1 changed files with 20 additions and 11 deletions

View File

@ -31,22 +31,31 @@ namespace App\Core;
*/
abstract class UserRoles
{
const ADMIN = 1;
const MODERATOR = 2;
const USER = 4;
public const ADMIN = 1;
public const MODERATOR = 2;
public const USER = 4;
public const BOT = 8;
public static $consts = null;
public static function bitmapToStrings(int $r): array
{
$roles = [];
$consts = (new ReflectionClass(__CLASS__))->getConstants();
while ($r != 0) {
foreach ($consts as $c => $v) {
if ($r & $v !== 0) {
$r &= ~$v;
$roles[] = "ROLE_{$c}";
}
$roles = [];
if (self::$consts == null) {
self::$consts = (new \ReflectionClass(__CLASS__))->getConstants();
}
foreach (self::$consts as $c => $v) {
if (($r & $v) !== 0) {
$r -= $v;
$roles[] = "ROLE_{$c}";
}
}
if ($r != 0) {
Log::error('User roles bitmap to array failed');
}
return $roles;
}
}