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
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