- 新增 DataPerm 模块(配置 CRUD、模板、字段脱敏、变更/访问日志 API)及 SQL 脚本 - 订单列表/导出接入 EffectiveScopeBuilder 五维数据权限过滤 - 修复集成测试 JSON 反序列化与 RBAC 用户名解析;新增 DataPerm API 集成测试 Co-authored-by: Cursor <cursoragent@cursor.com>
88 lines
2.5 KiB
C#
88 lines
2.5 KiB
C#
using Fake.DependencyInjection;
|
||
using Fake.Rbac.Infrastructure;
|
||
using Microsoft.EntityFrameworkCore;
|
||
|
||
namespace Oms.Application.Shared;
|
||
|
||
/// <summary>
|
||
/// 将 OMS 审计字段中的 RBAC mapId(user.userid)解析为展示名称。
|
||
/// </summary>
|
||
public class OmsRbacUserDisplayNameResolver : IScopedDependency
|
||
{
|
||
private readonly FakeRbacDbContext _rbacDbContext;
|
||
|
||
public OmsRbacUserDisplayNameResolver(FakeRbacDbContext rbacDbContext)
|
||
{
|
||
_rbacDbContext = rbacDbContext;
|
||
}
|
||
|
||
public async Task<string?> ResolveAsync(int userId, CancellationToken cancellationToken = default)
|
||
{
|
||
if (userId <= 0)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
var map = await ResolveManyAsync([userId], cancellationToken);
|
||
return FormatUserId(userId, map);
|
||
}
|
||
|
||
public async Task<IReadOnlyDictionary<int, string>> ResolveManyAsync(
|
||
IEnumerable<int> userIds,
|
||
CancellationToken cancellationToken = default)
|
||
{
|
||
var ids = userIds.Where(x => x > 0).Distinct().ToList();
|
||
if (ids.Count == 0)
|
||
{
|
||
return new Dictionary<int, string>();
|
||
}
|
||
|
||
try
|
||
{
|
||
var sql =
|
||
$"SELECT `userid` AS `{nameof(RbacUserNameRow.UserId)}`, `Name` AS `{nameof(RbacUserNameRow.DisplayName)}` FROM `user` WHERE `IsDeleted` = 0 AND `userid` IN ({string.Join(",", ids)})";
|
||
var rows = await _rbacDbContext.Database
|
||
.SqlQueryRaw<RbacUserNameRow>(sql)
|
||
.ToListAsync(cancellationToken);
|
||
|
||
return rows.ToDictionary(
|
||
x => x.UserId,
|
||
x => NormalizeDisplayName(x.DisplayName, x.UserId));
|
||
}
|
||
catch
|
||
{
|
||
return ids.ToDictionary(x => x, x => x.ToString());
|
||
}
|
||
}
|
||
|
||
public static string? FormatUserId(int userId, IReadOnlyDictionary<int, string>? nameMap)
|
||
{
|
||
if (userId <= 0)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
if (nameMap != null &&
|
||
nameMap.TryGetValue(userId, out var name) &&
|
||
!string.IsNullOrWhiteSpace(name))
|
||
{
|
||
return name;
|
||
}
|
||
|
||
return userId.ToString();
|
||
}
|
||
|
||
private static string NormalizeDisplayName(string? name, int userId)
|
||
{
|
||
var trimmed = name?.Trim();
|
||
return string.IsNullOrWhiteSpace(trimmed) ? userId.ToString() : trimmed;
|
||
}
|
||
|
||
private sealed class RbacUserNameRow
|
||
{
|
||
public int UserId { get; set; }
|
||
|
||
public string? DisplayName { get; set; }
|
||
}
|
||
}
|