fix(数据权限): 修复 DataScopeContext 缓存 JSON 反序列化失败

将 IReadOnlySet/IReadOnlyList 改为 HashSet/List 等可序列化类型;缓存读取失败时自动重建。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zzh 2026-07-03 17:47:29 +08:00
parent 5f1f16c9ad
commit 11520d014b
2 changed files with 15 additions and 7 deletions

View File

@ -14,17 +14,17 @@ public sealed class DataScopeContext
public string DataResourceCode { get; init; } = string.Empty; public string DataResourceCode { get; init; } = string.Empty;
public IReadOnlySet<Guid> OrganizationIds { get; init; } = new HashSet<Guid>(); public HashSet<Guid> OrganizationIds { get; init; } = [];
public int DataScope { get; init; } = DataPermConstants.DataScopePersonal; public int DataScope { get; init; } = DataPermConstants.DataScopePersonal;
public IReadOnlyList<int> ResolvedOwnerIds { get; init; } = []; public List<int> ResolvedOwnerIds { get; init; } = [];
public IReadOnlyDictionary<string, string[]>? BizFilters { get; init; } public Dictionary<string, string[]>? BizFilters { get; init; }
public IReadOnlySet<int> SecretLevels { get; init; } = new HashSet<int>(DataPermConstants.DefaultSecretLevels); public HashSet<int> SecretLevels { get; init; } = [.. DataPermConstants.DefaultSecretLevels];
public IReadOnlyList<CompiledFilter> CustomFilters { get; init; } = []; public List<CompiledFilter> CustomFilters { get; init; } = [];
} }
public sealed class CompiledFilter public sealed class CompiledFilter

View File

@ -22,8 +22,16 @@ public class DataPermContextCache(IDistributedCache cache) : IScopedDependency
return null; return null;
} }
try
{
return JsonSerializer.Deserialize<DataScopeContext>(bytes, JsonOptions); return JsonSerializer.Deserialize<DataScopeContext>(bytes, JsonOptions);
} }
catch (JsonException)
{
// 旧缓存或类型变更导致反序列化失败时重建
return null;
}
}
public Task SetAsync(DataScopeContext context, CancellationToken cancellationToken = default) public Task SetAsync(DataScopeContext context, CancellationToken cancellationToken = default)
{ {
@ -212,7 +220,7 @@ public class EffectiveScopeBuilder(DataPermRepository repository, DataPermContex
}) })
.ToList(); .ToList();
IReadOnlyList<int> resolvedOwnerIds = []; List<int> resolvedOwnerIds = [];
if (dataScope is DataPermConstants.DataScopeTeam or DataPermConstants.DataScopeDepartment) if (dataScope is DataPermConstants.DataScopeTeam or DataPermConstants.DataScopeDepartment)
{ {
if (user.OrganizationId is not { } ownerOrg || ownerOrg == Guid.Empty) if (user.OrganizationId is not { } ownerOrg || ownerOrg == Guid.Empty)