🐛 Fix permission control
This commit is contained in:
parent
33767a6d7f
commit
cfe29f5def
@ -35,11 +35,9 @@ public class PermissionService(
|
|||||||
{
|
{
|
||||||
var cacheKey = _GetPermissionCacheKey(actor, area, key);
|
var cacheKey = _GetPermissionCacheKey(actor, area, key);
|
||||||
|
|
||||||
var cachedValue = await cache.GetAsync<T>(cacheKey);
|
var (hit, cachedValue) = await cache.GetAsyncWithStatus<T>(cacheKey);
|
||||||
if (cachedValue != null)
|
if (hit)
|
||||||
{
|
|
||||||
return cachedValue;
|
return cachedValue;
|
||||||
}
|
|
||||||
|
|
||||||
var now = SystemClock.Instance.GetCurrentInstant();
|
var now = SystemClock.Instance.GetCurrentInstant();
|
||||||
var groupsKey = _GetGroupsCacheKey(actor);
|
var groupsKey = _GetGroupsCacheKey(actor);
|
||||||
@ -49,8 +47,8 @@ public class PermissionService(
|
|||||||
{
|
{
|
||||||
groupsId = await db.PermissionGroupMembers
|
groupsId = await db.PermissionGroupMembers
|
||||||
.Where(n => n.Actor == actor)
|
.Where(n => n.Actor == actor)
|
||||||
.Where(n => n.ExpiredAt == null || n.ExpiredAt < now)
|
.Where(n => n.ExpiredAt == null || n.ExpiredAt > now)
|
||||||
.Where(n => n.AffectedAt == null || n.AffectedAt >= now)
|
.Where(n => n.AffectedAt == null || n.AffectedAt <= now)
|
||||||
.Select(e => e.GroupId)
|
.Select(e => e.GroupId)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
@ -60,10 +58,11 @@ public class PermissionService(
|
|||||||
}
|
}
|
||||||
|
|
||||||
var permission = await db.PermissionNodes
|
var permission = await db.PermissionNodes
|
||||||
.Where(n => n.GroupId == null || groupsId.Contains(n.GroupId.Value))
|
.Where(n => (n.GroupId == null && n.Actor == actor) ||
|
||||||
.Where(n => n.Key == key && (n.GroupId != null || n.Actor == actor) && n.Area == area)
|
(n.GroupId != null && groupsId.Contains(n.GroupId.Value)))
|
||||||
.Where(n => n.ExpiredAt == null || n.ExpiredAt < now)
|
.Where(n => n.Key == key && n.Area == area)
|
||||||
.Where(n => n.AffectedAt == null || n.AffectedAt >= now)
|
.Where(n => n.ExpiredAt == null || n.ExpiredAt > now)
|
||||||
|
.Where(n => n.AffectedAt == null || n.AffectedAt <= now)
|
||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
|
|
||||||
var result = permission is not null ? _DeserializePermissionValue<T>(permission.Value) : default;
|
var result = permission is not null ? _DeserializePermissionValue<T>(permission.Value) : default;
|
||||||
|
@ -43,6 +43,11 @@ public interface ICacheService
|
|||||||
/// Gets a value from the cache
|
/// Gets a value from the cache
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task<T?> GetAsync<T>(string key);
|
Task<T?> GetAsync<T>(string key);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get a value from the cache with the found status
|
||||||
|
/// </summary>
|
||||||
|
Task<(bool found, T? value)> GetAsyncWithStatus<T>(string key);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes a specific key from the cache
|
/// Removes a specific key from the cache
|
||||||
@ -231,6 +236,20 @@ public class CacheServiceRedis : ICacheService
|
|||||||
return JsonConvert.DeserializeObject<T>(value!, _serializerSettings);
|
return JsonConvert.DeserializeObject<T>(value!, _serializerSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<(bool found, T? value)> GetAsyncWithStatus<T>(string key)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(key))
|
||||||
|
throw new ArgumentException("Key cannot be null or empty", nameof(key));
|
||||||
|
|
||||||
|
var value = await _database.StringGetAsync(key);
|
||||||
|
|
||||||
|
if (value.IsNullOrEmpty)
|
||||||
|
return (false, default);
|
||||||
|
|
||||||
|
// For NodaTime serialization, use the configured serializer settings
|
||||||
|
return (true, JsonConvert.DeserializeObject<T>(value!, _serializerSettings));
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<bool> RemoveAsync(string key)
|
public async Task<bool> RemoveAsync(string key)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(key))
|
if (string.IsNullOrEmpty(key))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user