diff --git a/AsbCloudInfrastructure/Repository/UserRepository.cs b/AsbCloudInfrastructure/Repository/UserRepository.cs index 1502375a..b38b923a 100644 --- a/AsbCloudInfrastructure/Repository/UserRepository.cs +++ b/AsbCloudInfrastructure/Repository/UserRepository.cs @@ -152,7 +152,7 @@ namespace AsbCloudInfrastructure.Repository { var roles = GetRolesByIdUser(idUser, 7); if (roles is null) - return Enumerable.Empty(); ; + return Enumerable.Empty(); var permissions = roles .Where(r => r.Permissions is not null) .SelectMany(r => r.Permissions); @@ -175,7 +175,7 @@ namespace AsbCloudInfrastructure.Repository } private IEnumerable? GetRolesNamesByIdUser(int idUser) - => GetRolesByIdUser(idUser) + => GetRolesByIdUser(idUser, 7) ?.Select(r => r.Caption) .Distinct(); diff --git a/AsbCloudInfrastructure/Repository/UserRoleRepository.cs b/AsbCloudInfrastructure/Repository/UserRoleRepository.cs index 91b4a3b1..61bb1aef 100644 --- a/AsbCloudInfrastructure/Repository/UserRoleRepository.cs +++ b/AsbCloudInfrastructure/Repository/UserRoleRepository.cs @@ -5,6 +5,7 @@ using AsbCloudApp.Repositories; using AsbCloudDb; using AsbCloudDb.Model; using AsbCloudInfrastructure.EfCache; +using DocumentFormat.OpenXml.Drawing; using Mapster; using Microsoft.EntityFrameworkCore; using System; @@ -56,7 +57,7 @@ namespace AsbCloudInfrastructure.Repository if (dtos is null) return Enumerable.Empty(); - return dtos; + return dtos.Select(Convert); } @@ -65,7 +66,7 @@ namespace AsbCloudInfrastructure.Repository var dto = GetCacheUserRole().FirstOrDefault(x => x.Id == id); if (dto is null) return null; - return dto; + return Convert(dto); } public async Task GetOrDefaultAsync(int id, CancellationToken token) @@ -74,7 +75,7 @@ namespace AsbCloudInfrastructure.Repository .ConfigureAwait(false)).FirstOrDefault(r => r.Id == id); if (dto is null) return null; - return dto; + return Convert(dto); } public async Task> GetByNamesAsync(IEnumerable names, CancellationToken token) @@ -88,7 +89,7 @@ namespace AsbCloudInfrastructure.Repository if (dtos?.Count() != names.Count()) throw new ArgumentInvalidException("Invalid role names", nameof(names)); - return dtos; + return dtos.Select(Convert); } public async Task UpdateAsync(UserRoleDto dto, CancellationToken token) @@ -105,13 +106,12 @@ namespace AsbCloudInfrastructure.Repository public IEnumerable GetNestedById(int id, int recursionLevel = 7) { - var dto = GetCacheUserRole() + var role = GetCacheUserRole() .FirstOrDefault(r => r.Id == id); - if (dto is null) + if (role is null) return Enumerable.Empty(); - var role = Convert(dto); - var roles = new SortedSet(ComparerIId.GetInstance()) { dto }; + var roles = new SortedSet(ComparerIId.GetInstance()) { Convert(role) }; if (recursionLevel <= 0 || role.RelationUserRoleUserRoles?.Any() != true) return roles; @@ -134,7 +134,7 @@ namespace AsbCloudInfrastructure.Repository if (dto is not null) { var entity = Convert(dto); - var removeEntity = dbContext.UserRoles.Remove(entity); + var removeEntity = dbContext.UserRoles.Remove(Convert(entity)); await dbContext.SaveChangesAsync(token); DropCacheUserRole(); return removeEntity?.Entity?.Id ?? 0; @@ -144,11 +144,10 @@ namespace AsbCloudInfrastructure.Repository public async Task DeleteAsync(IEnumerable 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(); dbContext.UserRoles.RemoveRange(entities); await dbContext.SaveChangesAsync(token); @@ -176,7 +175,7 @@ namespace AsbCloudInfrastructure.Repository var roles = dtos.Select(Convert); foreach (var role in roles) - if (HasPermission(role, idPermissionInfo)) + if (HasPermission(Convert(role), idPermissionInfo)) return true; return false; } @@ -194,7 +193,7 @@ namespace AsbCloudInfrastructure.Repository var dto = GetCacheUserRole() .First(p => p.Id == relation.IdInclude); var includedRole = Convert(dto); - if (HasPermission(includedRole, idPermission, --recursionLevel)) + if (HasPermission(Convert(includedRole), idPermission, --recursionLevel)) return true; } return false; @@ -243,16 +242,18 @@ namespace AsbCloudInfrastructure.Repository } } - private Task> GetCacheUserRoleAsync(CancellationToken token) + private Task> GetCacheUserRoleAsync(CancellationToken token) => dbContext.UserRoles .Include(r => r.RelationUserRolePermissions) .Include(r => r.RelationUserRoleUserRoles) - .FromCacheAsync(userRoleCacheTag, relationCacheObsolence, Convert, token); - private IEnumerable GetCacheUserRole() + .Include(r => r.RelationUsersUserRoles) + .FromCacheAsync(userRoleCacheTag, relationCacheObsolence, token); + private IEnumerable GetCacheUserRole() => dbContext.UserRoles .Include(r => r.RelationUserRolePermissions) .Include(r => r.RelationUserRoleUserRoles) - .FromCache(userRoleCacheTag, relationCacheObsolence, Convert); + .Include(r => r.RelationUsersUserRoles) + .FromCache(userRoleCacheTag, relationCacheObsolence); private void DropCacheUserRole() => dbContext.RelationUserUserRoles.DropCache(userRoleCacheTag); @@ -296,8 +297,8 @@ namespace AsbCloudInfrastructure.Repository { var rolesCache = GetCacheUserRole(); dto.Roles = entity.RelationUserRoleUserRoles - .Select(rel => rolesCache - .First(r => r.Id == rel.IdInclude)) + .Select(rel => Convert(rolesCache + .First(r => r.Id == rel.IdInclude))) .ToArray(); } return dto;