forked from ddrilling/AsbCloudServer
CS2-123: Returned parent user role permissions inheritance
This commit is contained in:
parent
b655360835
commit
aff16dc84b
@ -1,3 +1,5 @@
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@ -42,6 +44,16 @@ namespace AsbCloudInfrastructure.Services
|
||||
: 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)
|
||||
{
|
||||
foreach (var p in item.Permissions)
|
||||
@ -67,28 +79,27 @@ namespace AsbCloudInfrastructure.Services
|
||||
return roleDto;
|
||||
}
|
||||
|
||||
// private IEnumerable<int> GetAncestorsPermissionIds(int idRole, IEnumerable<int> currentPermissionsIds,
|
||||
// int? idParent, ref int counter)
|
||||
// {
|
||||
// if (idParent == default)
|
||||
// return currentPermissionsIds;
|
||||
//
|
||||
// if (counter > 10)
|
||||
// {
|
||||
// Trace.WriteLine($"User role with id: {idRole} has more than 10 nested parents");
|
||||
// return currentPermissionsIds;
|
||||
// }
|
||||
//
|
||||
// var parentRole = cacheUserRoles.FirstOrDefault(r => r.Id == idParent)
|
||||
// .Adapt<UserRoleDto>();
|
||||
// var parentRolePermissionsIds = cachePermissions.Where(p =>
|
||||
// p.IdUserRole == parentRole.Id).Select(perm => perm.IdPermission);
|
||||
// var resultPermissions = currentPermissionsIds.Union(parentRolePermissionsIds);
|
||||
//
|
||||
// counter++;
|
||||
//
|
||||
// return GetAncestorsPermissionIds(parentRole.Id, resultPermissions,
|
||||
// parentRole.IdParent, ref counter);
|
||||
// }
|
||||
private IEnumerable<Permission> GetAncestorsPermissions(int idRole, IEnumerable<Permission> currentPermissions,
|
||||
int? idParent, ref int counter)
|
||||
{
|
||||
if (idParent == null)
|
||||
return currentPermissions;
|
||||
|
||||
if (counter > 10)
|
||||
{
|
||||
Trace.WriteLine($"User role with id: {idRole} has more than 10 nested parents");
|
||||
return currentPermissions;
|
||||
}
|
||||
|
||||
var parentRole = cacheUserRoles.FirstOrDefault(r => r.Id == idParent);
|
||||
var parentRolePermissions = cachePermissions.Where(p =>
|
||||
p.IdUserRole == parentRole.Id);
|
||||
var resultPermissions = currentPermissions.Union(parentRolePermissions);
|
||||
|
||||
counter++;
|
||||
|
||||
return GetAncestorsPermissions(parentRole.Id, resultPermissions,
|
||||
parentRole.IdParent, ref counter);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user