Finer grained local permissions, also User.Can is gone.

This commit is contained in:
flash 2025-04-27 00:18:17 +00:00
parent bef41b2718
commit dd377358e2
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E
19 changed files with 70 additions and 54 deletions

View file

@ -23,14 +23,17 @@ public class FlashiiAuthResult : AuthResult {
if(UserPermissionsRaw.HasFlag(FlashiiUserPermissions.C_MESSAGE_EDIT_ANY))
perms |= UserPermissions.EditAnyMessage;
if(UserPermissionsRaw.HasFlag(FlashiiUserPermissions.C_MESSAGE_BROADCAST))
perms |= UserPermissions.Broadcast;
perms |= UserPermissions.SendBroadcast;
if(UserPermissionsRaw.HasFlag(FlashiiUserPermissions.C_USER_KICK))
perms |= UserPermissions.KickUser;
if(UserPermissionsRaw.HasFlag(FlashiiUserPermissions.C_USER_BAN))
perms |= UserPermissions.BanUser;
perms |= UserPermissions.BanUser
| UserPermissions.ViewBanList
| UserPermissions.PardonUser
| UserPermissions.PardonIPAddress;
if(UserPermissionsRaw.HasFlag(FlashiiUserPermissions.C_USER_VIEW_ADDR))
perms |= UserPermissions.SeeIPAddress;
perms |= UserPermissions.ViewIPAddress;
if(UserPermissionsRaw.HasFlag(FlashiiUserPermissions.C_CHANNEL_CREATE))
perms |= UserPermissions.CreateChannel;
@ -39,7 +42,7 @@ public class FlashiiAuthResult : AuthResult {
if(UserPermissionsRaw.HasFlag(FlashiiUserPermissions.C_CHANNEL_SET_PASSWORD))
perms |= UserPermissions.SetChannelPassword;
if(UserPermissionsRaw.HasFlag(FlashiiUserPermissions.C_CHANNEL_SET_MIN_RANK))
perms |= UserPermissions.SetChannelHierarchy;
perms |= UserPermissions.SetChannelMinimumRank;
if(UserPermissionsRaw.HasFlag(FlashiiUserPermissions.C_CHANNEL_DELETE))
perms |= UserPermissions.DeleteChannel;
if(UserPermissionsRaw.HasFlag(FlashiiUserPermissions.C_CHANNEL_JOIN_ANY))

View file

@ -12,7 +12,7 @@ public class BanListClientCommand(BansClient bansClient) : ClientCommand {
public async Task Dispatch(ClientCommandContext ctx) {
long msgId = ctx.Chat.RandomSnowflake.Next();
if(!ctx.User.Can(UserPermissions.BanUser | UserPermissions.KickUser)) {
if(!ctx.User.Permissions.HasFlag(UserPermissions.ViewBanList)) {
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
return;
}

View file

@ -12,7 +12,7 @@ public class BroadcastClientCommand : ClientCommand {
public async Task Dispatch(ClientCommandContext ctx) {
long msgId = ctx.Chat.RandomSnowflake.Next();
if(!ctx.User.Can(UserPermissions.Broadcast)) {
if(!ctx.User.Permissions.HasFlag(UserPermissions.SendBroadcast)) {
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
return;
}

View file

@ -10,7 +10,7 @@ public class CreateChannelClientCommand : ClientCommand {
public async Task Dispatch(ClientCommandContext ctx) {
long msgId = ctx.Chat.RandomSnowflake.Next();
if(!ctx.User.Can(UserPermissions.CreateChannel)) {
if(!ctx.User.Permissions.HasFlag(UserPermissions.CreateChannel)) {
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
return;
}
@ -47,7 +47,7 @@ public class CreateChannelClientCommand : ClientCommand {
Channel createChan = new(
createChanName,
isTemporary: !ctx.User.Can(UserPermissions.SetChannelPermanent),
isTemporary: !ctx.User.Permissions.HasFlag(UserPermissions.SetChannelPermanent),
rank: createChanHierarchy,
ownerId: ctx.User.UserId
);

View file

@ -26,7 +26,7 @@ public class DeleteChannelClientCommand : ClientCommand {
return;
}
if(!ctx.User.Can(UserPermissions.DeleteChannel) && delChan.IsOwner(ctx.User)) {
if(!ctx.User.Permissions.HasFlag(UserPermissions.DeleteChannel) && delChan.IsOwner(ctx.User)) {
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.CHANNEL_DELETE_FAILED, true, delChan.Name));
return;
}

View file

@ -13,9 +13,9 @@ public class DeleteMessageClientCommand : ClientCommand {
public async Task Dispatch(ClientCommandContext ctx) {
long msgId = ctx.Chat.RandomSnowflake.Next();
bool deleteAnyMessage = ctx.User.Can(UserPermissions.DeleteAnyMessage);
bool deleteAnyMessage = ctx.User.Permissions.HasFlag(UserPermissions.DeleteAnyMessage);
if(!deleteAnyMessage && !ctx.User.Can(UserPermissions.DeleteOwnMessage)) {
if(!deleteAnyMessage && !ctx.User.Permissions.HasFlag(UserPermissions.DeleteOwnMessage)) {
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
return;
}

View file

@ -14,7 +14,7 @@ public class KickBanClientCommand(BansClient bansClient) : ClientCommand {
bool isBanning = ctx.NameEquals("ban");
long msgId = ctx.Chat.RandomSnowflake.Next();
if(!ctx.User.Can(isBanning ? UserPermissions.BanUser : UserPermissions.KickUser)) {
if(!ctx.User.Permissions.HasFlag(isBanning ? UserPermissions.BanUser : UserPermissions.KickUser)) {
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
return;
}

View file

@ -14,9 +14,9 @@ public class NickClientCommand : ClientCommand {
public async Task Dispatch(ClientCommandContext ctx) {
long msgId = ctx.Chat.RandomSnowflake.Next();
bool setOthersNick = ctx.User.Can(UserPermissions.SetOthersNickname);
bool setOthersNick = ctx.User.Permissions.HasFlag(UserPermissions.SetOthersNickname);
if(!setOthersNick && !ctx.User.Can(UserPermissions.SetOwnNickname)) {
if(!setOthersNick && !ctx.User.Permissions.HasFlag(UserPermissions.SetOwnNickname)) {
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
return;
}

View file

@ -13,7 +13,7 @@ public class PardonAddressClientCommand(BansClient bansClient) : ClientCommand {
public async Task Dispatch(ClientCommandContext ctx) {
long msgId = ctx.Chat.RandomSnowflake.Next();
if(!ctx.User.Can(UserPermissions.BanUser | UserPermissions.KickUser)) {
if(!ctx.User.Permissions.HasFlag(UserPermissions.PardonIPAddress)) {
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
return;
}

View file

@ -12,7 +12,7 @@ public class PardonUserClientCommand(BansClient bansClient) : ClientCommand {
public async Task Dispatch(ClientCommandContext ctx) {
long msgId = ctx.Chat.RandomSnowflake.Next();
if(!ctx.User.Can(UserPermissions.BanUser | UserPermissions.KickUser)) {
if(!ctx.User.Permissions.HasFlag(UserPermissions.PardonUser)) {
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
return;
}

View file

@ -11,7 +11,7 @@ public class PasswordChannelClientCommand : ClientCommand {
public async Task Dispatch(ClientCommandContext ctx) {
long msgId = ctx.Chat.RandomSnowflake.Next();
if(!ctx.User.Can(UserPermissions.SetChannelPassword) || ctx.Channel.IsOwner(ctx.User)) {
if(!ctx.User.Permissions.HasFlag(UserPermissions.SetChannelPassword) || ctx.Channel.IsOwner(ctx.User)) {
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
return;
}

View file

@ -12,7 +12,7 @@ public class RankChannelClientCommand : ClientCommand {
public async Task Dispatch(ClientCommandContext ctx) {
long msgId = ctx.Chat.RandomSnowflake.Next();
if(!ctx.User.Can(UserPermissions.SetChannelHierarchy) || ctx.Channel.IsOwner(ctx.User)) {
if(!ctx.User.Permissions.HasFlag(UserPermissions.SetChannelMinimumRank) || ctx.Channel.IsOwner(ctx.User)) {
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
return;
}

View file

@ -12,7 +12,7 @@ public class RemoteAddressClientCommand : ClientCommand {
public async Task Dispatch(ClientCommandContext ctx) {
long msgId = ctx.Chat.RandomSnowflake.Next();
if(!ctx.User.Can(UserPermissions.SeeIPAddress)) {
if(!ctx.User.Permissions.HasFlag(UserPermissions.ViewIPAddress)) {
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, "/ip"));
return;
}

View file

@ -37,7 +37,7 @@ public class WhoClientCommand : ClientCommand {
return;
}
if(whoChan.Rank > ctx.User.Rank || (whoChan.HasPassword && !ctx.User.Can(UserPermissions.JoinAnyChannel))) {
if(whoChan.Rank > ctx.User.Rank || (whoChan.HasPassword && !ctx.User.Permissions.HasFlag(UserPermissions.JoinAnyChannel))) {
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.USERS_LISTING_ERROR, true, whoChanStr));
return;
}

View file

@ -275,7 +275,7 @@ public class Context {
return;
}
if(!user.Can(UserPermissions.JoinAnyChannel) && chan.IsOwner(user)) {
if(!user.Permissions.HasFlag(UserPermissions.JoinAnyChannel) && chan.IsOwner(user)) {
if(chan.Rank > user.Rank) {
await SendTo(user, new CommandResponseS2CPacket(RandomSnowflake.Next(), LCR.CHANNEL_INSUFFICIENT_HIERARCHY, true, chan.Name));
await ForceChannel(user);

View file

@ -8,7 +8,7 @@ public partial class MariaDBEventStorage {
if(sup.HasFlag(StoredUserPermissions.BanUser))
perms |= UserPermissions.BanUser;
if(sup.HasFlag(StoredUserPermissions.Broadcast))
perms |= UserPermissions.Broadcast;
perms |= UserPermissions.SendBroadcast;
if(sup.HasFlag(StoredUserPermissions.SetOwnNickname))
perms |= UserPermissions.SetOwnNickname;
if(sup.HasFlag(StoredUserPermissions.SetOthersNickname))
@ -22,7 +22,7 @@ public partial class MariaDBEventStorage {
if(sup.HasFlag(StoredUserPermissions.SetChannelPassword))
perms |= UserPermissions.SetChannelPassword;
if(sup.HasFlag(StoredUserPermissions.SetChannelHierarchy))
perms |= UserPermissions.SetChannelHierarchy;
perms |= UserPermissions.SetChannelMinimumRank;
if(sup.HasFlag(StoredUserPermissions.JoinAnyChannel))
perms |= UserPermissions.JoinAnyChannel;
if(sup.HasFlag(StoredUserPermissions.SendMessage))
@ -36,9 +36,15 @@ public partial class MariaDBEventStorage {
if(sup.HasFlag(StoredUserPermissions.EditAnyMessage))
perms |= UserPermissions.EditAnyMessage;
if(sup.HasFlag(StoredUserPermissions.SeeIPAddress))
perms |= UserPermissions.SeeIPAddress;
perms |= UserPermissions.ViewIPAddress;
if(sup.HasFlag(StoredUserPermissions.ViewLogs))
perms |= UserPermissions.ViewLogs;
if(sup.HasFlag(StoredUserPermissions.ViewBanList))
perms |= UserPermissions.ViewBanList;
if(sup.HasFlag(StoredUserPermissions.PardonUser))
perms |= UserPermissions.PardonUser;
if(sup.HasFlag(StoredUserPermissions.PardonIPAddress))
perms |= UserPermissions.PardonIPAddress;
return perms;
}
@ -50,7 +56,7 @@ public partial class MariaDBEventStorage {
perms |= StoredUserPermissions.KickUser;
if(up.HasFlag(UserPermissions.BanUser))
perms |= StoredUserPermissions.BanUser;
if(up.HasFlag(UserPermissions.Broadcast))
if(up.HasFlag(UserPermissions.SendBroadcast))
perms |= StoredUserPermissions.Broadcast;
if(up.HasFlag(UserPermissions.SetOwnNickname))
perms |= StoredUserPermissions.SetOwnNickname;
@ -64,7 +70,7 @@ public partial class MariaDBEventStorage {
perms |= StoredUserPermissions.SetChannelPermanent;
if(up.HasFlag(UserPermissions.SetChannelPassword))
perms |= StoredUserPermissions.SetChannelPassword;
if(up.HasFlag(UserPermissions.SetChannelHierarchy))
if(up.HasFlag(UserPermissions.SetChannelMinimumRank))
perms |= StoredUserPermissions.SetChannelHierarchy;
if(up.HasFlag(UserPermissions.JoinAnyChannel))
perms |= StoredUserPermissions.JoinAnyChannel;
@ -78,10 +84,16 @@ public partial class MariaDBEventStorage {
perms |= StoredUserPermissions.EditOwnMessage;
if(up.HasFlag(UserPermissions.EditAnyMessage))
perms |= StoredUserPermissions.EditAnyMessage;
if(up.HasFlag(UserPermissions.SeeIPAddress))
if(up.HasFlag(UserPermissions.ViewIPAddress))
perms |= StoredUserPermissions.SeeIPAddress;
if(up.HasFlag(UserPermissions.ViewLogs))
perms |= StoredUserPermissions.ViewLogs;
if(up.HasFlag(UserPermissions.ViewBanList))
perms |= StoredUserPermissions.ViewBanList;
if(up.HasFlag(UserPermissions.PardonUser))
perms |= StoredUserPermissions.PardonUser;
if(up.HasFlag(UserPermissions.PardonIPAddress))
perms |= StoredUserPermissions.PardonIPAddress;
return perms;
}

View file

@ -2,23 +2,26 @@
[Flags]
public enum StoredUserPermissions : int {
KickUser = 0x00000001,
BanUser = 0x00000002,
//SilenceUser = 0x00000004,
Broadcast = 0x00000008,
SetOwnNickname = 0x00000010,
SetOthersNickname = 0x00000020,
CreateChannel = 0x00000040,
DeleteChannel = 0x00010000,
SetChannelPermanent = 0x00000080,
SetChannelPassword = 0x00000100,
SetChannelHierarchy = 0x00000200,
JoinAnyChannel = 0x00020000,
SendMessage = 0x00000400,
DeleteOwnMessage = 0x00000800,
DeleteAnyMessage = 0x00001000,
EditOwnMessage = 0x00002000,
EditAnyMessage = 0x00004000,
SeeIPAddress = 0x00008000,
ViewLogs = 0x00040000,
KickUser = 0x1,
BanUser = 0x2,
//SilenceUser = 0x4,
Broadcast = 0x8,
SetOwnNickname = 0x10,
SetOthersNickname = 0x20,
CreateChannel = 0x40,
SetChannelPermanent = 0x80,
SetChannelPassword = 0x100,
SetChannelHierarchy = 0x200,
SendMessage = 0x400,
DeleteOwnMessage = 0x800,
DeleteAnyMessage = 0x1000,
EditOwnMessage = 0x2000,
EditAnyMessage = 0x4000,
SeeIPAddress = 0x8000,
DeleteChannel = 0x10000,
JoinAnyChannel = 0x20000,
ViewLogs = 0x40000,
ViewBanList = 0x80000,
PardonUser = 0x100000,
PardonIPAddress = 0x200000,
}

View file

@ -49,11 +49,6 @@ public class User(
}
}
public bool Can(UserPermissions perm, bool strict = false) {
UserPermissions perms = Permissions & perm;
return strict ? perms == perm : perms > 0;
}
public bool NameEquals(string name) {
return string.Equals(name, UserName, StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(name, NickName, StringComparison.InvariantCultureIgnoreCase)

View file

@ -12,17 +12,20 @@ public enum UserPermissions : ulong {
DeleteAnyMessage = 1ul << 3,
EditOwnMessage = 1ul << 4,
EditAnyMessage = 1ul << 5,
Broadcast = 1ul << 6,
SendBroadcast = 1ul << 6,
ViewLogs = 1ul << 7,
KickUser = 1ul << 10,
BanUser = 1ul << 11,
SeeIPAddress = 1ul << 12,
PardonUser = 1ul << 12,
PardonIPAddress = 1ul << 13,
ViewIPAddress = 1ul << 14,
ViewBanList = 1ul << 15,
CreateChannel = 1ul << 20,
SetChannelPermanent = 1ul << 21,
SetChannelPassword = 1ul << 22,
SetChannelHierarchy = 1ul << 23,
SetChannelMinimumRank = 1ul << 23,
DeleteChannel = 1ul << 24,
JoinAnyChannel = 1ul << 25,