|
|
|
@ -29,7 +29,7 @@ public partial class UserManager : DomainService
|
|
|
|
|
private static partial Regex UsernameRegex();
|
|
|
|
|
|
|
|
|
|
private readonly IUserRepository _repository;
|
|
|
|
|
private readonly ISqlSugarRepository<RoleEntity> _repositoryRole;
|
|
|
|
|
private readonly ISqlSugarRepository<RoleAggregateRoot> _repositoryRole;
|
|
|
|
|
|
|
|
|
|
private readonly ISqlSugarRepository<UserRoleEntity> _repositoryUserRole;
|
|
|
|
|
private readonly ISqlSugarRepository<UserPostEntity> _repositoryUserPost;
|
|
|
|
@ -44,7 +44,7 @@ public partial class UserManager : DomainService
|
|
|
|
|
ISqlSugarRepository<UserPostEntity> repositoryUserPost,
|
|
|
|
|
ISqlSugarRepository<UserOrganizationEntity> repositoryUserOrg,
|
|
|
|
|
IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache,
|
|
|
|
|
ILocalEventBus localEventBus, ISqlSugarRepository<RoleEntity> repositoryRole)
|
|
|
|
|
ILocalEventBus localEventBus, ISqlSugarRepository<RoleAggregateRoot> repositoryRole)
|
|
|
|
|
{
|
|
|
|
|
_repository = repository;
|
|
|
|
|
_repositoryUserRole = repositoryUserRole;
|
|
|
|
@ -58,33 +58,33 @@ public partial class UserManager : DomainService
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 创建用户
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="entity"></param>
|
|
|
|
|
public async Task CreateAsync(UserEntity entity)
|
|
|
|
|
/// <param name="aggregateRoot"></param>
|
|
|
|
|
public async Task CreateAsync(UserAggregateRoot aggregateRoot)
|
|
|
|
|
{
|
|
|
|
|
await ValidateUser(entity);
|
|
|
|
|
await ValidateUser(aggregateRoot);
|
|
|
|
|
|
|
|
|
|
var returnEntity = await _repository.InsertReturnEntityAsync(entity);
|
|
|
|
|
entity = returnEntity;
|
|
|
|
|
var returnEntity = await _repository.InsertReturnEntityAsync(aggregateRoot);
|
|
|
|
|
aggregateRoot = returnEntity;
|
|
|
|
|
|
|
|
|
|
// 触发事件
|
|
|
|
|
await _localEventBus.PublishAsync(new UserCreatedEventArgs(entity.Id));
|
|
|
|
|
await _localEventBus.PublishAsync(new UserCreatedEventArgs(aggregateRoot.Id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task ValidateUser(UserEntity entity)
|
|
|
|
|
private async Task ValidateUser(UserAggregateRoot aggregateRoot)
|
|
|
|
|
{
|
|
|
|
|
// TODO 不一定非要用户名,这里需要更自由的逻辑
|
|
|
|
|
if (entity.Username is UserConst.Admin or UserConst.TenantAdmin)
|
|
|
|
|
if (aggregateRoot.Username is UserConst.Admin or UserConst.TenantAdmin)
|
|
|
|
|
{
|
|
|
|
|
throw new UserFriendlyException("无效的用户名");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (entity.Username.Length < 2)
|
|
|
|
|
if (aggregateRoot.Username.Length < 2)
|
|
|
|
|
{
|
|
|
|
|
throw new UserFriendlyException("用户名长度错误,需大于2个字符");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 正则表达式,匹配只包含数字和字母的字符串
|
|
|
|
|
var isMatch = UsernameRegex().IsMatch(entity.Username);
|
|
|
|
|
var isMatch = UsernameRegex().IsMatch(aggregateRoot.Username);
|
|
|
|
|
if (!isMatch)
|
|
|
|
|
{
|
|
|
|
|
throw new UserFriendlyException("用户名不能包含除【字母】与【数字】的其他字符");
|
|
|
|
@ -92,14 +92,14 @@ public partial class UserManager : DomainService
|
|
|
|
|
|
|
|
|
|
// 密码长度判断
|
|
|
|
|
// TODO(需要读取配置)
|
|
|
|
|
if (entity.EncryptPassword.Password.Length < 6)
|
|
|
|
|
if (aggregateRoot.EncryptPassword.Password.Length < 6)
|
|
|
|
|
{
|
|
|
|
|
throw new UserFriendlyException($"密码格式错误,长度需大于等于{6}位");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(entity.PhoneNumber))
|
|
|
|
|
if (!string.IsNullOrEmpty(aggregateRoot.PhoneNumber))
|
|
|
|
|
{
|
|
|
|
|
if (await _repository.IsAnyAsync(x => x.PhoneNumber == entity.PhoneNumber))
|
|
|
|
|
if (await _repository.IsAnyAsync(x => x.PhoneNumber == aggregateRoot.PhoneNumber))
|
|
|
|
|
{
|
|
|
|
|
throw new UserFriendlyException("手机号重复");
|
|
|
|
|
}
|
|
|
|
@ -211,7 +211,7 @@ public partial class UserManager : DomainService
|
|
|
|
|
return ret!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private UserFullDto? EntityMapToDto(UserEntity? entity)
|
|
|
|
|
private UserFullDto? EntityMapToDto(UserAggregateRoot? entity)
|
|
|
|
|
{
|
|
|
|
|
if (entity is null)
|
|
|
|
|
{
|
|
|
|
|