2022-04-11 18:00:34 +05:00
|
|
|
using AsbCloudApp.Comparators;
|
2021-11-24 17:38:40 +05:00
|
|
|
using AsbCloudApp.Data;
|
2022-04-11 18:00:34 +05:00
|
|
|
using AsbCloudApp.Exceptions;
|
|
|
|
using AsbCloudApp.Services;
|
2021-11-24 17:38:40 +05:00
|
|
|
using AsbCloudDb.Model;
|
|
|
|
using AsbCloudInfrastructure.Services.Cache;
|
|
|
|
using Mapster;
|
2021-12-11 16:46:04 +05:00
|
|
|
using System;
|
2022-04-11 18:00:34 +05:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2021-11-24 17:38:40 +05:00
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
{
|
2021-12-03 15:03:33 +05:00
|
|
|
public class UserRoleService : IUserRoleService
|
2021-11-24 17:38:40 +05:00
|
|
|
{
|
|
|
|
private readonly CacheTable<UserRole> cacheUserRoles;
|
2022-01-13 15:51:06 +05:00
|
|
|
private readonly CacheTable<RelationUserRolePermission> cacheRelationUserRolePermissions;
|
|
|
|
private readonly CacheTable<RelationUserRoleUserRole> cacheRelationUserRoleUserRole;
|
2021-12-22 11:35:36 +05:00
|
|
|
|
|
|
|
public ISet<string> Includes { get; } = new SortedSet<string>();
|
2021-12-03 15:03:33 +05:00
|
|
|
|
2021-12-20 15:17:09 +05:00
|
|
|
public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb)
|
2021-11-24 17:38:40 +05:00
|
|
|
{
|
2022-01-13 14:36:27 +05:00
|
|
|
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context, nameof(UserRole.RelationUserRolePermissions), nameof(UserRole.RelationUserRoleUserRoles));
|
2022-01-13 15:51:06 +05:00
|
|
|
cacheRelationUserRolePermissions = cacheDb.GetCachedTable<RelationUserRolePermission>((AsbCloudDbContext)context, nameof(RelationUserRolePermission.Permission));
|
|
|
|
cacheRelationUserRoleUserRole = cacheDb.GetCachedTable<RelationUserRoleUserRole>((AsbCloudDbContext)context, nameof(RelationUserRoleUserRole.IncludeRole), nameof(RelationUserRoleUserRole.Role));
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
|
|
|
|
2021-12-11 16:46:04 +05:00
|
|
|
public async Task<int> InsertAsync(UserRoleDto dto, CancellationToken token = default)
|
|
|
|
{
|
|
|
|
var entity = dto.Adapt<UserRole>();
|
|
|
|
var updatedEntity = await cacheUserRoles.InsertAsync(entity, token)
|
|
|
|
.ConfigureAwait(false);
|
2021-12-20 15:17:09 +05:00
|
|
|
dto.Id = updatedEntity.Id;
|
2021-12-11 16:46:04 +05:00
|
|
|
await UpdatePermissionsAsync(dto, token);
|
2022-01-13 15:51:06 +05:00
|
|
|
await UpdateIncludedRolesAsync(dto, token);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
2021-12-11 16:46:04 +05:00
|
|
|
await cacheUserRoles.RefreshAsync(true, token)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
return updatedEntity?.Id ?? 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<int> InsertRangeAsync(IEnumerable<UserRoleDto> dtos, CancellationToken token = default)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
//var entities = dtos.Adapt<UserRole>();
|
|
|
|
//return await cacheUserRoles.InsertAsync(entities, token).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
2021-12-03 15:03:33 +05:00
|
|
|
public async Task<IEnumerable<UserRoleDto>> GetAllAsync(CancellationToken token = default)
|
2021-11-24 17:38:40 +05:00
|
|
|
{
|
2021-12-03 15:03:33 +05:00
|
|
|
var entities = await cacheUserRoles.WhereAsync(token)
|
|
|
|
.ConfigureAwait(false);
|
2021-12-20 15:17:09 +05:00
|
|
|
var dtos = entities?.Select(Convert);
|
2021-12-03 15:03:33 +05:00
|
|
|
return dtos;
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
2022-06-06 15:43:47 +05:00
|
|
|
public UserRoleDto Get(int id)
|
|
|
|
{
|
|
|
|
var entity = cacheUserRoles.FirstOrDefault(r => r.Id == id);
|
|
|
|
if (entity is null)
|
|
|
|
return null;
|
|
|
|
var dto = Convert(entity);
|
|
|
|
return dto;
|
|
|
|
}
|
2021-11-24 17:38:40 +05:00
|
|
|
|
2021-12-03 15:03:33 +05:00
|
|
|
public async Task<UserRoleDto> GetAsync(int id, CancellationToken token = default)
|
2021-11-24 17:38:40 +05:00
|
|
|
{
|
2022-04-11 18:00:34 +05:00
|
|
|
var entity = await cacheUserRoles.FirstOrDefaultAsync(r => r.Id == id, token)
|
2021-12-03 15:03:33 +05:00
|
|
|
.ConfigureAwait(false);
|
2021-12-20 15:17:09 +05:00
|
|
|
if (entity is null)
|
|
|
|
return null;
|
|
|
|
var dto = Convert(entity);
|
2021-12-03 15:03:33 +05:00
|
|
|
return dto;
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
|
|
|
|
2021-12-11 16:46:04 +05:00
|
|
|
public async Task<UserRoleDto> GetByNameAsync(string name, CancellationToken token = default)
|
2021-12-02 13:35:15 +05:00
|
|
|
{
|
2021-12-11 16:46:04 +05:00
|
|
|
var entity = await cacheUserRoles.FirstOrDefaultAsync(r => r.Caption == name, token)
|
2021-12-07 18:27:52 +05:00
|
|
|
.ConfigureAwait(false);
|
2021-12-20 15:17:09 +05:00
|
|
|
if (entity is null)
|
|
|
|
return null;
|
|
|
|
var dto = Convert(entity);
|
2021-12-11 16:46:04 +05:00
|
|
|
return dto;
|
2021-12-03 15:03:33 +05:00
|
|
|
}
|
|
|
|
|
2021-12-23 18:07:20 +05:00
|
|
|
public async Task<IEnumerable<UserRoleDto>> GetByNamesAsync(IEnumerable<string> names, CancellationToken token = default)
|
|
|
|
{
|
|
|
|
if (names?.Any() != true)
|
|
|
|
return null;
|
|
|
|
var entities = await cacheUserRoles.WhereAsync(r => names.Contains(r.Caption), token)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
if (entities?.Count() != names.Count())
|
2022-01-18 11:04:15 +05:00
|
|
|
throw new ArgumentInvalidException("Invalid role names", nameof(names));
|
2021-12-23 18:07:20 +05:00
|
|
|
var dtos = entities.Select(Convert);
|
|
|
|
return dtos;
|
|
|
|
}
|
|
|
|
|
2022-06-09 11:19:52 +05:00
|
|
|
public async Task<int> UpdateAsync(UserRoleDto dto, CancellationToken token = default)
|
2021-12-03 15:03:33 +05:00
|
|
|
{
|
2021-12-20 15:17:09 +05:00
|
|
|
var entity = Convert(dto);
|
2021-12-11 16:46:04 +05:00
|
|
|
await UpdatePermissionsAsync(dto, token);
|
2022-01-13 15:51:06 +05:00
|
|
|
await UpdateIncludedRolesAsync(dto, token);
|
2021-12-11 16:46:04 +05:00
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
var result = await cacheUserRoles.UpsertAsync(entity, token)
|
2021-12-03 15:03:33 +05:00
|
|
|
.ConfigureAwait(false);
|
2022-06-06 15:43:47 +05:00
|
|
|
return result;
|
2021-12-11 16:46:04 +05:00
|
|
|
}
|
2021-12-03 15:03:33 +05:00
|
|
|
|
2022-01-13 14:36:27 +05:00
|
|
|
public IEnumerable<UserRoleDto> GetNestedById(int id, int recursionLevel = 7)
|
2021-12-11 16:46:04 +05:00
|
|
|
{
|
|
|
|
var role = cacheUserRoles.FirstOrDefault(r => r.Id == id);
|
|
|
|
if (role is null)
|
|
|
|
return null;
|
2021-12-20 15:17:09 +05:00
|
|
|
var dto = Convert(role);
|
2022-04-11 18:00:34 +05:00
|
|
|
var roles = new SortedSet<UserRoleDto>(ComparerIId.GetInstance()) { dto };
|
2022-01-13 14:36:27 +05:00
|
|
|
|
|
|
|
if (recursionLevel <= 0 || role.RelationUserRoleUserRoles?.Any() != true)
|
|
|
|
return roles;
|
|
|
|
|
|
|
|
foreach (var relation in role.RelationUserRoleUserRoles)
|
|
|
|
{
|
|
|
|
var nestedRoles = GetNestedById(relation.IdInclude, --recursionLevel);
|
|
|
|
if (nestedRoles?.Any() != true)
|
|
|
|
continue;
|
|
|
|
foreach (var nestedRole in nestedRoles)
|
|
|
|
roles.Add(nestedRole);
|
|
|
|
}
|
|
|
|
return roles;
|
2021-12-11 16:46:04 +05:00
|
|
|
}
|
2021-12-03 15:03:33 +05:00
|
|
|
|
2022-01-13 15:51:06 +05:00
|
|
|
private async Task UpdatePermissionsAsync(UserRoleDto dto, CancellationToken token)
|
2021-12-11 16:46:04 +05:00
|
|
|
{
|
2022-01-13 15:51:06 +05:00
|
|
|
if (dto?.Permissions is null)
|
2021-12-20 15:17:09 +05:00
|
|
|
return;
|
|
|
|
|
2022-01-13 15:51:06 +05:00
|
|
|
await cacheRelationUserRolePermissions.RemoveAsync(r => r.IdUserRole == dto.Id, token)
|
2021-12-11 16:46:04 +05:00
|
|
|
.ConfigureAwait(false);
|
2021-12-20 15:17:09 +05:00
|
|
|
|
2022-01-13 15:51:06 +05:00
|
|
|
if (dto.Permissions.Any())
|
2021-12-11 16:46:04 +05:00
|
|
|
{
|
2022-01-13 15:51:06 +05:00
|
|
|
var newRelationRoleToPermission = dto.Permissions.Select(p => new RelationUserRolePermission
|
|
|
|
{
|
|
|
|
IdPermission = p.Id,
|
|
|
|
IdUserRole = dto.Id,
|
|
|
|
});
|
2021-12-11 16:46:04 +05:00
|
|
|
|
2022-01-13 15:51:06 +05:00
|
|
|
await cacheRelationUserRolePermissions.InsertAsync(newRelationRoleToPermission, token)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task UpdateIncludedRolesAsync(UserRoleDto dto, CancellationToken token)
|
|
|
|
{
|
|
|
|
if (dto?.Roles is null)
|
2022-04-11 18:00:34 +05:00
|
|
|
return;
|
2022-01-13 15:51:06 +05:00
|
|
|
|
|
|
|
await cacheRelationUserRoleUserRole.RemoveAsync(rel => rel.Id == dto.Id, token);
|
|
|
|
|
|
|
|
if (dto.Roles.Any())
|
|
|
|
{
|
2022-04-11 18:00:34 +05:00
|
|
|
var newRelations = dto.Roles.Select(r => new RelationUserRoleUserRole { Id = dto.Id, IdInclude = r.Id });
|
2022-01-13 15:51:06 +05:00
|
|
|
await cacheRelationUserRoleUserRole.UpsertAsync(newRelations, token);
|
|
|
|
}
|
2021-11-29 12:39:28 +05:00
|
|
|
}
|
2021-11-26 17:05:41 +05:00
|
|
|
|
2021-12-03 15:03:33 +05:00
|
|
|
public Task<int> DeleteAsync(int id, CancellationToken token = default)
|
|
|
|
=> cacheUserRoles.RemoveAsync(r => r.Id == id, token);
|
|
|
|
|
|
|
|
public Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default)
|
|
|
|
=> cacheUserRoles.RemoveAsync(r => ids.Contains(r.Id), token);
|
2021-12-11 16:46:04 +05:00
|
|
|
|
2021-12-16 16:00:47 +05:00
|
|
|
public bool HasPermission(IEnumerable<int> rolesIds, string permissionName)
|
2021-12-11 16:46:04 +05:00
|
|
|
{
|
2022-01-13 15:51:06 +05:00
|
|
|
var permissionInfo = cacheRelationUserRolePermissions
|
2021-12-20 15:17:09 +05:00
|
|
|
.FirstOrDefault(p => p.Permission?.Name.ToLower() == permissionName.ToLower())
|
|
|
|
?.Permission;
|
2021-12-11 16:46:04 +05:00
|
|
|
|
|
|
|
if (permissionInfo is null)
|
|
|
|
return false;
|
|
|
|
|
2022-01-31 11:09:24 +05:00
|
|
|
if (rolesIds.Contains(1))
|
|
|
|
return true;
|
|
|
|
|
2021-12-11 16:46:04 +05:00
|
|
|
var idPermissionInfo = permissionInfo.Id;
|
|
|
|
var roles = cacheUserRoles.Where(r => rolesIds.Contains(r.Id));
|
|
|
|
foreach (var role in roles)
|
2021-12-16 16:00:47 +05:00
|
|
|
if (HasPermission(role, idPermissionInfo))
|
2021-12-11 16:46:04 +05:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-15 16:21:52 +05:00
|
|
|
private bool HasPermission(UserRole userRole, int idPermission, int recursionLevel = 7)
|
2021-12-11 16:46:04 +05:00
|
|
|
{
|
2021-12-15 16:21:52 +05:00
|
|
|
if (userRole.RelationUserRolePermissions.Any(p => p.IdPermission == idPermission))
|
2021-12-11 16:46:04 +05:00
|
|
|
return true;
|
2022-01-13 14:36:27 +05:00
|
|
|
|
|
|
|
if (recursionLevel <= 0 || userRole.RelationUserRoleUserRoles?.Any() != true)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
foreach (var relation in userRole.RelationUserRoleUserRoles)
|
2021-12-11 16:46:04 +05:00
|
|
|
{
|
2022-01-13 14:36:27 +05:00
|
|
|
var includedRole = cacheUserRoles.First(p => p.Id == relation.IdInclude);
|
|
|
|
if (HasPermission(includedRole, idPermission, --recursionLevel))
|
|
|
|
return true;
|
2021-12-11 16:46:04 +05:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-20 15:17:09 +05:00
|
|
|
|
|
|
|
private static UserRole Convert(UserRoleDto dto)
|
|
|
|
{
|
|
|
|
var entity = dto.Adapt<UserRole>();
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
private UserRoleDto Convert(UserRole entity)
|
|
|
|
{
|
|
|
|
var dto = entity.Adapt<UserRoleDto>();
|
2022-04-11 18:00:34 +05:00
|
|
|
if (entity.RelationUserRolePermissions?.Any() == true)
|
2021-12-20 15:17:09 +05:00
|
|
|
{
|
2022-01-13 15:51:06 +05:00
|
|
|
dto.Permissions = cacheRelationUserRolePermissions
|
2021-12-22 11:35:36 +05:00
|
|
|
.Where(r => entity.Id == r.IdUserRole)
|
2021-12-20 15:17:09 +05:00
|
|
|
.Select(r => Convert(r.Permission));
|
|
|
|
}
|
2022-01-13 15:51:06 +05:00
|
|
|
|
|
|
|
if (entity.RelationUserRoleUserRoles?.Any() == true)
|
|
|
|
{
|
2022-04-11 18:00:34 +05:00
|
|
|
dto.Roles = entity.RelationUserRoleUserRoles.Select(rel =>
|
|
|
|
{
|
2022-01-13 15:51:06 +05:00
|
|
|
var includedRole = cacheUserRoles.First(r => r.Id == rel.IdInclude);
|
|
|
|
return Convert(includedRole);
|
|
|
|
}).ToList();
|
|
|
|
}
|
2021-12-20 15:17:09 +05:00
|
|
|
return dto;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static PermissionDto Convert(Permission entity)
|
|
|
|
{
|
|
|
|
var dto = entity.Adapt<PermissionDto>();
|
|
|
|
return dto;
|
|
|
|
}
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
|
|
|
}
|