Fix UserService.UpdateAsync() preserve users password.

This commit is contained in:
Фролов 2021-12-22 16:55:39 +05:00
parent 4d116769a8
commit ecfdbfc394
4 changed files with 31 additions and 7 deletions

View File

@ -11,6 +11,7 @@
public string Phone { get; set; }
public string Position { get; set; }
public int? IdCompany { get; set; }
public short? IdState { get; set; }
public CompanyDto Company { get; set; }
}
}

View File

@ -26,7 +26,7 @@ namespace AsbCloudDb.Model
public string PasswordHash { get; set; }
[Column("state"), Comment("состояние:\n100 - удален")]
public short? State { get; set; }
public short? IdState { get; set; }
[Column("name"), Comment("имя")]
[StringLength(255)]

View File

@ -49,7 +49,7 @@ namespace AsbCloudInfrastructure.Services
var (identity, user) = await GetClaimsUserAsync(login, password, token)
.ConfigureAwait(false);
if (identity == default || user.State == 0)
if (identity == default || user.IdState == 0)
return null;
var userDto = await userService.GetAsync(user.Id, token)
@ -93,7 +93,7 @@ namespace AsbCloudInfrastructure.Services
var newUser = new User
{
IdCompany = userDto.IdCompany,
State = 0,
IdState = 0,
Name = userDto.Name,
Surname = userDto.Surname,
Patronymic = userDto.Patronymic,

View File

@ -18,6 +18,14 @@ namespace AsbCloudInfrastructure.Services
public ISet<string> Includes { get; } = new SortedSet<string>();
public IUserRoleService RoleService { get; }
private static readonly TypeAdapterConfig userTypeAdapterConfig = TypeAdapterConfig<UserExtendedDto, User>
.NewConfig()
.Ignore(dst => dst.Company,
dst => dst.FileMarks,
dst => dst.Files,
dst => dst.RelationUsersUserRoles)
.Config;
public UserService(IAsbCloudDbContext context, CacheDb cacheDb, IUserRoleService roleService)
{
var db = (AsbCloudDbContext)context;
@ -38,7 +46,7 @@ namespace AsbCloudInfrastructure.Services
public async Task<int> InsertAsync(UserExtendedDto dto, CancellationToken token = default)
{
var entity = dto.Adapt<User>();
var entity = Convert(dto);
var updatedEntity = await cacheUsers.InsertAsync(entity, token).ConfigureAwait(false);
await UpdateRolesCacheForUserAsync((int)updatedEntity.Id, dto.RoleNames, token);
return updatedEntity?.Id ?? 0;
@ -55,7 +63,7 @@ namespace AsbCloudInfrastructure.Services
.ToList();
if (entities.Count == 0)
return null;
var dtos = entities.Adapt<UserExtendedDto>().ToList();
var dtos = entities.Select(Convert).ToList();
for (var i = 0; i < dtos.Count; i++)
dtos[i].RoleNames = GetRolesNamesByIdUser(dtos[i].Id);
@ -65,14 +73,14 @@ namespace AsbCloudInfrastructure.Services
public async Task<UserExtendedDto> GetAsync(int id, CancellationToken token = default)
{
var entity = await cacheUsers.FirstOrDefaultAsync(u=>u.Id == id, token).ConfigureAwait(false);
var dto = entity.Adapt<UserExtendedDto>();
var dto = Convert(entity);
dto.RoleNames = GetRolesNamesByIdUser(dto.Id);
return dto;
}
public async Task<int> UpdateAsync(int id, UserExtendedDto dto, CancellationToken token = default)
{
var entity = dto.Adapt<User>();
var entity = Convert(dto);
await UpdateRolesCacheForUserAsync(id, dto.RoleNames, token);
var result = await cacheUsers.UpsertAsync(entity, token)
@ -166,5 +174,20 @@ namespace AsbCloudInfrastructure.Services
return RoleService.HasPermission(relationsToRoles.Select(r => r.IdUserRole),
permissionName);
}
protected virtual User Convert(UserExtendedDto dto)
{
var entity = dto.Adapt<User>(userTypeAdapterConfig);
var oldUser = new Lazy<User>(() => cacheUsers.FirstOrDefault(u => u.Id == dto.Id));
if (string.IsNullOrEmpty(entity.PasswordHash))
entity.PasswordHash = oldUser.Value.PasswordHash;
return entity;
}
protected virtual UserExtendedDto Convert(User entity)
{
var dto = entity.Adapt<UserExtendedDto>();
return dto;
}
}
}