From 7cf1a69dd58ea74d401e8253e1ee58dbf4e0ab1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Mon, 17 Jan 2022 14:31:07 +0500 Subject: [PATCH] =?UTF-8?q?CS2-138=20=D0=97=D0=B0=D0=BF=D1=80=D0=B5=D1=82?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20=D1=83=D0=B4=D0=B0=D0=BB=D1=8F=D1=82=D1=8C?= =?UTF-8?q?=20=D0=B8=20=D1=80=D0=B5=D0=B4=D0=B0=D0=BA=D1=82=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8F=20dev?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/UserService.cs | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) 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)