forked from ddrilling/AsbCloudServer
#7205798 Перенос сервиса пользователей и ролей в репозиторий
This commit is contained in:
parent
4a2a94ddfb
commit
1348b1090b
@ -152,7 +152,7 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
{
|
{
|
||||||
var roles = GetRolesByIdUser(idUser, 7);
|
var roles = GetRolesByIdUser(idUser, 7);
|
||||||
if (roles is null)
|
if (roles is null)
|
||||||
return Enumerable.Empty<PermissionDto>(); ;
|
return Enumerable.Empty<PermissionDto>();
|
||||||
var permissions = roles
|
var permissions = roles
|
||||||
.Where(r => r.Permissions is not null)
|
.Where(r => r.Permissions is not null)
|
||||||
.SelectMany(r => r.Permissions);
|
.SelectMany(r => r.Permissions);
|
||||||
@ -175,7 +175,7 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<string>? GetRolesNamesByIdUser(int idUser)
|
private IEnumerable<string>? GetRolesNamesByIdUser(int idUser)
|
||||||
=> GetRolesByIdUser(idUser)
|
=> GetRolesByIdUser(idUser, 7)
|
||||||
?.Select(r => r.Caption)
|
?.Select(r => r.Caption)
|
||||||
.Distinct();
|
.Distinct();
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ using AsbCloudApp.Repositories;
|
|||||||
using AsbCloudDb;
|
using AsbCloudDb;
|
||||||
using AsbCloudDb.Model;
|
using AsbCloudDb.Model;
|
||||||
using AsbCloudInfrastructure.EfCache;
|
using AsbCloudInfrastructure.EfCache;
|
||||||
|
using DocumentFormat.OpenXml.Drawing;
|
||||||
using Mapster;
|
using Mapster;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System;
|
using System;
|
||||||
@ -56,7 +57,7 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
if (dtos is null)
|
if (dtos is null)
|
||||||
return Enumerable.Empty<UserRoleDto>();
|
return Enumerable.Empty<UserRoleDto>();
|
||||||
|
|
||||||
return dtos;
|
return dtos.Select(Convert);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +66,7 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
var dto = GetCacheUserRole().FirstOrDefault(x => x.Id == id);
|
var dto = GetCacheUserRole().FirstOrDefault(x => x.Id == id);
|
||||||
if (dto is null)
|
if (dto is null)
|
||||||
return null;
|
return null;
|
||||||
return dto;
|
return Convert(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<UserRoleDto?> GetOrDefaultAsync(int id, CancellationToken token)
|
public async Task<UserRoleDto?> GetOrDefaultAsync(int id, CancellationToken token)
|
||||||
@ -74,7 +75,7 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
.ConfigureAwait(false)).FirstOrDefault(r => r.Id == id);
|
.ConfigureAwait(false)).FirstOrDefault(r => r.Id == id);
|
||||||
if (dto is null)
|
if (dto is null)
|
||||||
return null;
|
return null;
|
||||||
return dto;
|
return Convert(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<UserRoleDto>> GetByNamesAsync(IEnumerable<string> names, CancellationToken token)
|
public async Task<IEnumerable<UserRoleDto>> GetByNamesAsync(IEnumerable<string> names, CancellationToken token)
|
||||||
@ -88,7 +89,7 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
if (dtos?.Count() != names.Count())
|
if (dtos?.Count() != names.Count())
|
||||||
throw new ArgumentInvalidException("Invalid role names", nameof(names));
|
throw new ArgumentInvalidException("Invalid role names", nameof(names));
|
||||||
|
|
||||||
return dtos;
|
return dtos.Select(Convert);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> UpdateAsync(UserRoleDto dto, CancellationToken token)
|
public async Task<int> UpdateAsync(UserRoleDto dto, CancellationToken token)
|
||||||
@ -105,13 +106,12 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
|
|
||||||
public IEnumerable<UserRoleDto> GetNestedById(int id, int recursionLevel = 7)
|
public IEnumerable<UserRoleDto> GetNestedById(int id, int recursionLevel = 7)
|
||||||
{
|
{
|
||||||
var dto = GetCacheUserRole()
|
var role = GetCacheUserRole()
|
||||||
.FirstOrDefault(r => r.Id == id);
|
.FirstOrDefault(r => r.Id == id);
|
||||||
if (dto is null)
|
if (role is null)
|
||||||
return Enumerable.Empty<UserRoleDto>();
|
return Enumerable.Empty<UserRoleDto>();
|
||||||
var role = Convert(dto);
|
|
||||||
|
|
||||||
var roles = new SortedSet<UserRoleDto>(ComparerIId.GetInstance()) { dto };
|
var roles = new SortedSet<UserRoleDto>(ComparerIId.GetInstance()) { Convert(role) };
|
||||||
|
|
||||||
if (recursionLevel <= 0 || role.RelationUserRoleUserRoles?.Any() != true)
|
if (recursionLevel <= 0 || role.RelationUserRoleUserRoles?.Any() != true)
|
||||||
return roles;
|
return roles;
|
||||||
@ -134,7 +134,7 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
if (dto is not null)
|
if (dto is not null)
|
||||||
{
|
{
|
||||||
var entity = Convert(dto);
|
var entity = Convert(dto);
|
||||||
var removeEntity = dbContext.UserRoles.Remove(entity);
|
var removeEntity = dbContext.UserRoles.Remove(Convert(entity));
|
||||||
await dbContext.SaveChangesAsync(token);
|
await dbContext.SaveChangesAsync(token);
|
||||||
DropCacheUserRole();
|
DropCacheUserRole();
|
||||||
return removeEntity?.Entity?.Id ?? 0;
|
return removeEntity?.Entity?.Id ?? 0;
|
||||||
@ -144,11 +144,10 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
|
|
||||||
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
|
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
|
||||||
{
|
{
|
||||||
var dtos = (await GetCacheUserRoleAsync(token)).Where(r => ids.Contains(r.Id));
|
var entities = (await GetCacheUserRoleAsync(token)).Where(r => ids.Contains(r.Id));
|
||||||
|
|
||||||
if (dtos is not null)
|
if (entities is not null)
|
||||||
{
|
{
|
||||||
var entities = dtos.Select(Convert);
|
|
||||||
var count = entities.Count();
|
var count = entities.Count();
|
||||||
dbContext.UserRoles.RemoveRange(entities);
|
dbContext.UserRoles.RemoveRange(entities);
|
||||||
await dbContext.SaveChangesAsync(token);
|
await dbContext.SaveChangesAsync(token);
|
||||||
@ -176,7 +175,7 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
var roles = dtos.Select(Convert);
|
var roles = dtos.Select(Convert);
|
||||||
|
|
||||||
foreach (var role in roles)
|
foreach (var role in roles)
|
||||||
if (HasPermission(role, idPermissionInfo))
|
if (HasPermission(Convert(role), idPermissionInfo))
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -194,7 +193,7 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
var dto = GetCacheUserRole()
|
var dto = GetCacheUserRole()
|
||||||
.First(p => p.Id == relation.IdInclude);
|
.First(p => p.Id == relation.IdInclude);
|
||||||
var includedRole = Convert(dto);
|
var includedRole = Convert(dto);
|
||||||
if (HasPermission(includedRole, idPermission, --recursionLevel))
|
if (HasPermission(Convert(includedRole), idPermission, --recursionLevel))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -243,16 +242,18 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task<IEnumerable<UserRoleDto>> GetCacheUserRoleAsync(CancellationToken token)
|
private Task<IEnumerable<UserRole>> GetCacheUserRoleAsync(CancellationToken token)
|
||||||
=> dbContext.UserRoles
|
=> dbContext.UserRoles
|
||||||
.Include(r => r.RelationUserRolePermissions)
|
.Include(r => r.RelationUserRolePermissions)
|
||||||
.Include(r => r.RelationUserRoleUserRoles)
|
.Include(r => r.RelationUserRoleUserRoles)
|
||||||
.FromCacheAsync(userRoleCacheTag, relationCacheObsolence, Convert, token);
|
.Include(r => r.RelationUsersUserRoles)
|
||||||
private IEnumerable<UserRoleDto> GetCacheUserRole()
|
.FromCacheAsync(userRoleCacheTag, relationCacheObsolence, token);
|
||||||
|
private IEnumerable<UserRole> GetCacheUserRole()
|
||||||
=> dbContext.UserRoles
|
=> dbContext.UserRoles
|
||||||
.Include(r => r.RelationUserRolePermissions)
|
.Include(r => r.RelationUserRolePermissions)
|
||||||
.Include(r => r.RelationUserRoleUserRoles)
|
.Include(r => r.RelationUserRoleUserRoles)
|
||||||
.FromCache(userRoleCacheTag, relationCacheObsolence, Convert);
|
.Include(r => r.RelationUsersUserRoles)
|
||||||
|
.FromCache(userRoleCacheTag, relationCacheObsolence);
|
||||||
private void DropCacheUserRole()
|
private void DropCacheUserRole()
|
||||||
=> dbContext.RelationUserUserRoles.DropCache(userRoleCacheTag);
|
=> dbContext.RelationUserUserRoles.DropCache(userRoleCacheTag);
|
||||||
|
|
||||||
@ -296,8 +297,8 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
{
|
{
|
||||||
var rolesCache = GetCacheUserRole();
|
var rolesCache = GetCacheUserRole();
|
||||||
dto.Roles = entity.RelationUserRoleUserRoles
|
dto.Roles = entity.RelationUserRoleUserRoles
|
||||||
.Select(rel => rolesCache
|
.Select(rel => Convert(rolesCache
|
||||||
.First(r => r.Id == rel.IdInclude))
|
.First(r => r.Id == rel.IdInclude)))
|
||||||
.ToArray();
|
.ToArray();
|
||||||
}
|
}
|
||||||
return dto;
|
return dto;
|
||||||
|
Loading…
Reference in New Issue
Block a user