2021-11-29 12:39:28 +05:00
|
|
|
using System.Collections.Generic;
|
2021-11-25 17:44:07 +05:00
|
|
|
using System.Diagnostics;
|
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-11-29 12:39:28 +05:00
|
|
|
private readonly CacheTable<Permission> cachePermissions;
|
|
|
|
private readonly CacheTable<RelationUserRolePermission> cacheUserRolesPermissions;
|
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-11-29 12:39:28 +05:00
|
|
|
cachePermissions = cacheDb.GetCachedTable<Permission>((AsbCloudDbContext)context);
|
|
|
|
cacheUserRolesPermissions =
|
|
|
|
cacheDb.GetCachedTable<RelationUserRolePermission>((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-11-29 12:39:28 +05:00
|
|
|
public override async Task<int> InsertAsync(UserRoleDto dto, CancellationToken token = default)
|
2021-11-24 17:38:40 +05:00
|
|
|
{
|
2021-11-25 11:55:52 +05:00
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
dto.PermissionIds = GetAncestorsPermissionIds(dto, ref counter);
|
|
|
|
|
|
|
|
var newRoleId = await base.InsertAsync(dto, token);
|
|
|
|
|
|
|
|
if (dto.PermissionIds == default)
|
|
|
|
return newRoleId;
|
|
|
|
|
|
|
|
foreach (var pId in dto.PermissionIds)
|
2021-11-24 17:38:40 +05:00
|
|
|
{
|
2021-11-29 12:39:28 +05:00
|
|
|
var relation = new RelationUserRolePermission()
|
|
|
|
{
|
|
|
|
IdUserRole = newRoleId,
|
|
|
|
IdPermission = pId
|
|
|
|
};
|
|
|
|
|
|
|
|
context.RelationUserRolesPermissions.Add(relation);
|
2021-11-26 17:05:41 +05:00
|
|
|
}
|
2021-11-29 12:39:28 +05:00
|
|
|
|
|
|
|
return await context.SaveChangesAsync(token);
|
|
|
|
}
|
2021-11-24 17:38:40 +05:00
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
public override async Task<int> UpdateAsync(int id, UserRoleDto item, CancellationToken token = default)
|
|
|
|
{
|
|
|
|
var result = await base.UpdateAsync(id, item, token);
|
2021-11-24 17:38:40 +05:00
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
if (item.PermissionIds == default)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
await cacheUserRolesPermissions.RemoveAsync(r => r.IdUserRole == item.Id, token)
|
|
|
|
.ConfigureAwait(false);
|
2021-11-24 17:38:40 +05:00
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
var newRelations = item.PermissionIds.Select(p => new RelationUserRolePermission()
|
|
|
|
{
|
|
|
|
IdUserRole = item.Id,
|
|
|
|
IdPermission = p
|
|
|
|
});
|
|
|
|
await cacheUserRolesPermissions.InsertAsync(newRelations, token);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2021-11-26 17:05:41 +05:00
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
private UserRoleDto FillUserRoleWithPermissions(UserRoleDto roleDto)
|
|
|
|
{
|
|
|
|
var rolePermissionIds = cacheUserRolesPermissions.Where(c =>
|
|
|
|
c.IdUserRole == roleDto.Id).Select(p => p.IdPermission);
|
|
|
|
roleDto.Permissions = cachePermissions.Where(permission => rolePermissionIds.Contains(permission.Id))
|
|
|
|
.Adapt<PermissionDto>();
|
|
|
|
|
|
|
|
return roleDto;
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
2021-11-25 17:44:07 +05:00
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
private IEnumerable<int> GetAncestorsPermissionIds(UserRoleDto userRoleDto, ref int counter)
|
2021-11-25 17:44:07 +05:00
|
|
|
{
|
2021-11-29 12:39:28 +05:00
|
|
|
var idParent = userRoleDto.IdParent;
|
|
|
|
var resultPermissionsIds = userRoleDto.PermissionIds ?? new List<int>();
|
|
|
|
|
|
|
|
if (idParent == default)
|
|
|
|
return resultPermissionsIds;
|
2021-11-25 17:44:07 +05:00
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
if (counter > 10)
|
|
|
|
{
|
|
|
|
Trace.WriteLine($"User role with id: {userRoleDto.Id} has more than 10 nested parents");
|
|
|
|
return resultPermissionsIds;
|
|
|
|
}
|
2021-11-25 17:44:07 +05:00
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
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++;
|
2021-11-25 17:44:07 +05:00
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
return GetAncestorsPermissionIds(parentRole, ref counter);
|
2021-11-25 17:44:07 +05:00
|
|
|
}
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
|
|
|
}
|