86 lines
2.0 KiB
C#
86 lines
2.0 KiB
C#
using API.Services.Interfaces;
|
|
using DAL.Contexts;
|
|
using DAL.Models;
|
|
using System.Linq.Expressions;
|
|
|
|
namespace API.Services
|
|
{
|
|
public class GrantManager : IGrantManager
|
|
{
|
|
private readonly SASGContext _context;
|
|
private ILogger<GrantManager> _logger;
|
|
|
|
public GrantManager(ILogger<GrantManager> logger, SASGContext context)
|
|
{
|
|
_logger = logger;
|
|
_context = context;
|
|
}
|
|
|
|
public bool hasGrant(ulong permissionId, string grantName)
|
|
{
|
|
return getGrant(x => x.permissionId == permissionId && x.name.Equals(grantName)).Any();
|
|
}
|
|
|
|
public List<string> getValues(ulong permissionId, string grantName)
|
|
{
|
|
List<Grant> grants = getGrant(x => x.permissionId == permissionId && x.name.StartsWith(grantName + ".")).ToList();
|
|
|
|
List<string> values = [];
|
|
foreach (Grant grant in grants)
|
|
{
|
|
string value = grant.name.Substring(grantName.Length + 1);
|
|
if (value.Contains('.'))
|
|
// Were not looking at a value and instead another grant
|
|
continue;
|
|
|
|
values.Add(value);
|
|
}
|
|
|
|
return values;
|
|
}
|
|
|
|
public List<string> getStringValues(ulong permissionId, string grantName)
|
|
{
|
|
List<string> values = getValues(permissionId, grantName);
|
|
|
|
// Get rid of numbers
|
|
values = values.Where(x => !Int32.TryParse(x, out int _)).ToList();
|
|
|
|
return values;
|
|
}
|
|
|
|
public List<int> getIntValues(ulong permissionId, string grantName)
|
|
{
|
|
List<string> values = getValues(permissionId, grantName);
|
|
List<int> intValues = [];
|
|
|
|
Parallel.ForEach(values, x =>
|
|
{
|
|
if (Int32.TryParse(x, out int parsed))
|
|
intValues.Add(parsed);
|
|
});
|
|
|
|
return intValues;
|
|
}
|
|
|
|
public List<ulong> getULongValues(ulong permissionId, string grantName)
|
|
{
|
|
List<string> values = getValues(permissionId, grantName);
|
|
List<ulong> uLongValues = [];
|
|
|
|
Parallel.ForEach(values, x =>
|
|
{
|
|
if (UInt64.TryParse(x, out ulong parsed))
|
|
uLongValues.Add(parsed);
|
|
});
|
|
|
|
return uLongValues;
|
|
}
|
|
|
|
private IEnumerable<Grant> getGrant(Expression<Func<Grant, bool>> whereClause)
|
|
{
|
|
return _context.Set<Grant>().Where(whereClause);
|
|
}
|
|
}
|
|
}
|