修改密码bug

Uat_Study
hang 2022-04-15 17:59:43 +08:00
parent 443329198a
commit b992cab7ae
3 changed files with 169 additions and 46 deletions

View File

@ -247,7 +247,7 @@ namespace IRaCIS.Application.Services
}
}
var list = await _userRepository.Where(t => t.EMail == email).Select(t => new UserAccountDto() { UserId = t.Id, UserName = t.UserName, UserRealName = t.LastName + " / " + t.FirstName,UserType = t.UserTypeRole.UserTypeShortName}).ToListAsync();
var list = await _userRepository.Where(t => t.EMail == email).Select(t => new UserAccountDto() { UserId = t.Id, UserName = t.UserName, UserRealName = t.LastName + " / " + t.FirstName, UserType = t.UserTypeRole.UserTypeShortName }).ToListAsync();
@ -291,40 +291,45 @@ namespace IRaCIS.Application.Services
/// </summary>
/// <returns></returns>
[HttpPost]
[UnitOfWork]
public async Task<IResponseOutput> ModifyPassword(EditPasswordCommand editPwModel)
{
if (!string.IsNullOrEmpty(editPwModel.NewUserName))
{
if (await _userRepository.AnyAsync(t => t.UserName == editPwModel.NewUserName && t.Id != _userInfo.Id))
{
return ResponseOutput.NotOk("UserId already exists");
}
var success = await _userRepository.UpdateFromQueryAsync(t => t.Id == _userInfo.Id, u => new User()
{
UserName = editPwModel.NewUserName,
});
}
//验证旧密码OK
var dbUser = await _userRepository.FirstOrDefaultAsync(t => t.Id == _userInfo.Id && t.Password == editPwModel.OldPassWord);
if (dbUser != null)
{
if (dbUser.Password == editPwModel.OldPassWord)
if (dbUser.Password == editPwModel.NewPassWord)
{
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.UpdateFromQueryAsync(t => t.Id == _userInfo.Id, u => new User()
{
UserName = editPwModel.NewUserName,
});
}
var success = await _userRepository.UpdateFromQueryAsync(t => t.Id == _userInfo.Id, u => new User()
{
Password = editPwModel.NewPassWord,
IsFirstAdd = false
});
return ResponseOutput.Result(success);
}

View File

@ -11,16 +11,17 @@ namespace IRaCIS.Core.Infra.EFCore
{
public interface ICommandRepository<TEntity>: ICommandRepository<TEntity, Guid> where TEntity : Entity
{
//Task<TEntity> InsertDictionaryAsync<TFrom>(TFrom from, params EntityVerifyExp<TEntity>[] verify);
Task<TEntity> InsertOrUpdateAsync<TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify);
Task<TEntity> InsertFromDTOAsync<TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify);
Task<TEntity> UpdateFromDTOAsync<TFrom>(TFrom from, bool autoSave = false, bool ignoreDtoNullProperty = true, params EntityVerifyExp<TEntity>[] verify);
TEntity UpdatePartialFields(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify);
//TEntity UpdatePartialFields(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify);
}
public interface ICommandRepository<TEntity, TKey> where TEntity : class

View File

