forked from ddrilling/AsbCloudServer
CS2-123: Added get of parent's permissions for new role
This commit is contained in:
parent
2f23fbce70
commit
c3c601c2a0
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@ -14,6 +15,7 @@ namespace AsbCloudInfrastructure.Services
|
||||
private readonly CacheTable<UserRole> cacheUserRoles;
|
||||
private readonly CacheTable<Permission> cachePermissions;
|
||||
private readonly CacheTable<RelationUserRolePermission> cacheUserRolesPermissions;
|
||||
private int counter = 0;
|
||||
|
||||
public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb) : base(context)
|
||||
{
|
||||
@ -37,11 +39,16 @@ namespace AsbCloudInfrastructure.Services
|
||||
{
|
||||
var roleDto = await base.GetAsync(id,token);
|
||||
|
||||
return roleDto is null ? null : FillUserRoleWithPermissions(roleDto);
|
||||
return roleDto is null
|
||||
? null
|
||||
: FillUserRoleWithPermissions(roleDto);
|
||||
}
|
||||
|
||||
public override async Task<int> InsertAsync(UserRoleDto dto, CancellationToken token = default)
|
||||
{
|
||||
|
||||
dto.PermissionIds = GetAncestorsPermissionIds(dto, ref counter);
|
||||
|
||||
var newRoleId = await base.InsertAsync(dto, token);
|
||||
|
||||
if (dto.PermissionIds == default)
|
||||
@ -90,5 +97,30 @@ namespace AsbCloudInfrastructure.Services
|
||||
|
||||
return roleDto;
|
||||
}
|
||||
|
||||
private IEnumerable<int> GetAncestorsPermissionIds(UserRoleDto userRoleDto, ref int counter)
|
||||
{
|
||||
var idParent = userRoleDto.IdParent;
|
||||
var resultPermissionsIds = userRoleDto.PermissionIds ?? new List<int>();
|
||||
|
||||
if (idParent == default)
|
||||
return resultPermissionsIds;
|
||||
|
||||
if (counter > 10)
|
||||
{
|
||||
Trace.WriteLine($"User role with id: {userRoleDto.Id} has more than 10 nested parents");
|
||||
return resultPermissionsIds;
|
||||
}
|
||||
|
||||
var parentRole = cacheUserRoles.FirstOrDefault(r => r.Id == idParent)
|
||||
.Adapt<UserRoleDto>();
|
||||
var parentRolePermissionsIds = cacheUserRolesPermissions.Where(p =>
|
||||
p.IdUserRole == parentRole.Id).Select(perm => perm.IdPermission);
|
||||
parentRole.PermissionIds = resultPermissionsIds.Union(parentRolePermissionsIds);
|
||||
|
||||
counter++;
|
||||
|
||||
return GetAncestorsPermissionIds(parentRole, ref counter);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user