- 从文件模块的多部分上传处理器中移除未使用的实体引用 - 从即时通讯模块的群组成员处理器中移除未使用的实体引用 - 从RAG模块的AI模型配置中移除未使用的实体引用 - 从监控模块的告警规则配置中移除未使用的实体引用 - 从平台模块的角色权限处理器中移除未使用的实体引用 - 从脚本模块的执行日志实体引用中进行清理 - 从多个基础设施配置类中移除未使用的依赖项 - 优化测试文件中的导入声明,移除不必要的引用
40 lines
1.6 KiB
C#
40 lines
1.6 KiB
C#
using BuildFlow.Shared.Domain.Errors;
|
|
using BuildFlow.Shared.Domain.Exceptions;
|
|
using MediatR;
|
|
using BuildFlow.Platform.Domain.Entities.Role;
|
|
using BuildFlow.Platform.Domain.Entities.User;
|
|
using BuildFlow.Platform.Domain.Entities.UserRole;
|
|
using BuildFlow.Platform.Infrastructure.Persistence;
|
|
using BuildFlow.Platform.Application.Auth.Commands;
|
|
|
|
namespace BuildFlow.Platform.Application.Auth.Handlers;
|
|
|
|
/// <summary>
|
|
/// 注册Command处理器。执行业务校验后持久化。
|
|
/// </summary>
|
|
public class RegisterCommandHandler(PlatformDbContext db) : IRequestHandler<RegisterCommand, Unit>
|
|
{
|
|
public async Task<Unit> Handle(RegisterCommand request, CancellationToken ct)
|
|
{
|
|
// 用户名与邮箱任一重复即拒绝,保证唯一性约束在业务层先行拦截
|
|
if (await db.Users.AnyAsync<UserEntity>(u => u.Username == request.Username || u.Email == request.Email, ct))
|
|
throw new BusinessException(ErrorCode.AlreadyExists, "用户名或邮箱已存在");
|
|
|
|
var user = new UserEntity
|
|
{
|
|
Username = request.Username,
|
|
Email = request.Email,
|
|
// 用 BCrypt 自带盐哈希密码,永不落地明文
|
|
PasswordHash = BCrypt.Net.BCrypt.HashPassword(request.Password)
|
|
};
|
|
|
|
// 自助注册一律授予最低权限的 "User" 角色,避免越权
|
|
var userRole = await db.Roles.FirstAsync<RoleEntity>(r => r.Name == "User", ct);
|
|
await db.Users.AddAsync(user, ct);
|
|
await db.UserRoles.AddAsync(new UserRoleEntity { UserId = user.Id, RoleId = userRole.Id }, ct);
|
|
await db.SaveChangesAsync(ct);
|
|
|
|
return Unit.Value;
|
|
}
|
|
}
|