@ -30,10 +30,6 @@ namespace IRaCIS.Core.Infra.EFCore
public Repository(IRaCISDBContext dbContext, IMapper mapper)
{
//if (typeof(TEntity) = typeof(DataInspection))
//{
//}
_dbContext = dbContext;
_mapper = mapper;
}
@ -101,22 +97,52 @@ namespace IRaCIS.Core.Infra.EFCore
}
else
{
return await UpdateFromDTOAsync(from, autoSave,false, verify);
return await UpdateFromDTOAsync(from, autoSave, false, verify);
}
}
private async Task EntityVerifyAsync(bool isAdd, EntityVerifyExp<TEntity>[] verify, Guid? entitydId = null)
{
if (isAdd)
{
foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyUpdate && t.IsVerify))
{
if (await _dbSet.IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false))
{
throw new BusinessValidationFailedException(verifyItem.VerifyMsg);
}
}
}
else
{
foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyAdd && t.IsVerify))
{
if (verifyItem.verifyType == VerifyEnum.OnlyUpdate)
{
if (await _dbSet.IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false))
{
throw new BusinessValidationFailedException(verifyItem.VerifyMsg);
}
}
else if (verifyItem.verifyType == VerifyEnum.Both)
{
if (await _dbSet.IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp.And(t => t.Id != entitydId)).ConfigureAwait(false))
{
throw new BusinessValidationFailedException(verifyItem.VerifyMsg);
}
}
}
}
}
public async Task<TEntity> InsertFromDTOAsync<TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify)
{
var entity = _mapper.Map<TEntity>(from);
foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyUpdate && t.IsVerify))
{
if (await _dbSet.IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false))
{
throw new BusinessValidationFailedException(verifyItem.VerifyMsg);
}
}
await EntityVerifyAsync(true, verify);
await _dbSet.AddAsync(entity).ConfigureAwait(false);
@ -135,23 +161,7 @@ namespace IRaCIS.Core.Infra.EFCore
var entity = _mapper.Map<TEntity>(from);
foreach (var verifyItem in verify.Where(t => t.verifyType != VerifyEnum.OnlyAdd && t.IsVerify))
{
if (verifyItem.verifyType == VerifyEnum.OnlyUpdate)
{
if (await _dbSet.IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp).ConfigureAwait(false))
{
throw new BusinessValidationFailedException(verifyItem.VerifyMsg);
}
}
else if (verifyItem.verifyType == VerifyEnum.Both)
{
if (await _dbSet.IgnoreQueryFilters().AnyAsync(verifyItem.VerifyExp.And(t => t.Id != entity.Id)).ConfigureAwait(false))
{
throw new BusinessValidationFailedException(verifyItem.VerifyMsg);
}
}
}
await EntityVerifyAsync(false, verify, entity.Id);
var dbEntity = await _dbSet.IgnoreQueryFilters().FirstOrDefaultAsync(t => t.Id == entity.Id).ConfigureAwait(false);
@ -190,6 +200,108 @@ namespace IRaCIS.Core.Infra.EFCore
}
//public TEntity UpdatePartialFields(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify)
//{
// EntityVerifyAsync(false, verify, id).RunSynchronously();
// var entity = updateFactory.Compile().Invoke(new TEntity() { Id = id });
// var entityEntry = _dbContext.Entry(entity);
// entityEntry.State = EntityState.Detached;
//}
/// <summary>
/// 部分字段更新 (只更新传递的字段名)
/// </summary>
/// <param name="entity"></param>
/// <param name="propertyNames"> 更新的字段数组 </param>
/// <param name="autoSave"></param>
/// <param name="ignoreEntityNullProperty"></param>
/// <param name="verify"></param>
/// <returns></returns>
public TEntity UpdatePartialFields(TEntity entity, string[] propertyNames,
bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify)
{
EntityVerifyAsync(false, verify, entity.Id).RunSynchronously();
var entityEntry = _dbContext.Entry(entity);
entityEntry.State = EntityState.Detached;
foreach (var propertyName in propertyNames)
{
_dbContext.Entry(entity).Property(propertyName).IsModified = true;
}
// 忽略空值
IgnoreNullValues(ref entity, ignoreEntityNullProperty);
return entityEntry.Entity;
}
/// <summary>
/// 更新 排除某些字段的更新
/// </summary>
/// <param name="entity"></param>
/// <param name="propertyNames"></param>
/// <param name="autoSave"></param>
/// <param name="ignoreEntityNullProperty"></param>
/// <param name="verify"></param>
/// <returns></returns>
public TEntity UpdateExcludeFields(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify)
{
EntityVerifyAsync(false, verify, entity.Id).RunSynchronously();
var entityEntry = _dbContext.Entry(entity);
entityEntry.State = EntityState.Modified;
foreach (var propertyName in propertyNames)
{
_dbContext.Entry(entity).Property(propertyName).IsModified = false;
}
// 忽略空值
IgnoreNullValues(ref entity, ignoreEntityNullProperty);
return entityEntry.Entity;
}
/// <summary>
/// 忽略空值属性
/// </summary>
/// <param name="entity"></param>
/// <param name="ignoreNullValues"></param>
private void IgnoreNullValues(ref TEntity entity, bool? ignoreNullValues = null)
{
var isIgnore = ignoreNullValues;
if (isIgnore == false) return;
// 获取所有的属性
var properties = _dbSet.EntityType.GetProperties();
if (properties == null) return;
foreach (var propety in properties)
{
var entityProperty = _dbContext.Entry(entity).Property(propety.Name);
var propertyValue = entityProperty?.CurrentValue;
var propertyType = entityProperty?.Metadata?.PropertyInfo?.PropertyType;
// 判断是否是无效的值,比如为 null默认时间以及空 Guid 值
var isInvalid = propertyValue == null
|| (propertyType == typeof(DateTime) && propertyValue?.ToString() == new DateTime().ToString())
|| (propertyType == typeof(DateTimeOffset) && propertyValue?.ToString() == new DateTimeOffset().ToString())
|| (propertyType == typeof(Guid) && propertyValue?.ToString() == Guid.Empty.ToString());
if (isInvalid && entityProperty != null)
{
entityProperty.IsModified = false;
}
}
}
public async Task<bool> SaveChangesAsync(CancellationToken cancellationToken = default)
{
@ -271,6 +383,8 @@ namespace IRaCIS.Core.Infra.EFCore
}
}
public async Task<bool> UpdateManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{
_dbSet.UpdateRange(entities);
@ -285,6 +399,9 @@ namespace IRaCIS.Core.Infra.EFCore
}
}
public async Task<bool> DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
_dbSet.Remove(entity);