diff --git a/AsbCloudInfrastructure/Services/UserService.cs b/AsbCloudInfrastructure/Services/UserService.cs index c9c938b1..beabc91d 100644 --- a/AsbCloudInfrastructure/Services/UserService.cs +++ b/AsbCloudInfrastructure/Services/UserService.cs @@ -46,8 +46,9 @@ namespace AsbCloudInfrastructure.Services public async Task InsertAsync(UserExtendedDto dto, CancellationToken token = default) { + dto.Id = default; var entity = Convert(dto); - await AssertLoginAsync(dto.Login, token); + await AssertLoginIsBusyAsync(dto.Login, token); var userRoles = await RoleService.GetByNamesAsync(dto.RoleNames, token).ConfigureAwait(false); var updatedEntity = await cacheUsers.InsertAsync(entity, token).ConfigureAwait(false); if (userRoles?.Any() == true) @@ -55,7 +56,7 @@ namespace AsbCloudInfrastructure.Services return updatedEntity?.Id ?? 0; } - private async Task AssertLoginAsync(string login, CancellationToken token = default) + private async Task AssertLoginIsBusyAsync(string login, CancellationToken token = default) { var existingUser = await cacheUsers.FirstOrDefaultAsync(u => u.Login.ToLower() == login.ToLower(), token); if (existingUser is not null) @@ -89,9 +90,12 @@ namespace AsbCloudInfrastructure.Services public async Task UpdateAsync(int id, UserExtendedDto dto, CancellationToken token = default) { + if (id <= 1) + throw new ArgumentException($"Invalid id {id}. You can't edit this user.", nameof(id)); + var oldUser = await cacheUsers.FirstOrDefaultAsync(u=>u.Id == id, token); if(oldUser.Login != dto.Login) - await AssertLoginAsync(dto.Login, token); + await AssertLoginIsBusyAsync(dto.Login, token); var userRoles = await RoleService.GetByNamesAsync(dto.RoleNames, token).ConfigureAwait(false); await UpdateRolesCacheForUserAsync(id, userRoles, token); @@ -108,10 +112,17 @@ namespace AsbCloudInfrastructure.Services } public Task DeleteAsync(int id, CancellationToken token = default) - => cacheUsers.RemoveAsync(r => r.Id == id, token); + { + if (id <= 1) + return Task.FromResult(0); + return cacheUsers.RemoveAsync(r => r.Id == id, token); + } public Task DeleteAsync(IEnumerable ids, CancellationToken token = default) - => cacheUsers.RemoveAsync(r => ids.Contains(r.Id), token); + { + var filteredIds = ids.Where(i => i > 1).ToList(); + return cacheUsers.RemoveAsync(r => filteredIds.Contains(r.Id), token); + } private IEnumerable GetRolesNamesByIdUser(int idUser) => GetRolesByIdUser(idUser)