修改用户配置

Uat_Study
hang 2022-05-23 14:19:10 +08:00
parent 7b8f86d832
commit 9f9f69ef8c
6 changed files with 168 additions and 77 deletions

View File

@ -122,6 +122,8 @@ namespace IRaCIS.Core.API
//services.AddIpPolicyRateLimitSetup(_configuration); //services.AddIpPolicyRateLimitSetup(_configuration);
// 用户类型 策略授权 // 用户类型 策略授权
services.AddAuthorizationPolicySetup(_configuration); services.AddAuthorizationPolicySetup(_configuration);
services.AddJsonConfigSetup(_configuration);
//转发头设置 获取真实IP //转发头设置 获取真实IP
services.Configure<ForwardedHeadersOptions>(options => services.Configure<ForwardedHeadersOptions>(options =>
{ {

View File

@ -0,0 +1,17 @@
using IRaCIS.Core.Domain.Share;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace IRaCIS.Core.API
{
public static class JsonConfigSetup
{
public static void AddJsonConfigSetup(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<ServiceVerifyConfigOption>(configuration.GetSection("BasicSystemConfig"));
}
}
}

View File

@ -13,9 +13,9 @@
}, },
"BasicSystemConfig": { "BasicSystemConfig": {
"OpenUserComplexPassword": false, "OpenUserComplexPassword": true,
"OpenSignDocumentBeforeWork": false "OpenSignDocumentBeforeWork": true
} }

View File

@ -6,6 +6,7 @@ using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Panda.DynamicWebApi.Attributes; using Panda.DynamicWebApi.Attributes;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
namespace IRaCIS.Application.Services namespace IRaCIS.Application.Services
{ {
@ -17,15 +18,20 @@ namespace IRaCIS.Application.Services
private readonly IRepository<VerificationCode> _verificationCodeRepository; private readonly IRepository<VerificationCode> _verificationCodeRepository;
private readonly IRepository<Doctor> _doctorRepository; private readonly IRepository<Doctor> _doctorRepository;
private readonly IRepository<TrialUser> _userTrialRepository; private readonly IRepository<TrialUser> _userTrialRepository;
private readonly IOptionsMonitor<ServiceVerifyConfigOption> _verifyConfig;
public UserService(IRepository<User> userRepository, public UserService(IRepository<User> userRepository,
IMailVerificationService mailVerificationService, IMailVerificationService mailVerificationService,
IRepository<VerificationCode> verificationCodeRepository, IRepository<VerificationCode> verificationCodeRepository,
IRepository<Doctor> doctorRepository, IRepository<Doctor> doctorRepository,
IRepository<TrialUser> userTrialRepository IRepository<TrialUser> userTrialRepository,
IOptionsMonitor<ServiceVerifyConfigOption> verifyConfig
) )
{ {
_verifyConfig = verifyConfig;
_userRepository = userRepository; _userRepository = userRepository;
_mailVerificationService = mailVerificationService; _mailVerificationService = mailVerificationService;
_verificationCodeRepository = verificationCodeRepository; _verificationCodeRepository = verificationCodeRepository;
@ -33,6 +39,77 @@ namespace IRaCIS.Application.Services
_userTrialRepository = userTrialRepository; _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> /// <summary>发送验证码 修改邮箱(已经登陆修改) New </summary>
[HttpGet("{email}")] [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_-]+)+$")) if (!Regex.IsMatch(email, @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$"))
{ {
return ResponseOutput.NotOk("Please input a legal email"); return ResponseOutput.NotOk("Please input a legal email");
} }
@ -87,11 +162,8 @@ namespace IRaCIS.Application.Services
//var pwd = MD5Helper.Md5(newPwd); //var pwd = MD5Helper.Md5(newPwd);
//var count = _doctorRepository.Update<Doctor>().Where(t => t.Id == doctor.Id).Set(d => d.Password == pwd).ExecuteAffrows(); //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() await _userRepository.UpdatePartialNowNoQueryAsync(_userInfo.Id, u => new User()
{ {
@ -112,6 +184,7 @@ namespace IRaCIS.Application.Services
public async Task<IResponseOutput> SetNewPhone(string newPhone) public async Task<IResponseOutput> SetNewPhone(string newPhone)
{ {
await VerifyUserPhoneAsync(_userInfo.Id, _userInfo.UserTypeId, newPhone);
await _userRepository.UpdatePartialNowNoQueryAsync(_userInfo.Id, u => new User() await _userRepository.UpdatePartialNowNoQueryAsync(_userInfo.Id, u => new User()
{ {
@ -125,11 +198,9 @@ namespace IRaCIS.Application.Services
[HttpPut("{newUserName}")] [HttpPut("{newUserName}")]
public async Task<IResponseOutput> SetNewUserName(string 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() await _userRepository.UpdatePartialNowNoQueryAsync(_userInfo.Id, u => new User()
{ {
UserName = newUserName UserName = newUserName
@ -139,24 +210,27 @@ namespace IRaCIS.Application.Services
} }
[HttpGet] [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个特殊字符 await VerifyUserPwdAsync(userId, newPWd);
//^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}
await VerifyUserNameAsync(userId, newUserName);
await _userRepository.UpdatePartialFromQueryAsync(userId, u => new User() await _userRepository.UpdatePartialFromQueryAsync(userId, u => new User()
{ {
UserName = newUserName, UserName = newUserName,
Password=newPWd, Password = newPWd,
IsFirstAdd=false, IsFirstAdd = false,
},true); }, true);
return ResponseOutput.Ok(); return ResponseOutput.Ok();
} }
@ -275,23 +349,16 @@ namespace IRaCIS.Application.Services
{ {
var dbUser = await _userRepository.FirstOrDefaultAsync(t => t.Id == userId); await VerifyUserPwdAsync(userId, newPwd);
if (dbUser != null)
var success = await _userRepository.BatchUpdateNoTrackingAsync(t => t.Id == userId, u => new User()
{ {
if (dbUser.Password == newPwd) Password = newPwd,
{ IsFirstAdd = false
return ResponseOutput.NotOk("password not change"); });
}
var success = await _userRepository.BatchUpdateNoTrackingAsync(t => t.Id == userId, u => new User() return ResponseOutput.Result(success);
{
Password = newPwd,
IsFirstAdd = false
});
return ResponseOutput.Result(success);
}
return ResponseOutput.NotOk("UserId 传递有误");
} }
/// <summary> /// <summary>
@ -303,54 +370,44 @@ namespace IRaCIS.Application.Services
public async Task<IResponseOutput> ModifyPassword(EditPasswordCommand editPwModel) public async Task<IResponseOutput> ModifyPassword(EditPasswordCommand editPwModel)
{ {
//验证旧密码OK await VerifyUserPwdAsync(_userInfo.Id, editPwModel.NewPassWord, editPwModel.OldPassWord);
var dbUser = await _userRepository.FirstOrDefaultAsync(t => t.Id == _userInfo.Id && t.Password == 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"); UserName = editPwModel.NewUserName,
}
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
}); });
return ResponseOutput.Result(success);
} }
//医生密码 var success = await _userRepository.BatchUpdateNoTrackingAsync(t => t.Id == _userInfo.Id, u => new User()
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,
{ 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()); return ResponseOutput.NotOk(" UserId or The mailbox for this user type already exists", new UserAddedReturnDTO());
} }
var saveItem = _mapper.Map<User>(userAddModel); var saveItem = _mapper.Map<User>(userAddModel);
saveItem.Code = await _userRepository.Select(t => t.Code).DefaultIfEmpty().MaxAsync() + 1; 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) 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))) await VerifyUserNameAsync(model.Id, model.UserName);
{
return ResponseOutput.NotOk("UserId or The mailbox for this user type already exists"); await VerifyUserEmailAsync(model.Id, model.UserTypeId, model.EMail);
}
var user = await _userRepository.FirstOrDefaultAsync(t => t.Id == model.Id); var user = await _userRepository.FirstOrDefaultAsync(t => t.Id == model.Id);

View File

@ -0,0 +1,14 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System;
using System.Collections.Generic;
namespace IRaCIS.Core.Domain.Share
{
public class ServiceVerifyConfigOption
{
public bool OpenUserComplexPassword { get; set; }
public bool OpenSignDocumentBeforeWork { get; set; }
}
}