From 9b604b67a7428d75e35eac6771ea37c7aebef2e0 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Fri, 16 Oct 2020 17:21:42 +0200 Subject: [PATCH] Rework PermissionChecker to not be dumb... --- ChaosBot/Services/CheckPermissions.cs | 29 ++++++++++----------------- 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/ChaosBot/Services/CheckPermissions.cs b/ChaosBot/Services/CheckPermissions.cs index 21a7ef7..1d4b59d 100644 --- a/ChaosBot/Services/CheckPermissions.cs +++ b/ChaosBot/Services/CheckPermissions.cs @@ -14,7 +14,7 @@ namespace ChaosBot.Services // Debug information LoggingFacade.Trace($"CheckCommandPerm.CheckPermissionsAsync|Checking permissions for command: {command}"); - // If user is not SocketGuildUser, then return error + // If user is not SocketGuildUser, then do not grant permission if (!(context.User is SocketGuildUser gUser)) return false; // Get the possible permissions @@ -33,29 +33,20 @@ namespace ChaosBot.Services // Loop through all permissions foreach (CommandPermission perm in commandPermissions) { - ulong requiredGroup; - // Check if it's a role or group permission and fetch the right type - if (perm.TargetType == (int)PermissionTarget.Role) - { - // If it's a role, check the configuration for the role otherwise return the permission value - requiredGroup = ConfigurationRepository.GetValue($"Role:{perm.TargetId}", context.Guild.Id, perm.TargetId); - } - else if (perm.TargetType == (int) PermissionTarget.User) + // TODO: fix {CommandPermission} model to not be int, but to be PermissionTarget, so we can remove the int casts + if (perm.TargetType == (int) PermissionTarget.User) { if (context.User.Id == perm.TargetId) return true; + } - return false; - } - else + // TODO: fix {CommandPermission} model to not be int, but to be PermissionTarget, so we can remove the int casts + if (perm.TargetType == (int) PermissionTarget.Role) { - // Return the permission value - requiredGroup = perm.TargetId; + // Check if any of the users roles are of the required group, if so, permission granted + if (gUser.Roles.Any(r => r.Id == perm.TargetId)) + return true; } - - // Check if any of the users roles are of the required group, if so, permission granted - if (gUser.Roles.Any(r => r.Id == requiredGroup)) - return true; } } else @@ -71,7 +62,9 @@ namespace ChaosBot.Services return true; } else + { LoggingFacade.Info("CheckCommandperm.CheckPermissionsAsync|commandPermissions: No Default"); + } } // Permission denied