修改用户校验

This commit is contained in:
2022-05-26 09:23:32 +08:00
parent 536d80718e
commit 815ed6e31c
4 changed files with 29 additions and 21 deletions
@@ -41,26 +41,26 @@ namespace IRaCIS.Application.Services
private async Task VerifyUserNameAsync(Guid userId, string userName)
private async Task VerifyUserNameAsync(Guid? userId, string userName)
{
if (await _userRepository.AnyAsync(t => t.UserName == userName && t.Id != userId))
if (await _userRepository.WhereIf(userId!=null,t=>t.Id!=userId).AnyAsync(t => t.UserName == userName ))
{
throw new BusinessValidationFailedException("用户名已经存在。");
}
}
private async Task VerifyUserPhoneAsync(Guid userId, Guid userTypeId, string phone)
private async Task VerifyUserPhoneAsync(Guid? userId, Guid userTypeId, string phone)
{
if (await _userRepository.AnyAsync(t => (t.Phone == phone && t.UserTypeId == userTypeId && t.Id != userId)))
if (await _userRepository.WhereIf(userId != null, t => t.Id != userId).AnyAsync(t => (t.Phone == phone && t.UserTypeId == userTypeId )))
{
throw new BusinessValidationFailedException("该用户类型中已存在具有相同的电话的用户。");
}
}
private async Task VerifyUserEmailAsync(Guid userId, Guid userTypeId, string email)
private async Task VerifyUserEmailAsync(Guid? userId, Guid userTypeId, string email)
{
if (await _userRepository.AnyAsync(t => (t.EMail == email && t.UserTypeId == userTypeId && t.Id != userId)))
if (await _userRepository.WhereIf(userId != null, t => t.Id != userId).AnyAsync(t => (t.EMail == email && t.UserTypeId == userTypeId)))
{
throw new BusinessValidationFailedException("该用户类型中已存在具有相同邮箱的用户。");
}
@@ -78,8 +78,13 @@ namespace IRaCIS.Application.Services
throw new BusinessValidationFailedException("新密码与旧密码相同。");
}
var dbUser = (await _userRepository.FirstOrDefaultAsync(t => t.Id == userId)).IfNullThrowException();
if (oldPwd != null && dbUser.Password != oldPwd)
{
throw new BusinessValidationFailedException("旧密码验证失败。");
}
if (dbUser.Password == newPwd)
{
@@ -87,10 +92,7 @@ namespace IRaCIS.Application.Services
}
if (dbUser.Password != oldPwd)
{
throw new BusinessValidationFailedException("旧密码验证失败。");
}
//正则 至少8个字符,至少1个大写字母,1个小写字母,1个数字和1个特殊字符:
@@ -441,7 +443,7 @@ namespace IRaCIS.Application.Services
/// 根据用户Id获取用户详细信息[New]
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <returns></returns>xiuga
[HttpGet("{id:guid}")]
public async Task<UserDetailDTO> GetUser(Guid id)
{
@@ -457,10 +459,13 @@ namespace IRaCIS.Application.Services
[UnitOfWork]
public async Task<IResponseOutput<UserAddedReturnDTO>> AddUser(UserCommand userAddModel)
{
if (await _userRepository.AnyAsync(t => t.UserName == userAddModel.UserName || (t.EMail == userAddModel.EMail && t.UserTypeId == userAddModel.UserTypeId)))
{
return ResponseOutput.NotOk(" 该用户类型中已存在具有相同用户名或者邮箱的用户。", new UserAddedReturnDTO());
}
await VerifyUserNameAsync(null, userAddModel.UserName);
await VerifyUserEmailAsync(null, userAddModel.UserTypeId, userAddModel.EMail);
await VerifyUserPhoneAsync(null, userAddModel.UserTypeId, userAddModel.Phone);
var saveItem = _mapper.Map<User>(userAddModel);
@@ -500,6 +505,7 @@ namespace IRaCIS.Application.Services
await VerifyUserEmailAsync(model.Id, model.UserTypeId, model.EMail);
await VerifyUserPhoneAsync(model.Id, model.UserTypeId, model.Phone);
var user = await _userRepository.FirstOrDefaultAsync(t => t.Id == model.Id);