CS2-123: Returned parent user role permissions inheritance

This commit is contained in:
KharchenkoVladimir 2021-12-02 13:35:15 +05:00
parent b655360835
commit aff16dc84b

View File

@ -1,3 +1,5 @@
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -42,6 +44,16 @@ namespace AsbCloudInfrastructure.Services
: FillUserRoleWithPermissions(roleDto); : FillUserRoleWithPermissions(roleDto);
} }
public override async Task<int> InsertAsync(UserRoleDto dto, CancellationToken token = default)
{
var newRoleId = await base.InsertAsync(dto, token);
var newRolePermissions = GetAncestorsPermissions(newRoleId,
dto.Permissions.Adapt<Permission>(), dto.IdParent, ref counter);
return await cachePermissions.InsertAsync(newRolePermissions, token);
}
public override async Task<int> UpdateAsync(int id, UserRoleDto item, CancellationToken token = default) public override async Task<int> UpdateAsync(int id, UserRoleDto item, CancellationToken token = default)
{ {
foreach (var p in item.Permissions) foreach (var p in item.Permissions)
@ -67,28 +79,27 @@ namespace AsbCloudInfrastructure.Services
return roleDto; return roleDto;
} }
// private IEnumerable<int> GetAncestorsPermissionIds(int idRole, IEnumerable<int> currentPermissionsIds, private IEnumerable<Permission> GetAncestorsPermissions(int idRole, IEnumerable<Permission> currentPermissions,
// int? idParent, ref int counter) int? idParent, ref int counter)
// { {
// if (idParent == default) if (idParent == null)
// return currentPermissionsIds; return currentPermissions;
//
// if (counter > 10) if (counter > 10)
// { {
// Trace.WriteLine($"User role with id: {idRole} has more than 10 nested parents"); Trace.WriteLine($"User role with id: {idRole} has more than 10 nested parents");
// return currentPermissionsIds; return currentPermissions;
// } }
//
// var parentRole = cacheUserRoles.FirstOrDefault(r => r.Id == idParent) var parentRole = cacheUserRoles.FirstOrDefault(r => r.Id == idParent);
// .Adapt<UserRoleDto>(); var parentRolePermissions = cachePermissions.Where(p =>
// var parentRolePermissionsIds = cachePermissions.Where(p => p.IdUserRole == parentRole.Id);
// p.IdUserRole == parentRole.Id).Select(perm => perm.IdPermission); var resultPermissions = currentPermissions.Union(parentRolePermissions);
// var resultPermissions = currentPermissionsIds.Union(parentRolePermissionsIds);
// counter++;
// counter++;
// return GetAncestorsPermissions(parentRole.Id, resultPermissions,
// return GetAncestorsPermissionIds(parentRole.Id, resultPermissions, parentRole.IdParent, ref counter);
// parentRole.IdParent, ref counter); }
// }
} }
} }