Merge pull request '#7759269 Проверка ролей на зацикливание включенных ролей' (#14) from feature/7759269 into dev

Reviewed-on: http://46.146.209.148:8080/DDrilling/AsbCloudServer/pulls/14
This commit is contained in:
ai.astrakhantsev 2022-11-29 12:12:33 +05:00
commit 2963d74f43

View File

@ -90,10 +90,10 @@ namespace AsbCloudInfrastructure.Repository
public async Task<int> UpdateAsync(UserRoleDto dto, CancellationToken token)
{
var entity = Convert(dto);
await UpdatePermissionsAsync(dto, token);
await UpdateIncludedRolesAsync(dto, token);
var entity = Convert(dto);
var result = dbContext.UserRoles.Upsert(entity);
await dbContext.SaveChangesAsync(token);
DropCacheUserRole();
@ -177,13 +177,29 @@ namespace AsbCloudInfrastructure.Repository
return false;
}
private IEnumerable<UserRoleDto> GetNestedByIds(IEnumerable<int> ids, int recursionLevel = 7)
{
var roles = new List<UserRoleDto>();
foreach (var id in ids)
roles.AddRange(GetNestedById(id, recursionLevel));
return roles;
}
private async Task UpdateIncludedRolesAsync(UserRoleDto dto, CancellationToken token)
{
if (dto?.Roles is null)
return;
var relations = (await GetCacheRelationUserRoleUserRoleAsync(token).ConfigureAwait(false))
.Where(r => r.Id == dto.Id);
var idsIncludeRole = GetNestedByIds(dto.Roles.Select(x => x.Id)).Select(x => x.Id);
if (idsIncludeRole is not null && idsIncludeRole.Any(x => x == dto.Id))
throw new ArgumentInvalidException("Invalid include role (self reference)", nameof(dto));
var relations = await dbContext.RelationUserRoleUserRoles
.Where(r => r.Id == dto.Id)
.ToListAsync(token)
.ConfigureAwait(false);
dbContext.RelationUserRoleUserRoles.RemoveRange(relations);
@ -201,9 +217,10 @@ namespace AsbCloudInfrastructure.Repository
if (dto?.Permissions is null)
return;
var relations = (await GetCacheRelationUserRolePermissionsAsync(token).ConfigureAwait(false))
.Where(r => r.IdUserRole == dto.Id);
var relations = await dbContext.RelationUserRolePermissions
.Where(r => r.IdUserRole == dto.Id)
.ToListAsync(token)
.ConfigureAwait(false);
dbContext.RelationUserRolePermissions.RemoveRange(relations);
if (dto.Permissions.Any())
@ -235,19 +252,9 @@ namespace AsbCloudInfrastructure.Repository
private void DropCacheUserRole()
=> dbContext.RelationUserUserRoles.DropCache(userRoleCacheTag);
private Task<IEnumerable<RelationUserRoleUserRole>> GetCacheRelationUserRoleUserRoleAsync(CancellationToken token)
=> dbContext.RelationUserRoleUserRoles
.Include(r => r.IncludeRole)
.Include(r => r.Role)
.FromCacheAsync(relationUserRoleUserRoleCacheTag, relationCacheObsolence, token);
private void DropCacheRelationUserRoleUserRole()
=> dbContext.RelationUserUserRoles.DropCache(relationUserRoleUserRoleCacheTag);
private Task<IEnumerable<RelationUserRolePermission>> GetCacheRelationUserRolePermissionsAsync(CancellationToken token)
=> dbContext.RelationUserRolePermissions
.Include(r => r.UserRole)
.Include(r => r.Permission)
.FromCacheAsync(relationUserRolePermissionsCacheTag, relationCacheObsolence, token);
private IEnumerable<RelationUserRolePermission> GetCacheRelationUserRolePermissions()
=> dbContext.RelationUserRolePermissions
.Include(r => r.UserRole)