2021-12-02 13:35:15 +05:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Collections.Generic;
|
2021-11-29 12:39:28 +05:00
|
|
|
using System.Linq;
|
2021-11-24 17:38:40 +05:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
using AsbCloudInfrastructure.Services.Cache;
|
|
|
|
using Mapster;
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
{
|
2021-11-25 11:55:52 +05:00
|
|
|
public class UserRoleService : CrudServiceBase<UserRoleDto, UserRole>
|
2021-11-24 17:38:40 +05:00
|
|
|
{
|
|
|
|
private readonly CacheTable<UserRole> cacheUserRoles;
|
2021-12-01 16:09:06 +05:00
|
|
|
private readonly CacheTable<PermissionInfo> cachePermissionsInfo;
|
|
|
|
private readonly CacheTable<Permission> cachePermissions;
|
2021-11-25 17:44:07 +05:00
|
|
|
private int counter = 0;
|
2021-11-24 17:38:40 +05:00
|
|
|
|
2021-11-25 11:55:52 +05:00
|
|
|
public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb) : base(context)
|
2021-11-24 17:38:40 +05:00
|
|
|
{
|
2021-11-25 11:55:52 +05:00
|
|
|
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context);
|
2021-12-01 16:09:06 +05:00
|
|
|
cachePermissionsInfo = cacheDb.GetCachedTable<PermissionInfo>((AsbCloudDbContext)context);
|
|
|
|
cachePermissions =
|
2021-11-30 17:22:38 +05:00
|
|
|
cacheDb.GetCachedTable<Permission>((AsbCloudDbContext)context);
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
|
|
|
|
2021-11-25 12:24:53 +05:00
|
|
|
public override async Task<PaginationContainer<UserRoleDto>> GetPageAsync(int skip = 0,
|
|
|
|
int take = 32, CancellationToken token = default)
|
2021-11-24 17:38:40 +05:00
|
|
|
{
|
2021-11-25 12:24:53 +05:00
|
|
|
var rolesDtos = await base.GetPageAsync(skip, take,token);
|
2021-11-24 17:38:40 +05:00
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
rolesDtos.Items = rolesDtos.Items.Select(FillUserRoleWithPermissions).ToList();
|
|
|
|
|
2021-11-25 12:24:53 +05:00
|
|
|
return rolesDtos;
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
public override async Task<UserRoleDto> GetAsync(int id, CancellationToken token = default)
|
2021-11-24 17:38:40 +05:00
|
|
|
{
|
2021-11-29 12:39:28 +05:00
|
|
|
var roleDto = await base.GetAsync(id,token);
|
2021-11-25 17:44:07 +05:00
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
return roleDto is null
|
|
|
|
? null
|
|
|
|
: FillUserRoleWithPermissions(roleDto);
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
|
|
|
|
2021-12-02 13:35:15 +05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
public override async Task<int> UpdateAsync(int id, UserRoleDto item, CancellationToken token = default)
|
|
|
|
{
|
2021-12-01 16:09:06 +05:00
|
|
|
foreach (var p in item.Permissions)
|
|
|
|
p.IdUserRole = item.Id;
|
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
var result = await base.UpdateAsync(id, item, token);
|
2021-11-24 17:38:40 +05:00
|
|
|
|
2021-12-01 16:09:06 +05:00
|
|
|
await cachePermissions.RemoveAsync(r => r.IdUserRole == item.Id, token)
|
2021-11-29 12:39:28 +05:00
|
|
|
.ConfigureAwait(false);
|
2021-11-24 17:38:40 +05:00
|
|
|
|
2021-12-01 16:09:06 +05:00
|
|
|
var newPermissions = item.Permissions.Adapt<Permission>();
|
|
|
|
|
|
|
|
await cachePermissions.InsertAsync(newPermissions, token);
|
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
return result;
|
|
|
|
}
|
2021-11-26 17:05:41 +05:00
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
private UserRoleDto FillUserRoleWithPermissions(UserRoleDto roleDto)
|
|
|
|
{
|
2021-12-01 16:09:06 +05:00
|
|
|
roleDto.Permissions = cachePermissions.Where(c =>
|
|
|
|
c.IdUserRole == roleDto.Id).Adapt<PermissionDto>();
|
2021-11-29 12:39:28 +05:00
|
|
|
|
|
|
|
return roleDto;
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
2021-11-25 17:44:07 +05:00
|
|
|
|
2021-12-02 13:35:15 +05:00
|
|
|
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);
|
|
|
|
}
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
|
|
|
}
|