diff --git a/API/Authentication/GrantNames/ColorGrantNames.cs b/API/Authentication/GrantNames/ColorGrantNames.cs new file mode 100644 index 0000000..707b55a --- /dev/null +++ b/API/Authentication/GrantNames/ColorGrantNames.cs @@ -0,0 +1,14 @@ +namespace API.Authentication.GrantNames +{ + public static class ColorGrantNames + { + public const string CanGetAll = "api.color.get.all"; + public const string CanGetAny = "api.color.get.any"; + public const string CanGet = "api.color.get"; + public const string CanAdd = "api.color.add"; + public const string CanUpdateAny = "api.color.update.any"; + public const string CanUpdate = "api.color.update"; + public const string CanDeleteAny = "api.color.delete.any"; + public const string CanDelete = "api.color.delete"; + } +} diff --git a/API/Services/GrantService.cs b/API/Services/GrantService.cs index 0c11e79..648ef5d 100644 --- a/API/Services/GrantService.cs +++ b/API/Services/GrantService.cs @@ -11,5 +11,66 @@ namespace API.Services public GrantService(ILogger logger, SASGContext context, IYesAuthentication auth) : base(logger, context, auth) { } + + public bool hasGrant(ulong permissionId, string grantName) + { + return getNoAuthentication(x => x.permissionId == permissionId && x.name.Equals(grantName)).Any(); + } + + public List getValues(ulong permissionId, string grantName) + { + List grants = getNoAuthentication(x => x.permissionId == permissionId && x.name.StartsWith(grantName + ".")).ToList(); + + List values = []; + foreach (Grant grant in grants) + { + string value = grant.name.Substring(grantName.Length); + if (value.Contains('.')) + // Were not looking at a value and instead another grant + continue; + + values.Add(value); + } + + return values; + } + + public List getStringValues(ulong permissionId, string grantName) + { + List values = getValues(permissionId, grantName); + + // Get rid of numbers + values = values.Where(x => !Int32.TryParse(x, out int _)).ToList(); + + return values; + } + + public List getIntValues(ulong permissionId, string grantName) + { + List values = getValues(permissionId, grantName); + List intValues = []; + + Parallel.ForEach(values, x => + { + if (Int32.TryParse(x, out int parsed)) + intValues.Add(parsed); + }); + + return intValues; + } + + public List getULongValues(ulong permissionId, string grantName) + { + List values = getValues(permissionId, grantName); + List uLongValues = []; + + Parallel.ForEach(values, x => + { + if (UInt64.TryParse(x, out ulong parsed)) + uLongValues.Add(parsed); + }); + + return uLongValues; + } } }