Rework PermissionChecker to not be dumb...
This commit is contained in:
parent
29c5a0a102
commit
9b604b67a7
@ -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,31 +33,22 @@ 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;
|
|
||||||
}
|
}
|
||||||
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
|
// Check if any of the users roles are of the required group, if so, permission granted
|
||||||
if (gUser.Roles.Any(r => r.Id == requiredGroup))
|
if (gUser.Roles.Any(r => r.Id == perm.TargetId))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (defaultRole == "Admin")
|
if (defaultRole == "Admin")
|
||||||
@ -71,8 +62,10 @@ 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
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user