DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/UserRoleService.cs

127 lines
4.6 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Mapster;
namespace AsbCloudInfrastructure.Services
{
public class UserRoleService : CrudServiceBase<UserRoleDto, UserRole>
{
private readonly CacheTable<UserRole> cacheUserRoles;
private readonly CacheTable<PermissionInfo> cachePermissions;
private readonly CacheTable<Permission> cacheUserRolesPermissions;
private int counter = 0;
public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb) : base(context)
{
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context);
cachePermissions = cacheDb.GetCachedTable<PermissionInfo>((AsbCloudDbContext)context);
cacheUserRolesPermissions =
cacheDb.GetCachedTable<Permission>((AsbCloudDbContext)context);
}
public override async Task<PaginationContainer<UserRoleDto>> GetPageAsync(int skip = 0,
int take = 32, CancellationToken token = default)
{
var rolesDtos = await base.GetPageAsync(skip, take,token);
rolesDtos.Items = rolesDtos.Items.Select(FillUserRoleWithPermissions).ToList();
return rolesDtos;
}
public override async Task<UserRoleDto> GetAsync(int id, CancellationToken token = default)
{
var roleDto = await base.GetAsync(id,token);
return roleDto is null
? null
: FillUserRoleWithPermissions(roleDto);
}
public override async Task<int> InsertAsync(UserRoleDto dto, CancellationToken token = default)
{
dto.PermissionIds = GetAncestorsPermissionIds(dto.Id, dto.PermissionIds, dto.IdParent, ref counter);
var newRoleId = await base.InsertAsync(dto, token);
if (dto.PermissionIds == default)
return newRoleId;
foreach (var pId in dto.PermissionIds)
{
var relation = new Permission()
{
IdRole = newRoleId,
IdPermission = pId
};
context.RelationUserRolesPermissions.Add(relation);
}
return await context.SaveChangesAsync(token);
}
public override async Task<int> UpdateAsync(int id, UserRoleDto item, CancellationToken token = default)
{
var result = await base.UpdateAsync(id, item, token);
if (item.PermissionIds == default)
return result;
await cacheUserRolesPermissions.RemoveAsync(r => r.IdRole == item.Id, token)
.ConfigureAwait(false);
var newRelations = item.PermissionIds.Select(p => new Permission()
{
IdRole = item.Id,
IdPermission = p
});
await cacheUserRolesPermissions.InsertAsync(newRelations, token);
return result;
}
private UserRoleDto FillUserRoleWithPermissions(UserRoleDto roleDto)
{
var rolePermissionIds = cacheUserRolesPermissions.Where(c =>
c.IdRole == roleDto.Id).Select(p => p.IdPermission);
roleDto.Permissions = cachePermissions.Where(permission => rolePermissionIds.Contains(permission.Id))
.Adapt<PermissionDto>();
return roleDto;
}
private IEnumerable<int> GetAncestorsPermissionIds(int idRole, IEnumerable<int> currentPermissionsIds,
int? idParent, ref int counter)
{
//var currentPermissionsIds = userRoleDto.PermissionIds ?? new List<int>();
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 = cacheUserRolesPermissions.Where(p =>
p.IdRole == parentRole.Id).Select(perm => perm.IdPermission);
var resultPermissions = currentPermissionsIds.Union(parentRolePermissionsIds);
counter++;
return GetAncestorsPermissionIds(parentRole.Id, resultPermissions,
parentRole.IdParent, ref counter);
}
}
}