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

105 lines
3.8 KiB
C#
Raw Normal View History

using System.Diagnostics;
using System.Collections.Generic;
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> cachePermissionsInfo;
private readonly CacheTable<Permission> cachePermissions;
private int counter = 0;
public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb) : base(context)
{
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context);
cachePermissionsInfo = cacheDb.GetCachedTable<PermissionInfo>((AsbCloudDbContext)context);
cachePermissions =
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)
{
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)
p.IdUserRole = item.Id;
var result = await base.UpdateAsync(id, item, token);
await cachePermissions.RemoveAsync(r => r.IdUserRole == item.Id, token)
.ConfigureAwait(false);
var newPermissions = item.Permissions.Adapt<Permission>();
await cachePermissions.InsertAsync(newPermissions, token);
return result;
}
private UserRoleDto FillUserRoleWithPermissions(UserRoleDto roleDto)
{
roleDto.Permissions = cachePermissions.Where(c =>
c.IdUserRole == roleDto.Id).Adapt<PermissionDto>();
return roleDto;
}
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);
}
}
}