From 818fd52dc6799d5b35266d98fa796e7333f40e85 Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 6 Aug 2024 20:32:02 +0000 Subject: [PATCH] Fixed group permissions --- Chat/Commands/CommandManager.cs | 6 ++-- Chat/Commands/VoiceCommand.cs | 10 +++---- .../Permissions/GroupPermissionManager.cs | 30 +++++++++++-------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Chat/Commands/CommandManager.cs b/Chat/Commands/CommandManager.cs index 8475f20..e21e3f6 100644 --- a/Chat/Commands/CommandManager.cs +++ b/Chat/Commands/CommandManager.cs @@ -71,10 +71,10 @@ namespace TwitchChatTTS.Chat.Commands long chatterId = long.Parse(message.ChatterUserId); if (chatterId != _user.OwnerId) { - bool executable = command.AcceptCustomPermission ? CanExecute(chatterId, groups, $"tts.command.{com}", selectorResult.Permissions) : false; + bool executable = command.AcceptCustomPermission ? CanExecute(chatterId, groups, $"tts.commands.{com}", selectorResult.Permissions) : false; if (!executable) { - _logger.Debug($"Denied permission to use command [chatter id: {chatterId}][command: {com}]"); + _logger.Warning($"Denied permission to use command [chatter id: {chatterId}][args: {arg}][command type: {command.GetType().Name}]"); return ChatCommandResult.Permission; } } @@ -116,7 +116,7 @@ namespace TwitchChatTTS.Chat.Commands private bool CanExecute(long chatterId, IEnumerable groups, string path, string[]? additionalPaths) { _logger.Debug($"Checking for permission [chatter id: {chatterId}][group: {string.Join(", ", groups)}][path: {path}]{(additionalPaths != null ? "[paths: " + string.Join('|', additionalPaths) + "]" : string.Empty)}"); - if (_permissionManager.CheckIfAllowed(groups, path) != false) + if (_permissionManager.CheckIfAllowed(groups, path) == true) { if (additionalPaths == null) return true; diff --git a/Chat/Commands/VoiceCommand.cs b/Chat/Commands/VoiceCommand.cs index 1cea797..9671871 100644 --- a/Chat/Commands/VoiceCommand.cs +++ b/Chat/Commands/VoiceCommand.cs @@ -25,7 +25,7 @@ namespace TwitchChatTTS.Chat.Commands b.CreateVoiceNameParameter("voiceName", true) .CreateCommand(new TTSVoiceSelector(_user, _logger)) .CreateMentionParameter("chatter", enabled: true, optional: true) - .AddPermission("tts.command.voice.admin") + .AddPermission("tts.commands.voice.admin") .CreateCommand(new TTSVoiceSelectorAdmin(_user, _logger)); }); } @@ -56,12 +56,12 @@ namespace TwitchChatTTS.Chat.Commands if (_user.VoicesSelected.ContainsKey(chatterId)) { await hermes.UpdateTTSUser(chatterId, voice.Key); - _logger.Debug($"Sent request to create chat TTS voice [voice: {voice.Value}][username: {message.ChatterUserLogin}][reason: command]"); + _logger.Debug($"Sent request to update chat TTS voice [voice: {voice.Value}][username: {message.ChatterUserLogin}][reason: command]"); } else { await hermes.CreateTTSUser(chatterId, voice.Key); - _logger.Debug($"Sent request to update chat TTS voice [voice: {voice.Value}][username: {message.ChatterUserLogin}][reason: command]"); + _logger.Debug($"Sent request to create chat TTS voice [voice: {voice.Value}][username: {message.ChatterUserLogin}][reason: command]"); } } } @@ -99,12 +99,12 @@ namespace TwitchChatTTS.Chat.Commands if (_user.VoicesSelected.ContainsKey(chatterId)) { await hermes.UpdateTTSUser(chatterId, voice.Key); - _logger.Debug($"Sent request to create chat TTS voice [voice: {voice.Value}][username: {mention.UserLogin}][reason: command]"); + _logger.Debug($"Sent request to update chat TTS voice [voice: {voice.Value}][username: {mention.UserLogin}][reason: command]"); } else { await hermes.CreateTTSUser(chatterId, voice.Key); - _logger.Debug($"Sent request to update chat TTS voice [voice: {voice.Value}][username: {mention.UserLogin}][reason: command]"); + _logger.Debug($"Sent request to create chat TTS voice [voice: {voice.Value}][username: {mention.UserLogin}][reason: command]"); } } } diff --git a/Chat/Groups/Permissions/GroupPermissionManager.cs b/Chat/Groups/Permissions/GroupPermissionManager.cs index ab4a34a..0403df5 100644 --- a/Chat/Groups/Permissions/GroupPermissionManager.cs +++ b/Chat/Groups/Permissions/GroupPermissionManager.cs @@ -18,30 +18,34 @@ namespace TwitchChatTTS.Chat.Groups.Permissions public bool? CheckIfAllowed(string path) { - var res = Get(path)?.Allow; + var res = Get(path)!.Allow; _logger.Debug($"Permission Node GET {path} = {res?.ToString() ?? "null"}"); return res; } public bool? CheckIfDirectAllowed(string path) { - var res = Get(path)?.DirectAllow; + var node = Get(path, nullIfMissing: true); + if (node == null) + return null; + + var res = node.DirectAllow; _logger.Debug($"Permission Node GET {path} = {res?.ToString() ?? "null"} [direct]"); return res; } public bool? CheckIfAllowed(IEnumerable groups, string path) { - bool overall = false; + bool overall = true; foreach (var group in groups) { var result = CheckIfAllowed($"{group}.{path}"); - if (result == false) - return false; if (result == true) - overall = true; + return true; + if (result == false) + overall = false; } - return overall ? true : null; + return overall ? null : false; } public bool? CheckIfDirectAllowed(IEnumerable groups, string path) @@ -83,16 +87,16 @@ namespace TwitchChatTTS.Chat.Groups.Permissions public void Set(string path, bool? allow) { var node = Get(path, true); - node.Allow = allow; + node!.Allow = allow; _logger.Debug($"Permission Node ADD {path} = {allow?.ToString() ?? "null"}"); } - private PermissionNode Get(string path, bool edit = false) + private PermissionNode? Get(string path, bool edit = false, bool nullIfMissing = false) { - return Get(_root, path.ToLower(), edit); + return Get(_root, path.ToLower(), edit, nullIfMissing); } - private PermissionNode Get(PermissionNode node, string path, bool edit) + private PermissionNode? Get(PermissionNode node, string path, bool edit, bool nullIfMissing) { if (path.Length == 0) return node; @@ -103,12 +107,12 @@ namespace TwitchChatTTS.Chat.Groups.Permissions if (next == null) { if (!edit) - return node; + return nullIfMissing ? null : node; next = new PermissionNode(name, node, null); node.Add(next); } - return Get(next, string.Join('.', parts.Skip(1)), edit); + return Get(next, string.Join('.', parts.Skip(1)), edit, nullIfMissing); } private sealed class PermissionNode