Rework PermissionChecker to not be dumb...

This commit is contained in:
Daniel_I_Am 2020-10-16 17:21:42 +02:00
parent 29c5a0a102
commit 9b604b67a7
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84

View File

@ -14,7 +14,7 @@ namespace ChaosBot.Services
// Debug information // Debug information
LoggingFacade.Trace($"CheckCommandPerm.CheckPermissionsAsync|Checking permissions for command: {command}"); 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; if (!(context.User is SocketGuildUser gUser)) return false;
// Get the possible permissions // Get the possible permissions
@ -33,29 +33,20 @@ namespace ChaosBot.Services
// Loop through all permissions // Loop through all permissions
foreach (CommandPermission perm in commandPermissions) foreach (CommandPermission perm in commandPermissions)
{ {
ulong requiredGroup; // TODO: fix {CommandPermission} model to not be int, but to be PermissionTarget, so we can remove the int casts
// Check if it's a role or group permission and fetch the right type if (perm.TargetType == (int) PermissionTarget.User)
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)
{ {
if (context.User.Id == perm.TargetId) if (context.User.Id == perm.TargetId)
return true; return true;
}
return false; // 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)
else
{ {
// Return the permission value // Check if any of the users roles are of the required group, if so, permission granted
requiredGroup = perm.TargetId; 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 else
@ -71,7 +62,9 @@ namespace ChaosBot.Services
return true; return true;
} }
else else
{
LoggingFacade.Info("CheckCommandperm.CheckPermissionsAsync|commandPermissions: No Default"); LoggingFacade.Info("CheckCommandperm.CheckPermissionsAsync|commandPermissions: No Default");
}
} }
// Permission denied // Permission denied