Merge SharpChat permission set into the Misuzu permission system directly.

This commit is contained in:
flash 2023-09-06 20:44:28 +00:00
parent 15e96684c2
commit 498ec0cf9a
3 changed files with 78 additions and 59 deletions

View file

@ -85,11 +85,37 @@ final class Perm {
public const F_POST_DELETE_OWN = 0b00000_00000000_00000000_00000000_10000000_00000000_00000000; public const F_POST_DELETE_OWN = 0b00000_00000000_00000000_00000000_10000000_00000000_00000000;
public const F_POST_DELETE_ANY = 0b00000_00000000_00000000_00000001_00000000_00000000_00000000; public const F_POST_DELETE_ANY = 0b00000_00000000_00000000_00000001_00000000_00000000_00000000;
public const INFO_FOR_USER = ['global', 'user']; // CHAT ALLOCATION:
// 0bXXXXX_XXXXXXXX_XXXXXXXX_XXXXXXXX_XXXXXXMA_MMUMUUMT_ATATAMMM
// U -> Normal user permissions
// T -> Tenshi permissions
// M -> Moderator permissions
// A -> Administrator permissions
// no staircase here because it's a mess
public const C_USER_KICK = 0b00000_00000000_00000000_00000000_00000000_00000000_00000001;
public const C_USER_BAN = 0b00000_00000000_00000000_00000000_00000000_00000000_00000010;
//public const C_USER_SILENCE = 0b00000_00000000_00000000_00000000_00000000_00000000_00000100; // deprecated: silencing is no longer a thing
public const C_MESSAGE_BROADCAST = 0b00000_00000000_00000000_00000000_00000000_00000000_00001000;
public const C_NICK_SET_OWN = 0b00000_00000000_00000000_00000000_00000000_00000000_00010000;
public const C_NICK_SET_ANY = 0b00000_00000000_00000000_00000000_00000000_00000000_00100000;
public const C_CHANNEL_CREATE = 0b00000_00000000_00000000_00000000_00000000_00000000_01000000;
public const C_CHANNEL_SET_PERSIST = 0b00000_00000000_00000000_00000000_00000000_00000000_10000000;
public const C_CHANNEL_SET_PASSWORD = 0b00000_00000000_00000000_00000000_00000000_00000001_00000000;
public const C_CHANNEL_SET_MIN_RANK = 0b00000_00000000_00000000_00000000_00000000_00000010_00000000;
public const C_MESSAGE_SEND = 0b00000_00000000_00000000_00000000_00000000_00000100_00000000;
public const C_MESSAGE_DELETE_OWN = 0b00000_00000000_00000000_00000000_00000000_00001000_00000000;
public const C_MESSAGE_DELETE_ANY = 0b00000_00000000_00000000_00000000_00000000_00010000_00000000;
public const C_MESSAGE_EDIT_OWN = 0b00000_00000000_00000000_00000000_00000000_00100000_00000000;
public const C_MESSAGE_EDIT_ANY = 0b00000_00000000_00000000_00000000_00000000_01000000_00000000;
public const C_USER_VIEW_ADDR = 0b00000_00000000_00000000_00000000_00000000_10000000_00000000;
public const C_CHANNEL_DELETE = 0b00000_00000000_00000000_00000000_00000001_00000000_00000000;
public const C_CHANNEL_JOIN_ANY = 0b00000_00000000_00000000_00000000_00000010_00000000_00000000;
public const INFO_FOR_USER = ['global', 'user', 'chat'];
public const INFO_FOR_ROLE = self::INFO_FOR_USER; // just alias for now, no clue if this will ever desync public const INFO_FOR_ROLE = self::INFO_FOR_USER; // just alias for now, no clue if this will ever desync
public const INFO_FOR_FORUM_CATEGORY = ['forum']; public const INFO_FOR_FORUM_CATEGORY = ['forum'];
public const LISTS_FOR_USER = ['global:general', 'global:changelog', 'global:news', 'global:forum', 'global:comments', 'user:personal', 'user:manage']; public const LISTS_FOR_USER = ['global:general', 'global:changelog', 'global:news', 'global:forum', 'global:comments', 'user:personal', 'user:manage', 'chat:general'];
public const LISTS_FOR_ROLE = self::LISTS_FOR_USER; // idem public const LISTS_FOR_ROLE = self::LISTS_FOR_USER; // idem
public const LISTS_FOR_FORUM_CATEGORY = ['forum:category', 'forum:topic', 'forum:post']; public const LISTS_FOR_FORUM_CATEGORY = ['forum:category', 'forum:topic', 'forum:post'];
@ -213,6 +239,33 @@ final class Perm {
self::F_POST_DELETE_ANY, self::F_POST_DELETE_ANY,
], ],
], ],
'chat:general' => [
'title' => 'Chat Permissions',
'perms' => [
'chat',
self::C_MESSAGE_SEND,
self::C_MESSAGE_BROADCAST,
self::C_MESSAGE_EDIT_OWN,
self::C_MESSAGE_EDIT_ANY,
self::C_MESSAGE_DELETE_OWN,
self::C_MESSAGE_DELETE_ANY,
self::C_NICK_SET_OWN,
self::C_NICK_SET_ANY,
self::C_USER_KICK,
self::C_USER_BAN,
self::C_USER_VIEW_ADDR,
self::C_CHANNEL_CREATE,
self::C_CHANNEL_SET_PERSIST,
self::C_CHANNEL_SET_PASSWORD,
self::C_CHANNEL_SET_MIN_RANK,
self::C_CHANNEL_JOIN_ANY,
self::C_CHANNEL_DELETE,
],
],
]; ];
public const LABELS = [ public const LABELS = [
@ -286,6 +339,27 @@ final class Perm {
self::F_POST_DELETE_OWN => 'Can delete own forum posts.', self::F_POST_DELETE_OWN => 'Can delete own forum posts.',
self::F_POST_DELETE_ANY => 'Can delete ANY forum post.', self::F_POST_DELETE_ANY => 'Can delete ANY forum post.',
], ],
'chat' => [
self::C_USER_KICK => 'Can kick other users.',
self::C_USER_BAN => 'Can ban other users.',
//self::C_USER_SILENCE => 'Can silence other users.',
self::C_MESSAGE_BROADCAST => 'Can broadcast messages across all channels.',
self::C_NICK_SET_OWN => 'Can change own nickname.',
self::C_NICK_SET_ANY => 'Can change ANYONE\'s nickname.',
self::C_CHANNEL_CREATE => 'Can create channels.',
self::C_CHANNEL_SET_PERSIST => 'Can set channels to persist after all users leave it.',
self::C_CHANNEL_SET_PASSWORD => 'Can set passwords for own channel.',
self::C_CHANNEL_SET_MIN_RANK => 'Can set minimum requires rank to join channel.',
self::C_MESSAGE_SEND => 'Can send messages.',
self::C_MESSAGE_DELETE_OWN => 'Can delete own messages.',
self::C_MESSAGE_DELETE_ANY => 'Can delete ANY message.',
self::C_MESSAGE_EDIT_OWN => 'Can edit own messages.',
self::C_MESSAGE_EDIT_ANY => 'Can edit ANY message.',
self::C_USER_VIEW_ADDR => 'Can view IP addresses of other users.',
self::C_CHANNEL_DELETE => 'Can delete own channels.',
self::C_CHANNEL_JOIN_ANY => 'Can join any channel regardless of join requirements.',
],
]; ];
public static function label(string $category, int $permission): string { public static function label(string $category, int $permission): string {

View file

@ -1,56 +0,0 @@
<?php
namespace Misuzu\SharpChat;
use Misuzu\Perm;
use Misuzu\Perms\Permissions;
use Misuzu\Users\UserInfo;
final class SharpChatPerms {
private const P_KICK_USER = 0x00000001;
private const P_BAN_USER = 0x00000002;
//private const P_SILENCE_USER = 0x00000004;
private const P_BROADCAST = 0x00000008;
private const P_SET_OWN_NICK = 0x00000010;
private const P_SET_OTHER_NICK = 0x00000020;
private const P_CREATE_CHANNEL = 0x00000040;
private const P_DELETE_CHANNEL = 0x00010000;
private const P_SET_CHAN_PERMA = 0x00000080;
private const P_SET_CHAN_PASS = 0x00000100;
private const P_SET_CHAN_HIER = 0x00000200;
private const P_JOIN_ANY_CHAN = 0x00020000;
private const P_SEND_MESSAGE = 0x00000400;
private const P_DELETE_OWN_MSG = 0x00000800;
private const P_DELETE_ANY_MSG = 0x00001000;
private const P_EDIT_OWN_MSG = 0x00002000;
private const P_EDIT_ANY_MSG = 0x00004000;
private const P_VIEW_IP_ADDR = 0x00008000;
private const PERMS_DEFAULT = self::P_SEND_MESSAGE | self::P_DELETE_OWN_MSG | self::P_EDIT_OWN_MSG;
private const PERMS_MANAGE_USERS = self::P_SET_OWN_NICK | self::P_SET_OTHER_NICK | self::P_DELETE_ANY_MSG
| self::P_EDIT_ANY_MSG | self::P_VIEW_IP_ADDR | self::P_BROADCAST;
private const PERMS_CHANGE_BACKG = self::P_SET_OWN_NICK | self::P_CREATE_CHANNEL | self::P_SET_CHAN_PASS;
private const PERMS_MANAGE_FORUM = self::P_CREATE_CHANNEL | self::P_SET_CHAN_PERMA | self::P_SET_CHAN_PASS
| self::P_SET_CHAN_HIER | self::P_DELETE_CHANNEL | self::P_JOIN_ANY_CHAN;
public static function convert(Permissions $perms, UserInfo $userInfo): int {
$perms = $perms->getPermissions(['global', 'user'], $userInfo);
$convert = self::PERMS_DEFAULT;
if($perms->user->check(Perm::U_USERS_MANAGE))
$convert |= self::PERMS_MANAGE_USERS;
if($perms->user->check(Perm::U_WARNINGS_MANAGE))
$convert |= self::P_KICK_USER;
if($perms->user->check(Perm::U_BANS_MANAGE))
$convert |= self::P_BAN_USER;
if($perms->user->check(Perm::U_PROFILE_BACKGROUND_CHANGE))
$convert |= self::PERMS_CHANGE_BACKG;
if($perms->global->check(Perm::G_FORUM_CATEGORIES_MANAGE))
$convert |= self::PERMS_MANAGE_FORUM;
return $convert;
}
}

View file

@ -249,6 +249,7 @@ final class SharpChatRoutes implements IRouteHandler {
$this->usersCtx->getUsers()->recordUserActivity($userInfo, remoteAddr: $ipAddress); $this->usersCtx->getUsers()->recordUserActivity($userInfo, remoteAddr: $ipAddress);
$userColour = $this->usersCtx->getUsers()->getUserColour($userInfo); $userColour = $this->usersCtx->getUsers()->getUserColour($userInfo);
$userRank = $this->usersCtx->getUsers()->getUserRank($userInfo); $userRank = $this->usersCtx->getUsers()->getUserRank($userInfo);
$chatPerms = $this->perms->getPermissions('chat', $userInfo);
return [ return [
'success' => true, 'success' => true,
@ -257,7 +258,7 @@ final class SharpChatRoutes implements IRouteHandler {
'colour_raw' => Colour::toMisuzu($userColour), 'colour_raw' => Colour::toMisuzu($userColour),
'rank' => $userRank, 'rank' => $userRank,
'hierarchy' => $userRank, 'hierarchy' => $userRank,
'perms' => SharpChatPerms::convert($this->perms, $userInfo), 'perms' => $chatPerms->getCalculated(),
]; ];
} }