forked from ddrilling/AsbCloudServer
CS2-138 Запретить удалять и редактировать пользователя dev
This commit is contained in:
parent
ee4632ae38
commit
7cf1a69dd5
@ -46,8 +46,9 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
|
|
||||||
public async Task<int> InsertAsync(UserExtendedDto dto, CancellationToken token = default)
|
public async Task<int> InsertAsync(UserExtendedDto dto, CancellationToken token = default)
|
||||||
{
|
{
|
||||||
|
dto.Id = default;
|
||||||
var entity = Convert(dto);
|
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 userRoles = await RoleService.GetByNamesAsync(dto.RoleNames, token).ConfigureAwait(false);
|
||||||
var updatedEntity = await cacheUsers.InsertAsync(entity, token).ConfigureAwait(false);
|
var updatedEntity = await cacheUsers.InsertAsync(entity, token).ConfigureAwait(false);
|
||||||
if (userRoles?.Any() == true)
|
if (userRoles?.Any() == true)
|
||||||
@ -55,7 +56,7 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
return updatedEntity?.Id ?? 0;
|
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);
|
var existingUser = await cacheUsers.FirstOrDefaultAsync(u => u.Login.ToLower() == login.ToLower(), token);
|
||||||
if (existingUser is not null)
|
if (existingUser is not null)
|
||||||
@ -89,9 +90,12 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
|
|
||||||
public async Task<int> UpdateAsync(int id, UserExtendedDto dto, CancellationToken token = default)
|
public async Task<int> 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);
|
var oldUser = await cacheUsers.FirstOrDefaultAsync(u=>u.Id == id, token);
|
||||||
if(oldUser.Login != dto.Login)
|
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);
|
var userRoles = await RoleService.GetByNamesAsync(dto.RoleNames, token).ConfigureAwait(false);
|
||||||
await UpdateRolesCacheForUserAsync(id, userRoles, token);
|
await UpdateRolesCacheForUserAsync(id, userRoles, token);
|
||||||
@ -108,10 +112,17 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Task<int> DeleteAsync(int id, CancellationToken token = default)
|
public Task<int> 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<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default)
|
public Task<int> DeleteAsync(IEnumerable<int> 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<string> GetRolesNamesByIdUser(int idUser)
|
private IEnumerable<string> GetRolesNamesByIdUser(int idUser)
|
||||||
=> GetRolesByIdUser(idUser)
|
=> GetRolesByIdUser(idUser)
|
||||||
|
Loading…
Reference in New Issue
Block a user