From 9f9f69ef8c9f60e9bbb83978470ae6727cbe2ec9 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Mon, 23 May 2022 14:19:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.API/Program.cs | 2 +- IRaCIS.Core.API/Startup.cs | 2 + .../_ServiceExtensions/JsonConfigSetup.cs | 17 ++ IRaCIS.Core.API/appsettings.Development.json | 4 +- .../Service/Management/UserService.cs | 206 +++++++++++------- .../ServiceVerifyConfigOption.cs | 14 ++ 6 files changed, 168 insertions(+), 77 deletions(-) create mode 100644 IRaCIS.Core.API/_ServiceExtensions/JsonConfigSetup.cs create mode 100644 IRaCIS.Core.Domain.Share/ServiceVerifyConfigOption.cs diff --git a/IRaCIS.Core.API/Program.cs b/IRaCIS.Core.API/Program.cs index 8675aa1e6..e5046008f 100644 --- a/IRaCIS.Core.API/Program.cs +++ b/IRaCIS.Core.API/Program.cs @@ -37,7 +37,7 @@ namespace IRaCIS.Core.API .AddJsonFile($"appsettings.{environment}.json", false, true); }) .Build(); - + NewId.SetProcessIdProvider(new CurrentProcessIdProvider()); diff --git a/IRaCIS.Core.API/Startup.cs b/IRaCIS.Core.API/Startup.cs index 706fd4e49..869b55ee6 100644 --- a/IRaCIS.Core.API/Startup.cs +++ b/IRaCIS.Core.API/Startup.cs @@ -122,6 +122,8 @@ namespace IRaCIS.Core.API //services.AddIpPolicyRateLimitSetup(_configuration); // 用户类型 策略授权 services.AddAuthorizationPolicySetup(_configuration); + + services.AddJsonConfigSetup(_configuration); //转发头设置 获取真实IP services.Configure(options => { diff --git a/IRaCIS.Core.API/_ServiceExtensions/JsonConfigSetup.cs b/IRaCIS.Core.API/_ServiceExtensions/JsonConfigSetup.cs new file mode 100644 index 000000000..3707ae1a0 --- /dev/null +++ b/IRaCIS.Core.API/_ServiceExtensions/JsonConfigSetup.cs @@ -0,0 +1,17 @@ +锘縰sing 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(configuration.GetSection("BasicSystemConfig")); + + + } + } +} diff --git a/IRaCIS.Core.API/appsettings.Development.json b/IRaCIS.Core.API/appsettings.Development.json index 73669f49d..f9797882d 100644 --- a/IRaCIS.Core.API/appsettings.Development.json +++ b/IRaCIS.Core.API/appsettings.Development.json @@ -13,9 +13,9 @@ }, "BasicSystemConfig": { - "OpenUserComplexPassword": false, + "OpenUserComplexPassword": true, - "OpenSignDocumentBeforeWork": false + "OpenSignDocumentBeforeWork": true } diff --git a/IRaCIS.Core.Application/Service/Management/UserService.cs b/IRaCIS.Core.Application/Service/Management/UserService.cs index 028e99f18..0d9f0f1c1 100644 --- a/IRaCIS.Core.Application/Service/Management/UserService.cs +++ b/IRaCIS.Core.Application/Service/Management/UserService.cs @@ -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 _verificationCodeRepository; private readonly IRepository _doctorRepository; private readonly IRepository _userTrialRepository; + + private readonly IOptionsMonitor _verifyConfig; public UserService(IRepository userRepository, IMailVerificationService mailVerificationService, IRepository verificationCodeRepository, IRepository doctorRepository, - IRepository userTrialRepository + IRepository userTrialRepository, + IOptionsMonitor 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"); + } + + + //姝e垯 鑷冲皯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; + + + } + + /// 鍙戦侀獙璇佺爜 淇敼閭锛堝凡缁忕櫥闄嗕慨鏀癸級 New [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().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 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 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 @@ -138,25 +209,28 @@ namespace IRaCIS.Application.Services return ResponseOutput.Ok(); } - + + + + [HttpGet] - public async Task InitSetUserNameAndPwd (Guid userId, string newUserName,string newPWd) + public async Task InitSetUserNameAndPwd(Guid userId, string newUserName, string newPWd) { - //姝e垯 鑷冲皯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 浼犻掓湁璇"); } /// @@ -303,54 +370,44 @@ namespace IRaCIS.Application.Services public async Task ModifyPassword(EditPasswordCommand editPwModel) { - //楠岃瘉鏃у瘑鐮丱K - 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(userAddModel); saveItem.Code = await _userRepository.Select(t => t.Code).DefaultIfEmpty().MaxAsync() + 1; @@ -433,11 +491,11 @@ namespace IRaCIS.Application.Services public async Task 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); diff --git a/IRaCIS.Core.Domain.Share/ServiceVerifyConfigOption.cs b/IRaCIS.Core.Domain.Share/ServiceVerifyConfigOption.cs new file mode 100644 index 000000000..b2c42b775 --- /dev/null +++ b/IRaCIS.Core.Domain.Share/ServiceVerifyConfigOption.cs @@ -0,0 +1,14 @@ +锘縰sing 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; } + } +} \ No newline at end of file