feat: 角色模板改为数据库管理支持前端自定义
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using TakeoutSaaS.Application.Identity.Templates;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// 角色模板提供者,用于获取预置模板定义。
|
||||
/// </summary>
|
||||
public interface IRoleTemplateProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取全部角色模板定义。
|
||||
/// </summary>
|
||||
/// <returns>模板定义集合。</returns>
|
||||
IReadOnlyList<RoleTemplateDefinition> GetTemplates();
|
||||
|
||||
/// <summary>
|
||||
/// 根据模板编码查找模板。
|
||||
/// </summary>
|
||||
/// <param name="templateCode">模板编码。</param>
|
||||
/// <returns>模板定义;不存在时返回 null。</returns>
|
||||
RoleTemplateDefinition? FindByCode(string templateCode);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Collections.Generic;
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.Identity.Contracts;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 创建角色模板命令。
|
||||
/// </summary>
|
||||
public sealed record CreateRoleTemplateCommand : IRequest<RoleTemplateDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// 模板编码。
|
||||
/// </summary>
|
||||
public string TemplateCode { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 模板名称。
|
||||
/// </summary>
|
||||
public string Name { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 模板描述。
|
||||
/// </summary>
|
||||
public string? Description { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用。
|
||||
/// </summary>
|
||||
public bool IsActive { get; init; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 权限编码集合。
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<string> PermissionCodes { get; init; } = Array.Empty<string>();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using MediatR;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 删除角色模板命令。
|
||||
/// </summary>
|
||||
public sealed record DeleteRoleTemplateCommand : IRequest<bool>
|
||||
{
|
||||
/// <summary>
|
||||
/// 模板编码。
|
||||
/// </summary>
|
||||
public string TemplateCode { get; init; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Collections.Generic;
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.Identity.Contracts;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 更新角色模板命令。
|
||||
/// </summary>
|
||||
public sealed record UpdateRoleTemplateCommand : IRequest<RoleTemplateDto?>
|
||||
{
|
||||
/// <summary>
|
||||
/// 模板编码(路径参数)。
|
||||
/// </summary>
|
||||
public string TemplateCode { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 模板名称。
|
||||
/// </summary>
|
||||
public string Name { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 模板描述。
|
||||
/// </summary>
|
||||
public string? Description { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用。
|
||||
/// </summary>
|
||||
public bool IsActive { get; init; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 权限编码集合。
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<string> PermissionCodes { get; init; } = Array.Empty<string>();
|
||||
}
|
||||
@@ -22,6 +22,11 @@ public sealed record RoleTemplateDto
|
||||
/// </summary>
|
||||
public string? Description { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用。
|
||||
/// </summary>
|
||||
public bool IsActive { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 包含的权限定义。
|
||||
/// </summary>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using TakeoutSaaS.Application.Identity.Abstractions;
|
||||
using TakeoutSaaS.Application.Identity.Services;
|
||||
using TakeoutSaaS.Application.Identity.Templates;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Extensions;
|
||||
|
||||
@@ -18,7 +17,6 @@ public static class IdentityServiceCollectionExtensions
|
||||
public static IServiceCollection AddIdentityApplication(this IServiceCollection services, bool enableMiniSupport = false)
|
||||
{
|
||||
services.AddScoped<IAdminAuthService, AdminAuthService>();
|
||||
services.AddSingleton<IRoleTemplateProvider, RoleTemplateProvider>();
|
||||
|
||||
if (enableMiniSupport)
|
||||
{
|
||||
|
||||
@@ -2,7 +2,6 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.Identity.Abstractions;
|
||||
using TakeoutSaaS.Application.Identity.Commands;
|
||||
using TakeoutSaaS.Application.Identity.Contracts;
|
||||
using TakeoutSaaS.Domain.Identity.Entities;
|
||||
@@ -17,7 +16,7 @@ namespace TakeoutSaaS.Application.Identity.Handlers;
|
||||
/// 角色模板复制处理器。
|
||||
/// </summary>
|
||||
public sealed class CopyRoleTemplateCommandHandler(
|
||||
IRoleTemplateProvider roleTemplateProvider,
|
||||
IRoleTemplateRepository roleTemplateRepository,
|
||||
IRoleRepository roleRepository,
|
||||
IPermissionRepository permissionRepository,
|
||||
IRolePermissionRepository rolePermissionRepository,
|
||||
@@ -27,9 +26,16 @@ public sealed class CopyRoleTemplateCommandHandler(
|
||||
/// <inheritdoc />
|
||||
public async Task<RoleDto> Handle(CopyRoleTemplateCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var template = roleTemplateProvider.FindByCode(request.TemplateCode)
|
||||
var template = await roleTemplateRepository.FindByCodeAsync(request.TemplateCode, cancellationToken)
|
||||
?? throw new BusinessException(ErrorCodes.NotFound, $"角色模板 {request.TemplateCode} 不存在");
|
||||
|
||||
var templatePermissions = await roleTemplateRepository.GetPermissionsAsync(template.Id, cancellationToken);
|
||||
var permissionCodes = templatePermissions
|
||||
.Select(x => x.PermissionCode)
|
||||
.Where(code => !string.IsNullOrWhiteSpace(code))
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var roleCode = string.IsNullOrWhiteSpace(request.RoleCode) ? template.TemplateCode : request.RoleCode.Trim();
|
||||
var roleName = string.IsNullOrWhiteSpace(request.RoleName) ? template.Name : request.RoleName.Trim();
|
||||
@@ -64,18 +70,12 @@ public sealed class CopyRoleTemplateCommandHandler(
|
||||
}
|
||||
|
||||
// 2. 确保模板权限全部存在,不存在则按模板定义创建。
|
||||
var targetPermissionCodes = template.Permissions
|
||||
.Select(permission => permission.Code)
|
||||
.Where(code => !string.IsNullOrWhiteSpace(code))
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
|
||||
var existingPermissions = await permissionRepository.GetByCodesAsync(tenantId, targetPermissionCodes, cancellationToken);
|
||||
var existingPermissions = await permissionRepository.GetByCodesAsync(tenantId, permissionCodes, cancellationToken);
|
||||
var permissionMap = existingPermissions.ToDictionary(x => x.Code, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var permissionDefinition in template.Permissions)
|
||||
foreach (var code in permissionCodes)
|
||||
{
|
||||
if (permissionMap.ContainsKey(permissionDefinition.Code))
|
||||
if (permissionMap.ContainsKey(code))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -83,13 +83,13 @@ public sealed class CopyRoleTemplateCommandHandler(
|
||||
var permission = new Permission
|
||||
{
|
||||
TenantId = tenantId,
|
||||
Name = permissionDefinition.Name,
|
||||
Code = permissionDefinition.Code,
|
||||
Description = permissionDefinition.Description
|
||||
Name = code,
|
||||
Code = code,
|
||||
Description = code
|
||||
};
|
||||
|
||||
await permissionRepository.AddAsync(permission, cancellationToken);
|
||||
permissionMap[permissionDefinition.Code] = permission;
|
||||
permissionMap[code] = permission;
|
||||
}
|
||||
|
||||
await roleRepository.SaveChangesAsync(cancellationToken);
|
||||
@@ -100,7 +100,7 @@ public sealed class CopyRoleTemplateCommandHandler(
|
||||
.Select(x => x.PermissionId)
|
||||
.ToHashSet();
|
||||
|
||||
var targetPermissionIds = targetPermissionCodes
|
||||
var targetPermissionIds = permissionCodes
|
||||
.Select(code => permissionMap[code].Id)
|
||||
.ToHashSet();
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.Identity.Commands;
|
||||
using TakeoutSaaS.Application.Identity.Contracts;
|
||||
using TakeoutSaaS.Domain.Identity.Entities;
|
||||
using TakeoutSaaS.Domain.Identity.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 创建角色模板处理器。
|
||||
/// </summary>
|
||||
public sealed class CreateRoleTemplateCommandHandler(IRoleTemplateRepository roleTemplateRepository)
|
||||
: IRequestHandler<CreateRoleTemplateCommand, RoleTemplateDto>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<RoleTemplateDto> Handle(CreateRoleTemplateCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request.TemplateCode) || string.IsNullOrWhiteSpace(request.Name))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "模板编码与名称不能为空");
|
||||
}
|
||||
|
||||
var existing = await roleTemplateRepository.FindByCodeAsync(request.TemplateCode, cancellationToken);
|
||||
if (existing != null)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.Conflict, $"模板编码 {request.TemplateCode} 已存在");
|
||||
}
|
||||
|
||||
var template = new RoleTemplate
|
||||
{
|
||||
TemplateCode = request.TemplateCode.Trim(),
|
||||
Name = request.Name.Trim(),
|
||||
Description = request.Description,
|
||||
IsActive = request.IsActive
|
||||
};
|
||||
|
||||
var permissions = request.PermissionCodes
|
||||
.Where(code => !string.IsNullOrWhiteSpace(code))
|
||||
.Select(code => code.Trim())
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
|
||||
await roleTemplateRepository.AddAsync(template, permissions, cancellationToken);
|
||||
await roleTemplateRepository.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return TemplateMapper.ToDto(template, permissions);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.Identity.Commands;
|
||||
using TakeoutSaaS.Domain.Identity.Repositories;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 删除角色模板处理器。
|
||||
/// </summary>
|
||||
public sealed class DeleteRoleTemplateCommandHandler(IRoleTemplateRepository roleTemplateRepository)
|
||||
: IRequestHandler<DeleteRoleTemplateCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(DeleteRoleTemplateCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var template = await roleTemplateRepository.FindByCodeAsync(request.TemplateCode, cancellationToken);
|
||||
if (template == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
await roleTemplateRepository.DeleteAsync(template.Id, cancellationToken);
|
||||
await roleTemplateRepository.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,28 @@
|
||||
using System.Linq;
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.Identity.Abstractions;
|
||||
using TakeoutSaaS.Application.Identity.Contracts;
|
||||
using TakeoutSaaS.Application.Identity.Queries;
|
||||
using TakeoutSaaS.Domain.Identity.Repositories;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 角色模板详情查询处理器。
|
||||
/// </summary>
|
||||
public sealed class GetRoleTemplateQueryHandler(IRoleTemplateProvider roleTemplateProvider)
|
||||
public sealed class GetRoleTemplateQueryHandler(IRoleTemplateRepository roleTemplateRepository)
|
||||
: IRequestHandler<GetRoleTemplateQuery, RoleTemplateDto?>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public Task<RoleTemplateDto?> Handle(GetRoleTemplateQuery request, CancellationToken cancellationToken)
|
||||
public async Task<RoleTemplateDto?> Handle(GetRoleTemplateQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var template = roleTemplateProvider.FindByCode(request.TemplateCode);
|
||||
return Task.FromResult(template is null ? null : TemplateMapper.ToDto(template));
|
||||
var template = await roleTemplateRepository.FindByCodeAsync(request.TemplateCode, cancellationToken);
|
||||
if (template == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var permissions = await roleTemplateRepository.GetPermissionsAsync(template.Id, cancellationToken);
|
||||
var codes = permissions.Select(x => x.PermissionCode).ToArray();
|
||||
return TemplateMapper.ToDto(template, codes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.Identity.Abstractions;
|
||||
using TakeoutSaaS.Application.Identity.Commands;
|
||||
using TakeoutSaaS.Application.Identity.Contracts;
|
||||
using TakeoutSaaS.Domain.Identity.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
|
||||
@@ -14,39 +14,47 @@ namespace TakeoutSaaS.Application.Identity.Handlers;
|
||||
/// 租户角色模板批量初始化处理器。
|
||||
/// </summary>
|
||||
public sealed class InitializeRoleTemplatesCommandHandler(
|
||||
IRoleTemplateProvider roleTemplateProvider,
|
||||
IRoleTemplateRepository roleTemplateRepository,
|
||||
IMediator mediator)
|
||||
: IRequestHandler<InitializeRoleTemplatesCommand, IReadOnlyList<RoleDto>>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<RoleDto>> Handle(InitializeRoleTemplatesCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 解析需要初始化的模板编码,默认取全部预置模板。
|
||||
// 1. 解析需要初始化的模板编码,默认取全部模板。
|
||||
var requestedCodes = request.TemplateCodes?
|
||||
.Where(code => !string.IsNullOrWhiteSpace(code))
|
||||
.Select(code => code.Trim())
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
|
||||
var availableTemplates = await roleTemplateRepository.GetAllAsync(true, cancellationToken);
|
||||
var availableCodes = availableTemplates.Select(t => t.TemplateCode).ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var targetCodes = requestedCodes?.Length > 0
|
||||
? requestedCodes
|
||||
: roleTemplateProvider.GetTemplates().Select(template => template.TemplateCode).ToArray();
|
||||
: availableTemplates.Select(template => template.TemplateCode).ToArray();
|
||||
|
||||
if (targetCodes.Length == 0)
|
||||
{
|
||||
return Array.Empty<RoleDto>();
|
||||
}
|
||||
|
||||
foreach (var code in targetCodes)
|
||||
{
|
||||
if (!availableCodes.Contains(code))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, $"角色模板 {code} 不存在或未启用");
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 逐个复制模板,幂等写入角色与权限。
|
||||
var roles = new List<RoleDto>(targetCodes.Length);
|
||||
foreach (var templateCode in targetCodes)
|
||||
{
|
||||
var template = roleTemplateProvider.FindByCode(templateCode)
|
||||
?? throw new BusinessException(ErrorCodes.NotFound, $"角色模板 {templateCode} 不存在");
|
||||
|
||||
var role = await mediator.Send(new CopyRoleTemplateCommand
|
||||
{
|
||||
TemplateCode = template.TemplateCode
|
||||
TemplateCode = templateCode
|
||||
}, cancellationToken);
|
||||
|
||||
roles.Add(role);
|
||||
|
||||
@@ -1,26 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.Identity.Abstractions;
|
||||
using TakeoutSaaS.Application.Identity.Contracts;
|
||||
using TakeoutSaaS.Application.Identity.Queries;
|
||||
using TakeoutSaaS.Domain.Identity.Repositories;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 角色模板列表查询处理器。
|
||||
/// </summary>
|
||||
public sealed class ListRoleTemplatesQueryHandler(IRoleTemplateProvider roleTemplateProvider)
|
||||
public sealed class ListRoleTemplatesQueryHandler(IRoleTemplateRepository roleTemplateRepository)
|
||||
: IRequestHandler<ListRoleTemplatesQuery, IReadOnlyList<RoleTemplateDto>>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public Task<IReadOnlyList<RoleTemplateDto>> Handle(ListRoleTemplatesQuery request, CancellationToken cancellationToken)
|
||||
public async Task<IReadOnlyList<RoleTemplateDto>> Handle(ListRoleTemplatesQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var templates = roleTemplateProvider.GetTemplates()
|
||||
var templates = await roleTemplateRepository.GetAllAsync(request.IsActive, cancellationToken);
|
||||
var permissionsMap = await roleTemplateRepository.GetPermissionsAsync(templates.Select(t => t.Id), cancellationToken);
|
||||
|
||||
var dtos = templates
|
||||
.OrderBy(template => template.TemplateCode, StringComparer.OrdinalIgnoreCase)
|
||||
.Select(TemplateMapper.ToDto)
|
||||
.Select(template =>
|
||||
{
|
||||
var codes = permissionsMap.TryGetValue(template.Id, out var perms)
|
||||
? (IReadOnlyCollection<string>)perms.Select(p => p.PermissionCode).ToArray()
|
||||
: Array.Empty<string>();
|
||||
return TemplateMapper.ToDto(template, codes);
|
||||
})
|
||||
.ToArray();
|
||||
|
||||
return Task.FromResult<IReadOnlyList<RoleTemplateDto>>(templates);
|
||||
return dtos;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using TakeoutSaaS.Application.Identity.Contracts;
|
||||
using TakeoutSaaS.Application.Identity.Templates;
|
||||
using TakeoutSaaS.Domain.Identity.Entities;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Handlers;
|
||||
|
||||
@@ -10,28 +11,27 @@ namespace TakeoutSaaS.Application.Identity.Handlers;
|
||||
internal static class TemplateMapper
|
||||
{
|
||||
/// <summary>
|
||||
/// 将角色模板定义映射为 DTO。
|
||||
/// 将角色模板与权限编码集合映射为 DTO。
|
||||
/// </summary>
|
||||
/// <param name="definition">角色模板定义。</param>
|
||||
/// <param name="template">角色模板实体。</param>
|
||||
/// <param name="permissionCodes">权限编码集合。</param>
|
||||
/// <returns>模板 DTO。</returns>
|
||||
public static RoleTemplateDto ToDto(RoleTemplateDefinition definition)
|
||||
public static RoleTemplateDto ToDto(RoleTemplate template, IReadOnlyCollection<string> permissionCodes)
|
||||
{
|
||||
return new RoleTemplateDto
|
||||
{
|
||||
TemplateCode = definition.TemplateCode,
|
||||
Name = definition.Name,
|
||||
Description = definition.Description,
|
||||
Permissions = definition.Permissions.Select(ToDto).ToArray()
|
||||
};
|
||||
}
|
||||
|
||||
private static PermissionTemplateDto ToDto(PermissionTemplateDefinition definition)
|
||||
{
|
||||
return new PermissionTemplateDto
|
||||
{
|
||||
Code = definition.Code,
|
||||
Name = definition.Name,
|
||||
Description = definition.Description
|
||||
TemplateCode = template.TemplateCode,
|
||||
Name = template.Name,
|
||||
Description = template.Description,
|
||||
IsActive = template.IsActive,
|
||||
Permissions = permissionCodes
|
||||
.Select(code => new PermissionTemplateDto
|
||||
{
|
||||
Code = code,
|
||||
Name = code,
|
||||
Description = null
|
||||
})
|
||||
.ToArray()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.Identity.Commands;
|
||||
using TakeoutSaaS.Application.Identity.Contracts;
|
||||
using TakeoutSaaS.Domain.Identity.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 更新角色模板处理器。
|
||||
/// </summary>
|
||||
public sealed class UpdateRoleTemplateCommandHandler(IRoleTemplateRepository roleTemplateRepository)
|
||||
: IRequestHandler<UpdateRoleTemplateCommand, RoleTemplateDto?>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<RoleTemplateDto?> Handle(UpdateRoleTemplateCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request.TemplateCode) || string.IsNullOrWhiteSpace(request.Name))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "模板编码与名称不能为空");
|
||||
}
|
||||
|
||||
var template = await roleTemplateRepository.FindByCodeAsync(request.TemplateCode, cancellationToken);
|
||||
if (template == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
template.Name = request.Name.Trim();
|
||||
template.Description = request.Description;
|
||||
template.IsActive = request.IsActive;
|
||||
|
||||
var permissions = request.PermissionCodes
|
||||
.Where(code => !string.IsNullOrWhiteSpace(code))
|
||||
.Select(code => code.Trim())
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
|
||||
await roleTemplateRepository.UpdateAsync(template, permissions, cancellationToken);
|
||||
await roleTemplateRepository.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return TemplateMapper.ToDto(template, permissions);
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,10 @@ namespace TakeoutSaaS.Application.Identity.Queries;
|
||||
/// <summary>
|
||||
/// 查询角色模板列表。
|
||||
/// </summary>
|
||||
public sealed record ListRoleTemplatesQuery : IRequest<IReadOnlyList<RoleTemplateDto>>;
|
||||
public sealed record ListRoleTemplatesQuery : IRequest<IReadOnlyList<RoleTemplateDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否仅返回启用模板。
|
||||
/// </summary>
|
||||
public bool? IsActive { get; init; }
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
namespace TakeoutSaaS.Application.Identity.Templates;
|
||||
|
||||
/// <summary>
|
||||
/// 权限模板定义。
|
||||
/// </summary>
|
||||
public sealed record PermissionTemplateDefinition
|
||||
{
|
||||
/// <summary>
|
||||
/// 权限编码(唯一键)。
|
||||
/// </summary>
|
||||
public required string Code { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 权限名称。
|
||||
/// </summary>
|
||||
public required string Name { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 权限描述。
|
||||
/// </summary>
|
||||
public string? Description { get; init; }
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Templates;
|
||||
|
||||
/// <summary>
|
||||
/// 角色模板定义。
|
||||
/// </summary>
|
||||
public sealed record RoleTemplateDefinition
|
||||
{
|
||||
/// <summary>
|
||||
/// 模板编码(唯一键)。
|
||||
/// </summary>
|
||||
public required string TemplateCode { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 角色名称。
|
||||
/// </summary>
|
||||
public required string Name { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 角色描述。
|
||||
/// </summary>
|
||||
public string? Description { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 模板绑定的权限集合。
|
||||
/// </summary>
|
||||
public IReadOnlyList<PermissionTemplateDefinition> Permissions { get; init; } = [];
|
||||
}
|
||||
@@ -1,511 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Frozen;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using TakeoutSaaS.Application.Identity.Abstractions;
|
||||
|
||||
namespace TakeoutSaaS.Application.Identity.Templates;
|
||||
|
||||
/// <summary>
|
||||
/// 预置角色模板提供者。
|
||||
/// </summary>
|
||||
public sealed class RoleTemplateProvider : IRoleTemplateProvider
|
||||
{
|
||||
private static readonly FrozenDictionary<string, PermissionTemplateDefinition> PermissionDefinitions =
|
||||
new Dictionary<string, PermissionTemplateDefinition>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
["identity:role:read"] = new()
|
||||
{
|
||||
Code = "identity:role:read",
|
||||
Name = "角色查询",
|
||||
Description = "查看当前租户的角色列表与详情。"
|
||||
},
|
||||
["identity:role:create"] = new()
|
||||
{
|
||||
Code = "identity:role:create",
|
||||
Name = "角色创建",
|
||||
Description = "在当前租户内创建新角色。"
|
||||
},
|
||||
["identity:role:update"] = new()
|
||||
{
|
||||
Code = "identity:role:update",
|
||||
Name = "角色更新",
|
||||
Description = "修改角色名称与描述。"
|
||||
},
|
||||
["identity:role:delete"] = new()
|
||||
{
|
||||
Code = "identity:role:delete",
|
||||
Name = "角色删除",
|
||||
Description = "删除租户内的角色记录。"
|
||||
},
|
||||
["identity:role:bind-permission"] = new()
|
||||
{
|
||||
Code = "identity:role:bind-permission",
|
||||
Name = "角色权限绑定",
|
||||
Description = "为角色绑定或调整权限集合。"
|
||||
},
|
||||
["identity:permission:read"] = new()
|
||||
{
|
||||
Code = "identity:permission:read",
|
||||
Name = "权限查询",
|
||||
Description = "查看权限列表与详情。"
|
||||
},
|
||||
["identity:permission:create"] = new()
|
||||
{
|
||||
Code = "identity:permission:create",
|
||||
Name = "权限创建",
|
||||
Description = "创建新的权限定义。"
|
||||
},
|
||||
["identity:permission:update"] = new()
|
||||
{
|
||||
Code = "identity:permission:update",
|
||||
Name = "权限更新",
|
||||
Description = "修改权限名称或描述。"
|
||||
},
|
||||
["identity:permission:delete"] = new()
|
||||
{
|
||||
Code = "identity:permission:delete",
|
||||
Name = "权限删除",
|
||||
Description = "删除租户内的权限记录。"
|
||||
},
|
||||
["identity:profile:read"] = new()
|
||||
{
|
||||
Code = "identity:profile:read",
|
||||
Name = "个人信息查看",
|
||||
Description = "查看当前登录人的账号与权限信息。"
|
||||
},
|
||||
["tenant:create"] = new()
|
||||
{
|
||||
Code = "tenant:create",
|
||||
Name = "租户创建",
|
||||
Description = "创建新的租户记录。"
|
||||
},
|
||||
["tenant:read"] = new()
|
||||
{
|
||||
Code = "tenant:read",
|
||||
Name = "租户查询",
|
||||
Description = "查看租户列表与详情。"
|
||||
},
|
||||
["tenant:review"] = new()
|
||||
{
|
||||
Code = "tenant:review",
|
||||
Name = "租户审核",
|
||||
Description = "审核租户实名与资质信息。"
|
||||
},
|
||||
["tenant:subscription"] = new()
|
||||
{
|
||||
Code = "tenant:subscription",
|
||||
Name = "租户订阅管理",
|
||||
Description = "创建、续费或调整租户套餐订阅。"
|
||||
},
|
||||
["tenant:quota:check"] = new()
|
||||
{
|
||||
Code = "tenant:quota:check",
|
||||
Name = "租户配额校验",
|
||||
Description = "校验并占用门店、账号、短信、配送等配额。"
|
||||
},
|
||||
["tenant-package:read"] = new()
|
||||
{
|
||||
Code = "tenant-package:read",
|
||||
Name = "套餐查询",
|
||||
Description = "查看平台套餐定义列表与详情。"
|
||||
},
|
||||
["tenant-package:create"] = new()
|
||||
{
|
||||
Code = "tenant-package:create",
|
||||
Name = "套餐创建",
|
||||
Description = "创建平台套餐定义。"
|
||||
},
|
||||
["tenant-package:update"] = new()
|
||||
{
|
||||
Code = "tenant-package:update",
|
||||
Name = "套餐更新",
|
||||
Description = "更新平台套餐定义。"
|
||||
},
|
||||
["tenant-package:delete"] = new()
|
||||
{
|
||||
Code = "tenant-package:delete",
|
||||
Name = "套餐删除",
|
||||
Description = "删除平台套餐定义。"
|
||||
},
|
||||
["merchant:create"] = new()
|
||||
{
|
||||
Code = "merchant:create",
|
||||
Name = "商户创建",
|
||||
Description = "创建或提交商户入驻信息。"
|
||||
},
|
||||
["merchant:read"] = new()
|
||||
{
|
||||
Code = "merchant:read",
|
||||
Name = "商户查看",
|
||||
Description = "查看商户资料与审核状态。"
|
||||
},
|
||||
["merchant:update"] = new()
|
||||
{
|
||||
Code = "merchant:update",
|
||||
Name = "商户更新",
|
||||
Description = "更新商户资料或合同等信息。"
|
||||
},
|
||||
["merchant:delete"] = new()
|
||||
{
|
||||
Code = "merchant:delete",
|
||||
Name = "商户删除",
|
||||
Description = "删除或作废商户记录。"
|
||||
},
|
||||
["merchant:review"] = new()
|
||||
{
|
||||
Code = "merchant:review",
|
||||
Name = "商户审核",
|
||||
Description = "审核商户入驻、合同与证照。"
|
||||
},
|
||||
["merchant_category:read"] = new()
|
||||
{
|
||||
Code = "merchant_category:read",
|
||||
Name = "类目查询",
|
||||
Description = "查看经营类目列表。"
|
||||
},
|
||||
["merchant_category:create"] = new()
|
||||
{
|
||||
Code = "merchant_category:create",
|
||||
Name = "类目创建",
|
||||
Description = "新增经营类目。"
|
||||
},
|
||||
["merchant_category:update"] = new()
|
||||
{
|
||||
Code = "merchant_category:update",
|
||||
Name = "类目更新",
|
||||
Description = "调整经营类目名称或顺序。"
|
||||
},
|
||||
["merchant_category:delete"] = new()
|
||||
{
|
||||
Code = "merchant_category:delete",
|
||||
Name = "类目删除",
|
||||
Description = "删除经营类目。"
|
||||
},
|
||||
["store:create"] = new()
|
||||
{
|
||||
Code = "store:create",
|
||||
Name = "门店创建",
|
||||
Description = "创建新的门店记录。"
|
||||
},
|
||||
["store:read"] = new()
|
||||
{
|
||||
Code = "store:read",
|
||||
Name = "门店查看",
|
||||
Description = "查看门店列表与详情。"
|
||||
},
|
||||
["store:update"] = new()
|
||||
{
|
||||
Code = "store:update",
|
||||
Name = "门店更新",
|
||||
Description = "更新门店资料与配置。"
|
||||
},
|
||||
["store:delete"] = new()
|
||||
{
|
||||
Code = "store:delete",
|
||||
Name = "门店删除",
|
||||
Description = "删除或停用门店。"
|
||||
},
|
||||
["product:create"] = new()
|
||||
{
|
||||
Code = "product:create",
|
||||
Name = "商品创建",
|
||||
Description = "创建商品、菜品或规格。"
|
||||
},
|
||||
["product:read"] = new()
|
||||
{
|
||||
Code = "product:read",
|
||||
Name = "商品查看",
|
||||
Description = "查看商品/菜品列表与详情。"
|
||||
},
|
||||
["product:update"] = new()
|
||||
{
|
||||
Code = "product:update",
|
||||
Name = "商品更新",
|
||||
Description = "更新商品、菜品或上下架状态。"
|
||||
},
|
||||
["product:delete"] = new()
|
||||
{
|
||||
Code = "product:delete",
|
||||
Name = "商品删除",
|
||||
Description = "删除商品或菜品。"
|
||||
},
|
||||
["order:create"] = new()
|
||||
{
|
||||
Code = "order:create",
|
||||
Name = "订单创建",
|
||||
Description = "创建订单或手工录单。"
|
||||
},
|
||||
["order:read"] = new()
|
||||
{
|
||||
Code = "order:read",
|
||||
Name = "订单查看",
|
||||
Description = "查看订单列表与详情。"
|
||||
},
|
||||
["order:update"] = new()
|
||||
{
|
||||
Code = "order:update",
|
||||
Name = "订单更新",
|
||||
Description = "修改订单状态或履约信息。"
|
||||
},
|
||||
["order:delete"] = new()
|
||||
{
|
||||
Code = "order:delete",
|
||||
Name = "订单删除",
|
||||
Description = "删除或作废订单。"
|
||||
},
|
||||
["delivery:create"] = new()
|
||||
{
|
||||
Code = "delivery:create",
|
||||
Name = "配送创建",
|
||||
Description = "创建或发起配送单。"
|
||||
},
|
||||
["delivery:read"] = new()
|
||||
{
|
||||
Code = "delivery:read",
|
||||
Name = "配送查看",
|
||||
Description = "查看配送订单与轨迹。"
|
||||
},
|
||||
["delivery:update"] = new()
|
||||
{
|
||||
Code = "delivery:update",
|
||||
Name = "配送更新",
|
||||
Description = "更新配送状态或骑手信息。"
|
||||
},
|
||||
["delivery:delete"] = new()
|
||||
{
|
||||
Code = "delivery:delete",
|
||||
Name = "配送删除",
|
||||
Description = "取消或删除配送单。"
|
||||
},
|
||||
["payment:create"] = new()
|
||||
{
|
||||
Code = "payment:create",
|
||||
Name = "支付创建",
|
||||
Description = "创建收款或支付记录。"
|
||||
},
|
||||
["payment:read"] = new()
|
||||
{
|
||||
Code = "payment:read",
|
||||
Name = "支付查看",
|
||||
Description = "查看支付、退款记录。"
|
||||
},
|
||||
["payment:update"] = new()
|
||||
{
|
||||
Code = "payment:update",
|
||||
Name = "支付更新",
|
||||
Description = "更新支付状态或补充信息。"
|
||||
},
|
||||
["payment:delete"] = new()
|
||||
{
|
||||
Code = "payment:delete",
|
||||
Name = "支付删除",
|
||||
Description = "删除或作废支付记录。"
|
||||
},
|
||||
["dictionary:group:read"] = new()
|
||||
{
|
||||
Code = "dictionary:group:read",
|
||||
Name = "字典分组查询",
|
||||
Description = "查看字典分组与明细。"
|
||||
},
|
||||
["dictionary:group:create"] = new()
|
||||
{
|
||||
Code = "dictionary:group:create",
|
||||
Name = "字典分组创建",
|
||||
Description = "新增字典分组。"
|
||||
},
|
||||
["dictionary:group:update"] = new()
|
||||
{
|
||||
Code = "dictionary:group:update",
|
||||
Name = "字典分组更新",
|
||||
Description = "修改字典分组信息。"
|
||||
},
|
||||
["dictionary:group:delete"] = new()
|
||||
{
|
||||
Code = "dictionary:group:delete",
|
||||
Name = "字典分组删除",
|
||||
Description = "删除字典分组。"
|
||||
},
|
||||
["dictionary:item:create"] = new()
|
||||
{
|
||||
Code = "dictionary:item:create",
|
||||
Name = "字典项创建",
|
||||
Description = "新增字典项。"
|
||||
},
|
||||
["dictionary:item:update"] = new()
|
||||
{
|
||||
Code = "dictionary:item:update",
|
||||
Name = "字典项更新",
|
||||
Description = "调整字典项内容。"
|
||||
},
|
||||
["dictionary:item:delete"] = new()
|
||||
{
|
||||
Code = "dictionary:item:delete",
|
||||
Name = "字典项删除",
|
||||
Description = "删除字典项。"
|
||||
},
|
||||
["system-parameter:create"] = new()
|
||||
{
|
||||
Code = "system-parameter:create",
|
||||
Name = "系统参数创建",
|
||||
Description = "新增系统参数配置。"
|
||||
},
|
||||
["system-parameter:read"] = new()
|
||||
{
|
||||
Code = "system-parameter:read",
|
||||
Name = "系统参数查询",
|
||||
Description = "查看系统参数列表与详情。"
|
||||
},
|
||||
["system-parameter:update"] = new()
|
||||
{
|
||||
Code = "system-parameter:update",
|
||||
Name = "系统参数更新",
|
||||
Description = "更新系统参数配置。"
|
||||
},
|
||||
["system-parameter:delete"] = new()
|
||||
{
|
||||
Code = "system-parameter:delete",
|
||||
Name = "系统参数删除",
|
||||
Description = "删除系统参数配置。"
|
||||
}
|
||||
}.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
private static readonly FrozenDictionary<string, RoleTemplateDefinition> Templates =
|
||||
new Dictionary<string, RoleTemplateDefinition>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
["platform-admin"] = new()
|
||||
{
|
||||
TemplateCode = "platform-admin",
|
||||
Name = "平台管理员",
|
||||
Description = "平台全量权限,负责租户、商户、配置与运维。",
|
||||
Permissions = BuildPermissions(PermissionDefinitions.Keys)
|
||||
},
|
||||
["tenant-admin"] = new()
|
||||
{
|
||||
TemplateCode = "tenant-admin",
|
||||
Name = "租户管理员",
|
||||
Description = "管理本租户的门店、商品、订单与团队权限。",
|
||||
Permissions = BuildPermissions(new[]
|
||||
{
|
||||
"identity:profile:read",
|
||||
"identity:role:read",
|
||||
"identity:role:create",
|
||||
"identity:role:update",
|
||||
"identity:role:delete",
|
||||
"identity:role:bind-permission",
|
||||
"identity:permission:read",
|
||||
"identity:permission:create",
|
||||
"identity:permission:update",
|
||||
"identity:permission:delete",
|
||||
"tenant:read",
|
||||
"tenant:subscription",
|
||||
"tenant:quota:check",
|
||||
"merchant:read",
|
||||
"merchant:update",
|
||||
"merchant_category:read",
|
||||
"merchant_category:create",
|
||||
"merchant_category:update",
|
||||
"merchant_category:delete",
|
||||
"store:create",
|
||||
"store:read",
|
||||
"store:update",
|
||||
"store:delete",
|
||||
"product:create",
|
||||
"product:read",
|
||||
"product:update",
|
||||
"product:delete",
|
||||
"order:create",
|
||||
"order:read",
|
||||
"order:update",
|
||||
"delivery:create",
|
||||
"delivery:read",
|
||||
"delivery:update",
|
||||
"payment:create",
|
||||
"payment:read",
|
||||
"payment:update",
|
||||
"dictionary:group:read",
|
||||
"dictionary:group:create",
|
||||
"dictionary:group:update",
|
||||
"dictionary:group:delete",
|
||||
"dictionary:item:create",
|
||||
"dictionary:item:update",
|
||||
"dictionary:item:delete",
|
||||
"system-parameter:read"
|
||||
})
|
||||
},
|
||||
["store-manager"] = new()
|
||||
{
|
||||
TemplateCode = "store-manager",
|
||||
Name = "店长",
|
||||
Description = "负责门店日常运营、商品与订单管理。",
|
||||
Permissions = BuildPermissions(new[]
|
||||
{
|
||||
"identity:profile:read",
|
||||
"store:read",
|
||||
"store:update",
|
||||
"product:create",
|
||||
"product:read",
|
||||
"product:update",
|
||||
"order:create",
|
||||
"order:read",
|
||||
"order:update",
|
||||
"delivery:read",
|
||||
"delivery:update",
|
||||
"payment:read",
|
||||
"payment:update",
|
||||
"dictionary:group:read",
|
||||
"dictionary:item:create",
|
||||
"dictionary:item:update",
|
||||
"dictionary:item:delete"
|
||||
})
|
||||
},
|
||||
["store-staff"] = new()
|
||||
{
|
||||
TemplateCode = "store-staff",
|
||||
Name = "店员",
|
||||
Description = "处理订单履约、配送跟踪与收款查询。",
|
||||
Permissions = BuildPermissions(new[]
|
||||
{
|
||||
"identity:profile:read",
|
||||
"store:read",
|
||||
"product:read",
|
||||
"order:read",
|
||||
"order:update",
|
||||
"delivery:read",
|
||||
"payment:read"
|
||||
})
|
||||
}
|
||||
}.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<RoleTemplateDefinition> GetTemplates()
|
||||
=> Templates.Values.ToArray();
|
||||
|
||||
/// <inheritdoc />
|
||||
public RoleTemplateDefinition? FindByCode(string templateCode)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(templateCode))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return Templates.GetValueOrDefault(templateCode);
|
||||
}
|
||||
|
||||
private static IReadOnlyList<PermissionTemplateDefinition> BuildPermissions(IEnumerable<string> codes)
|
||||
{
|
||||
return codes
|
||||
.Where(code => !string.IsNullOrWhiteSpace(code))
|
||||
.Select(code => code.Trim())
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.Select(code => PermissionDefinitions.TryGetValue(code, out var definition)
|
||||
? definition
|
||||
: new PermissionTemplateDefinition
|
||||
{
|
||||
Code = code,
|
||||
Name = code,
|
||||
Description = "未在预置表中定义的权限,请补充描述。"
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user