refactor: AdminApi 剔除租户侧能力
This commit is contained in:
@@ -9,7 +9,6 @@ using TakeoutSaaS.Domain.Dictionary.Enums;
|
||||
using TakeoutSaaS.Domain.Dictionary.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.Dictionary.Services;
|
||||
|
||||
@@ -19,8 +18,6 @@ namespace TakeoutSaaS.Application.Dictionary.Services;
|
||||
public sealed class DictionaryAppService(
|
||||
IDictionaryRepository repository,
|
||||
IDictionaryCache cache,
|
||||
ITenantProvider tenantProvider,
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
ILogger<DictionaryAppService> logger) : IDictionaryAppService
|
||||
{
|
||||
/// <summary>
|
||||
@@ -33,7 +30,7 @@ public sealed class DictionaryAppService(
|
||||
{
|
||||
// 1. 规范化编码并确定租户
|
||||
var normalizedCode = NormalizeCode(request.Code);
|
||||
var targetTenant = ResolveTargetTenant(request.Scope);
|
||||
var targetTenant = ResolveTargetTenant(request.Scope, request.TenantId);
|
||||
|
||||
// 2. 校验编码唯一
|
||||
var existing = await repository.FindGroupByCodeAsync(normalizedCode, cancellationToken);
|
||||
@@ -74,7 +71,6 @@ public sealed class DictionaryAppService(
|
||||
{
|
||||
// 1. 读取分组并校验权限
|
||||
var group = await RequireGroupAsync(groupId, cancellationToken);
|
||||
EnsureScopePermission(group.Scope);
|
||||
|
||||
if (request.RowVersion == null || request.RowVersion.Length == 0)
|
||||
{
|
||||
@@ -116,7 +112,6 @@ public sealed class DictionaryAppService(
|
||||
{
|
||||
// 1. 读取分组并校验权限
|
||||
var group = await RequireGroupAsync(groupId, cancellationToken);
|
||||
EnsureScopePermission(group.Scope);
|
||||
|
||||
// 2. 删除并失效缓存
|
||||
await repository.RemoveGroupAsync(group, cancellationToken);
|
||||
@@ -134,9 +129,8 @@ public sealed class DictionaryAppService(
|
||||
public async Task<IReadOnlyList<DictionaryGroupDto>> SearchGroupsAsync(DictionaryGroupQuery request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 确定查询范围并校验权限
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var tenantId = request.TenantId ?? 0;
|
||||
var scope = ResolveScopeForQuery(request.Scope, tenantId);
|
||||
EnsureScopePermission(scope);
|
||||
|
||||
// 2. 查询分组及可选项
|
||||
var groups = await repository.SearchGroupsAsync(scope, cancellationToken);
|
||||
@@ -169,7 +163,6 @@ public sealed class DictionaryAppService(
|
||||
{
|
||||
// 1. 校验分组与权限
|
||||
var group = await RequireGroupAsync(request.GroupId, cancellationToken);
|
||||
EnsureScopePermission(group.Scope);
|
||||
|
||||
// 2. 构建字典项
|
||||
var item = new DictionaryItem
|
||||
@@ -206,7 +199,6 @@ public sealed class DictionaryAppService(
|
||||
// 1. 读取字典项与分组并校验权限
|
||||
var item = await RequireItemAsync(itemId, cancellationToken);
|
||||
var group = await RequireGroupAsync(item.GroupId, cancellationToken);
|
||||
EnsureScopePermission(group.Scope);
|
||||
|
||||
if (request.RowVersion == null || request.RowVersion.Length == 0)
|
||||
{
|
||||
@@ -251,7 +243,6 @@ public sealed class DictionaryAppService(
|
||||
// 1. 读取字典项与分组并校验权限
|
||||
var item = await RequireItemAsync(itemId, cancellationToken);
|
||||
var group = await RequireGroupAsync(item.GroupId, cancellationToken);
|
||||
EnsureScopePermission(group.Scope);
|
||||
|
||||
// 2. 删除并失效缓存
|
||||
await repository.RemoveItemAsync(item, cancellationToken);
|
||||
@@ -281,7 +272,7 @@ public sealed class DictionaryAppService(
|
||||
}
|
||||
|
||||
// 2. 按租户合并系统与业务字典
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var tenantId = request.TenantId ?? 0;
|
||||
var result = new Dictionary<string, IReadOnlyList<DictionaryItemDto>>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var code in normalizedCodes)
|
||||
@@ -324,21 +315,19 @@ public sealed class DictionaryAppService(
|
||||
return item;
|
||||
}
|
||||
|
||||
private long ResolveTargetTenant(DictionaryScope scope)
|
||||
private static long ResolveTargetTenant(DictionaryScope scope, long? tenantId)
|
||||
{
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
if (scope == DictionaryScope.System)
|
||||
{
|
||||
EnsurePlatformTenant(tenantId);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (tenantId == 0)
|
||||
if (!tenantId.HasValue || tenantId.Value <= 0)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "业务参数需指定租户");
|
||||
throw new BusinessException(ErrorCodes.ValidationFailed, "业务参数需指定租户");
|
||||
}
|
||||
|
||||
return tenantId;
|
||||
return tenantId.Value;
|
||||
}
|
||||
|
||||
private static string NormalizeCode(string code) => code.Trim().ToLowerInvariant();
|
||||
@@ -353,23 +342,6 @@ public sealed class DictionaryAppService(
|
||||
return tenantId == 0 ? DictionaryScope.System : DictionaryScope.Business;
|
||||
}
|
||||
|
||||
private void EnsureScopePermission(DictionaryScope scope)
|
||||
{
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
if (scope == DictionaryScope.System && tenantId != 0 && !DictionaryAccessHelper.IsPlatformAdmin(httpContextAccessor))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.Forbidden, "仅平台管理员可操作系统字典");
|
||||
}
|
||||
}
|
||||
|
||||
private void EnsurePlatformTenant(long tenantId)
|
||||
{
|
||||
if (tenantId != 0 && !DictionaryAccessHelper.IsPlatformAdmin(httpContextAccessor))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.Forbidden, "仅平台管理员可操作系统字典");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task InvalidateCacheAsync(DictionaryGroup group, CancellationToken cancellationToken)
|
||||
{
|
||||
await cache.RemoveAsync(group.TenantId, group.Code, cancellationToken);
|
||||
|
||||
Reference in New Issue
Block a user