@ -6,6 +6,7 @@ using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc ;
using Panda.DynamicWebApi.Attributes ;
using Microsoft.AspNetCore.Authorization ;
using Microsoft.Extensions.Options ;
namespace IRaCIS.Application.Services
{
@ -17,15 +18,20 @@ namespace IRaCIS.Application.Services
private readonly IRepository < VerificationCode > _verificationCodeRepository ;
private readonly IRepository < Doctor > _doctorRepository ;
private readonly IRepository < TrialUser > _userTrialRepository ;
private readonly IOptionsMonitor < ServiceVerifyConfigOption > _verifyConfig ;
public UserService ( IRepository < User > userRepository ,
IMailVerificationService mailVerificationService ,
IRepository < VerificationCode > verificationCodeRepository ,
IRepository < Doctor > doctorRepository ,
IRepository < TrialUser > userTrialRepository
IRepository < TrialUser > userTrialRepository ,
IOptionsMonitor < ServiceVerifyConfigOption > verifyConfig
)
{
_verifyConfig = verifyConfig ;
_userRepository = userRepository ;
_mailVerificationService = mailVerificationService ;
_verificationCodeRepository = verificationCodeRepository ;
@ -33,6 +39,77 @@ namespace IRaCIS.Application.Services
_userTrialRepository = userTrialRepository ;
}
private async Task VerifyUserNameAsync ( Guid userId , string userName )
{
if ( await _userRepository . AnyAsync ( t = > t . UserName = = userName & & t . Id ! = userId ) )
{
throw new BusinessValidationFailedException ( "UserId already exists" ) ;
}
}
private async Task VerifyUserPhoneAsync ( Guid userId , Guid userTypeId , string phone )
{
if ( await _userRepository . AnyAsync ( t = > ( t . Phone = = phone & & t . UserTypeId = = userTypeId & & t . Id ! = userId ) ) )
{
throw new BusinessValidationFailedException ( "The phone for this user type already exists" ) ;
}
}
private async Task VerifyUserEmailAsync ( Guid userId , Guid userTypeId , string email )
{
if ( await _userRepository . AnyAsync ( t = > ( t . EMail = = email & & t . UserTypeId = = userTypeId & & t . Id ! = userId ) ) )
{
throw new BusinessValidationFailedException ( "The mailbox for this user type already exists" ) ;
}
}
private async Task VerifyUserPwdAsync ( Guid userId , string newPwd , string? oldPwd = null )
{
//var dbUser = (await _userRepository.FirstOrDefaultAsync(t => t.Id == userId)).IfNullThrowException();
if ( _verifyConfig . CurrentValue . OpenUserComplexPassword )
{
if ( oldPwd ! = null & & oldPwd = = newPwd )
{
throw new BusinessValidationFailedException ( "password not change" ) ;
}
var dbUser = ( await _userRepository . FirstOrDefaultAsync ( t = > t . Id = = userId ) ) . IfNullThrowException ( ) ;
if ( dbUser . Password = = newPwd )
{
throw new BusinessValidationFailedException ( "password not change" ) ;
}
if ( dbUser . Password = = oldPwd )
{
throw new BusinessValidationFailedException ( "old password error" ) ;
}
//正则 至少8个字符, 至少1个大写字母, 1个小写字母, 1个数字和1个特殊字符:
//^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}
if ( ! Regex . IsMatch ( newPwd , @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{10,}" ) )
{
throw new BusinessValidationFailedException ( "至少10个字符, 其中至少1个大写字母, 1个小写字母, 1个数字和1个特殊字符" ) ;
}
}
await Task . CompletedTask ;
}
/// <summary>发送验证码 修改邮箱(已经登陆修改) New </summary>
[HttpGet("{email}")]
@ -42,9 +119,7 @@ namespace IRaCIS.Application.Services
//检查手机或者邮箱是否有效
if ( ! Regex . IsMatch ( email , @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$" ) )
{
return ResponseOutput . NotOk ( "Please input a legal email" ) ;
}
@ -87,11 +162,8 @@ namespace IRaCIS.Application.Services
//var pwd = MD5Helper.Md5(newPwd);
//var count = _doctorRepository.Update<Doctor>().Where(t => t.Id == doctor.Id).Set(d => d.Password == pwd).ExecuteAffrows();
await VerifyUserEmailAsync ( _userInfo . Id , _userInfo . UserTypeId , newEmail ) ;
if ( await _userRepository . AnyAsync ( t = > ( t . EMail = = newEmail & & t . UserTypeId = = _userInfo . UserTypeId & & t . Id ! = _userInfo . Id ) ) )
{
return ResponseOutput . NotOk ( "The mailbox for this user type already exists" ) ;
}
await _userRepository . UpdatePartialNowNoQueryAsync ( _userInfo . Id , u = > new User ( )
{
@ -112,6 +184,7 @@ namespace IRaCIS.Application.Services
public async Task < IResponseOutput > SetNewPhone ( string newPhone )
{
await VerifyUserPhoneAsync ( _userInfo . Id , _userInfo . UserTypeId , newPhone ) ;
await _userRepository . UpdatePartialNowNoQueryAsync ( _userInfo . Id , u = > new User ( )
{
@ -125,11 +198,9 @@ namespace IRaCIS.Application.Services
[HttpPut("{newUserName}")]
public async Task < IResponseOutput > SetNewUserName ( string newUserName )
{
await VerifyUserNameAsync ( _userInfo . Id , newUserName ) ;
if ( await _userRepository . AnyAsync ( t = > t . UserName = = newUserName & & t . Id ! = _userInfo . Id ) )
{
return ResponseOutput . NotOk ( "UserId already exists" ) ;
}
await _userRepository . UpdatePartialNowNoQueryAsync ( _userInfo . Id , u = > new User ( )
{
UserName = newUserName
@ -139,24 +210,27 @@ namespace IRaCIS.Application.Services
}
[HttpGet]
public async Task < IResponseOutput > InitSetUserNameAndPwd ( Guid userId , string newUserName , string newPWd )
public async Task < IResponseOutput > InitSetUserNameAndPwd ( Guid userId , string newUserName , string newPWd )
{
//正则 至少8个字符, 至少1个大写字母, 1个小写字母, 1个数字和1个特殊字符:
//^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}
await VerifyUserPwdAsync ( userId , newPWd ) ;
await VerifyUserNameAsync ( userId , newUserName ) ;
await _userRepository . UpdatePartialFromQueryAsync ( userId , u = > new User ( )
{
UserName = newUserName ,
Password = newPWd ,
Password = newPWd ,
IsFirstAdd = false ,
IsFirstAdd = false ,
} , true ) ;
} , true ) ;
return ResponseOutput . Ok ( ) ;
}
@ -275,23 +349,16 @@ namespace IRaCIS.Application.Services
{
var dbUser = await _userRepository . FirstOrDefaultAsync ( t = > t . Id = = userId ) ;
if ( dbUser ! = null )
await VerifyUserPwdAsync ( userId , newPwd ) ;
var success = await _userRepository . BatchUpdateNoTrackingAsync ( t = > t . Id = = userId , u = > new User ( )
{
if ( dbUser . Password = = newPwd )
{
return ResponseOutput . NotOk ( "password not change" ) ;
}
Password = newPwd ,
IsFirstAdd = false
} ) ;
var success = await _userRepository . BatchUpdateNoTrackingAsync ( t = > t . Id = = userId , u = > new User ( )
{
Password = newPwd ,
IsFirstAdd = false
} ) ;
return ResponseOutput . Result ( success ) ;
return ResponseOutput . Result ( success ) ;
}
return ResponseOutput . NotOk ( "UserId 传递有误" ) ;
}
/// <summary>
@ -303,54 +370,44 @@ namespace IRaCIS.Application.Services
public async Task < IResponseOutput > ModifyPassword ( EditPasswordCommand editPwModel )
{
//验证旧密码OK
var dbUser = await _userRepository . FirstOrDefaultAsync ( t = > t . Id = = _userInfo . Id & & t . Password = = editPwModel . OldPassWord ) ;
await VerifyUserPwdAsync ( _userInfo . Id , editPwModel . NewPassWord , editPwModel . OldPassWord ) ;
if ( dbUser ! = null )
if ( ! string . IsNullOrEmpty ( editPwModel . NewUserName ) )
{
if ( dbUser . Password = = editPwModel . NewPassWord )
await VerifyUserNameAsync ( _userInfo . Id , editPwModel . NewUserName ) ;
await _userRepository . BatchUpdateNoTrackingAsync ( t = > t . Id = = _userInfo . Id , u = > new User ( )
{
return ResponseOutput . NotOk ( "password not change" ) ;
}
if ( ! string . IsNullOrEmpty ( editPwModel . NewUserName ) )
{
if ( await _userRepository . AnyAsync ( t = > t . UserName = = editPwModel . NewUserName & & t . Id ! = _userInfo . Id ) )
{
return ResponseOutput . NotOk ( "UserId already exists" ) ;
}
await _userRepository . BatchUpdateNoTrackingAsync ( t = > t . Id = = _userInfo . Id , u = > new User ( )
{
UserName = editPwModel . NewUserName ,
} ) ;
}
var success = await _userRepository . BatchUpdateNoTrackingAsync ( t = > t . Id = = _userInfo . Id , u = > new User ( )
{
Password = editPwModel . NewPassWord ,
IsFirstAdd = false
UserName = editPwModel . NewUserName ,
} ) ;
return ResponseOutput . Result ( success ) ;
}
//医生密码
if ( await _doctorRepository . AnyAsync ( t = > t . Id = = _userInfo . Id & & t . Password = = editPwModel . OldPassWord ) )
var success = await _userRepository . BatchUpdateNoTrackingAsync ( t = > t . Id = = _userInfo . Id , u = > new User ( )
{
var success = await _doctorRepository . BatchUpdateNoTrackingAsync ( t = > t . Id = = _userInfo . Id , u = > new Doctor ( )
{
Password = editPwModel . NewPassWord ,
IsFirstAdd = false
} ) ;
Password = editPwModel . NewPassWord
} ) ;
return ResponseOutput . Result ( success ) ;
}
return ResponseOutput . Result ( success ) ;
return ResponseOutput . NotOk ( "Old password is wrong." ) ;
////医生密码
//if (await _doctorRepository.AnyAsync(t => t.Id == _userInfo.Id && t.Password == editPwModel.OldPassWord))
//{
// var success = await _doctorRepository.BatchUpdateNoTrackingAsync(t => t.Id == _userInfo.Id, u => new Doctor()
// {
// Password = editPwModel.NewPassWord
// });
// return ResponseOutput.Result(success);
//}
//return ResponseOutput.NotOk("Old password is wrong.");
}
@ -401,6 +458,7 @@ namespace IRaCIS.Application.Services
return ResponseOutput . NotOk ( " UserId or The mailbox for this user type already exists" , new UserAddedReturnDTO ( ) ) ;
}
var saveItem = _mapper . Map < User > ( userAddModel ) ;
saveItem . Code = await _userRepository . Select ( t = > t . Code ) . DefaultIfEmpty ( ) . MaxAsync ( ) + 1 ;
@ -433,11 +491,11 @@ namespace IRaCIS.Application.Services
public async Task < IResponseOutput > UpdateUser ( UserCommand model )
{
// 判断当前用户名是否已经存在
if ( await _userRepository . AnyAsync ( t = > ( t . UserName = = model . UserName & & t . Id ! = model . Id ) | | ( t . EMail = = model . EMail & & t . UserTypeId = = model . UserTypeId & & t . Id ! = model . Id ) ) )
{
return ResponseOutput . NotOk ( "UserId or The mailbox for this user type already exists" ) ;
}
await VerifyUserNameAsync ( model . Id , model . UserName ) ;
await VerifyUserEmailAsync ( model . Id , model . UserTypeId , model . EMail ) ;
var user = await _userRepository . FirstOrDefaultAsync ( t = > t . Id = = model . Id ) ;