forked from ddrilling/AsbCloudServer
#7205798 Перенос сервиса пользователей и ролей в репозиторий
This commit is contained in:
parent
0bab711eb8
commit
5ea6919738
41
AsbCloudApp/Repositories/IUserRepository.cs
Normal file
41
AsbCloudApp/Repositories/IUserRepository.cs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
using AsbCloudApp.Data;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AsbCloudApp.Repositories
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Репозиторий пользователей
|
||||||
|
/// </summary>
|
||||||
|
public interface IUserRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Добавление пользователя
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dto"></param>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<UserExtendedDto> InsertAsync(UserExtendedDto dto, CancellationToken token);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Обновление ролей пользователя
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="idUser"></param>
|
||||||
|
/// <param name="newRoles"></param>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task UpdateRolesCacheForUserAsync(int idUser, IEnumerable<UserRoleDto> newRoles, CancellationToken token);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получить пользователя по логину
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="login"></param>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<UserExtendedDto?> GetUserByLoginAsync(string login, CancellationToken token);
|
||||||
|
}
|
||||||
|
}
|
@ -1,23 +1,24 @@
|
|||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Services;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace AsbCloudApp.Services
|
namespace AsbCloudApp.Repositories
|
||||||
{
|
{
|
||||||
|
#nullable enable
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Репозиторий ролей пользователя
|
/// Разрешения на доступ к данным
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IUserRoleService : ICrudService<UserRoleDto>
|
public interface IUserRoleRepository : ICrudService<UserRoleDto>
|
||||||
{
|
{
|
||||||
// todo: переименовать
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// получить dto по названиям
|
/// получить dto по названиям
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="names"></param>
|
/// <param name="names"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<IEnumerable<UserRoleDto>> GetByNamesAsync(IEnumerable<string> names, CancellationToken token = default);
|
Task<IEnumerable<UserRoleDto>?> GetByNamesAsync(IEnumerable<string> names, CancellationToken token = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// получить все вложенные разрешения
|
/// получить все вложенные разрешения
|
||||||
@ -25,7 +26,7 @@ namespace AsbCloudApp.Services
|
|||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
/// <param name="counter"></param>
|
/// <param name="counter"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IEnumerable<UserRoleDto> GetNestedById(int id, int counter = 10);
|
IEnumerable<UserRoleDto>? GetNestedById(int id, int counter = 10);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// определяет содержится ли разрешение в одной из указанных ролей
|
/// определяет содержится ли разрешение в одной из указанных ролей
|
||||||
@ -35,4 +36,5 @@ namespace AsbCloudApp.Services
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
bool HasPermission(IEnumerable<int> rolesIds, string permissionName);
|
bool HasPermission(IEnumerable<int> rolesIds, string permissionName);
|
||||||
}
|
}
|
||||||
|
#nullable disable
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Repositories;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace AsbCloudApp.Services
|
namespace AsbCloudApp.Services
|
||||||
@ -11,7 +12,7 @@ namespace AsbCloudApp.Services
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Сервис ролей
|
/// Сервис ролей
|
||||||
/// </summary>
|
/// </summary>
|
||||||
IUserRoleService RoleService { get; }
|
IUserRoleRepository RoleService { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получить список всех прав пользователя (включая наследование групп)
|
/// Получить список всех прав пользователя (включая наследование групп)
|
||||||
|
@ -50,7 +50,8 @@ namespace AsbCloudApp.Services
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Сохранение файла
|
/// Сохранение файла
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="idDto"></param>
|
/// <param name="idWell"></param>
|
||||||
|
/// <param name="idCategory"></param>
|
||||||
/// <param name="idUser"></param>
|
/// <param name="idUser"></param>
|
||||||
/// <param name="fileStream"></param>
|
/// <param name="fileStream"></param>
|
||||||
/// <param name="fileName"></param>
|
/// <param name="fileName"></param>
|
||||||
|
@ -28,6 +28,7 @@ namespace AsbCloudDb.Model
|
|||||||
DbSet<RelationUserDrillingProgramPart> RelationDrillingProgramPartUsers { get; }
|
DbSet<RelationUserDrillingProgramPart> RelationDrillingProgramPartUsers { get; }
|
||||||
DbSet<RelationUserRolePermission> RelationUserRolePermissions { get; }
|
DbSet<RelationUserRolePermission> RelationUserRolePermissions { get; }
|
||||||
DbSet<RelationUserUserRole> RelationUserUserRoles { get; }
|
DbSet<RelationUserUserRole> RelationUserUserRoles { get; }
|
||||||
|
DbSet<RelationUserRoleUserRole> RelationUserRoleUserRoles { get; }
|
||||||
DbSet<ReportProperty> ReportProperties { get; }
|
DbSet<ReportProperty> ReportProperties { get; }
|
||||||
DbSet<Subsystem> Subsystems { get; }
|
DbSet<Subsystem> Subsystems { get; }
|
||||||
DbSet<SubsystemOperationTime> SubsystemOperationTimes { get; }
|
DbSet<SubsystemOperationTime> SubsystemOperationTimes { get; }
|
||||||
|
@ -121,15 +121,12 @@ namespace AsbCloudInfrastructure
|
|||||||
services.AddTransient<ITelemetryUserService, TelemetryUserService>();
|
services.AddTransient<ITelemetryUserService, TelemetryUserService>();
|
||||||
services.AddTransient<ITimezoneService, TimezoneService>();
|
services.AddTransient<ITimezoneService, TimezoneService>();
|
||||||
services.AddTransient<IUserService, UserService>();
|
services.AddTransient<IUserService, UserService>();
|
||||||
services.AddTransient<IUserRoleService, UserRoleService>();
|
|
||||||
services.AddTransient<IWellService, WellService>();
|
services.AddTransient<IWellService, WellService>();
|
||||||
services.AddTransient<IWellOperationImportService, WellOperationImportService>();
|
services.AddTransient<IWellOperationImportService, WellOperationImportService>();
|
||||||
services.AddTransient<IWellOperationService, WellOperationService>();
|
services.AddTransient<IWellOperationService, WellOperationService>();
|
||||||
services.AddTransient<IScheduleReportService, ScheduleReportService>();
|
services.AddTransient<IScheduleReportService, ScheduleReportService>();
|
||||||
services.AddTransient<IDailyReportService, DailyReportService>();
|
services.AddTransient<IDailyReportService, DailyReportService>();
|
||||||
services.AddTransient<IDetectedOperationService, DetectedOperationService>();
|
services.AddTransient<IDetectedOperationService, DetectedOperationService>();
|
||||||
//services.AddTransient<IDrillerService, DrillerService>();
|
|
||||||
|
|
||||||
services.AddTransient<ISubsystemOperationTimeService, SubsystemOperationTimeService>();
|
services.AddTransient<ISubsystemOperationTimeService, SubsystemOperationTimeService>();
|
||||||
services.AddTransient<IScheduleRepository, ScheduleRepository>();
|
services.AddTransient<IScheduleRepository, ScheduleRepository>();
|
||||||
services.AddTransient<IRepositoryWellRelated<OperationValueDto>, CrudWellRelatedServiceBase<OperationValueDto, OperationValue>>();
|
services.AddTransient<IRepositoryWellRelated<OperationValueDto>, CrudWellRelatedServiceBase<OperationValueDto, OperationValue>>();
|
||||||
@ -165,6 +162,7 @@ namespace AsbCloudInfrastructure
|
|||||||
services.AddTransient<IFileRepository, FileRepository>();
|
services.AddTransient<IFileRepository, FileRepository>();
|
||||||
services.AddTransient<IFileStorageRepository, FileStorageRepository>();
|
services.AddTransient<IFileStorageRepository, FileStorageRepository>();
|
||||||
services.AddTransient<IWellCompositeRepository, WellCompositeRepository>();
|
services.AddTransient<IWellCompositeRepository, WellCompositeRepository>();
|
||||||
|
services.AddTransient<IUserRoleRepository, UserRoleRepository>();
|
||||||
// Subsystem service
|
// Subsystem service
|
||||||
services.AddTransient<ICrudService<SubsystemDto>, CrudCacheServiceBase<SubsystemDto, Subsystem>>();
|
services.AddTransient<ICrudService<SubsystemDto>, CrudCacheServiceBase<SubsystemDto, Subsystem>>();
|
||||||
services.AddTransient<ISubsystemService, SubsystemService>();
|
services.AddTransient<ISubsystemService, SubsystemService>();
|
||||||
|
102
AsbCloudInfrastructure/Repository/UserRepository.cs
Normal file
102
AsbCloudInfrastructure/Repository/UserRepository.cs
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Repositories;
|
||||||
|
using AsbCloudDb.Model;
|
||||||
|
using AsbCloudInfrastructure.EfCache;
|
||||||
|
using Mapster;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AsbCloudInfrastructure.Repository
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public class UserRepository : IUserRepository
|
||||||
|
{
|
||||||
|
private readonly IAsbCloudDbContext db;
|
||||||
|
private static readonly System.TimeSpan cacheObsolescence = System.TimeSpan.FromMinutes(15);
|
||||||
|
private static readonly TypeAdapterConfig userTypeAdapterConfig = TypeAdapterConfig<UserExtendedDto, User>
|
||||||
|
.NewConfig()
|
||||||
|
.Ignore(dst => dst.Company,
|
||||||
|
dst => dst.FileMarks,
|
||||||
|
dst => dst.Files,
|
||||||
|
dst => dst.RelationUsersUserRoles)
|
||||||
|
.Config;
|
||||||
|
|
||||||
|
public UserRepository(IAsbCloudDbContext db) {
|
||||||
|
this.db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<UserExtendedDto> InsertAsync(UserExtendedDto dto, CancellationToken token)
|
||||||
|
{
|
||||||
|
dto.Id = default;
|
||||||
|
var updatedEntity = await db.Users.AddAsync(Convert(dto), token).ConfigureAwait(false);
|
||||||
|
return Convert(updatedEntity.Entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UpdateRolesCacheForUserAsync(int idUser, IEnumerable<UserRoleDto> newRoles, CancellationToken token)
|
||||||
|
{
|
||||||
|
var entity = await db.RelationUserUserRoles.FirstOrDefaultAsync(x => x.IdUser == idUser, token).ConfigureAwait(false);
|
||||||
|
if (entity is not null)
|
||||||
|
{
|
||||||
|
db.RelationUserUserRoles.Remove(entity);
|
||||||
|
if (newRoles?.Any() == true)
|
||||||
|
await db.RelationUserUserRoles.AddRangeAsync(newRoles.Select(role => new RelationUserUserRole
|
||||||
|
{
|
||||||
|
IdUser = idUser,
|
||||||
|
IdUserRole = role.Id
|
||||||
|
}), token).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<UserExtendedDto?> GetUserByLoginAsync(string login, CancellationToken token)
|
||||||
|
{
|
||||||
|
var entities = await db.Users.FromCacheAsync("Users", cacheObsolescence, token).ConfigureAwait(false);
|
||||||
|
var entity = entities.FirstOrDefault(x => x.Login.ToLower() == login.ToLower());
|
||||||
|
if (entity is not null)
|
||||||
|
return Convert(entity);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<UserExtendedDto>?> GetAllAsync(CancellationToken token = default)
|
||||||
|
{
|
||||||
|
var entities = await db.Users.FromCacheAsync("Users", cacheObsolescence, token).ConfigureAwait(false);
|
||||||
|
if (entities is null)
|
||||||
|
return null;
|
||||||
|
var dtos = entities.Select(Convert).ToList();
|
||||||
|
//for (var i = 0; i < dtos.Count; i++)
|
||||||
|
// dtos[i].RoleNames = GetRolesNamesByIdUser(dtos[i].Id, token);
|
||||||
|
return dtos;
|
||||||
|
}
|
||||||
|
|
||||||
|
//private async Task<IEnumerable<string>> GetRolesNamesByIdUser(int idUser, CancellationToken token)
|
||||||
|
//=> await GetRolesByIdUser(idUser, token)
|
||||||
|
// ?.Select(r => r.Caption)
|
||||||
|
// .Distinct();
|
||||||
|
|
||||||
|
//public async Task<IEnumerable<UserRoleDto>> GetRolesByIdUser(int idUser, CancellationToken token, int nestedLevel = 0)
|
||||||
|
//{
|
||||||
|
// var allRoles = await db.RelationUserUserRoles.FromCacheAsync("RelationUserUserRoles", cacheObsolescence, token).ConfigureAwait(false);
|
||||||
|
// var roles = allRoles.Where(r => r.IdUser == idUser);
|
||||||
|
// if (roles?.Any() != true)
|
||||||
|
// return null;
|
||||||
|
// return roles.SelectMany(r => RoleService.GetNestedById(r.IdUserRole, nestedLevel));
|
||||||
|
//}
|
||||||
|
|
||||||
|
private User Convert(UserExtendedDto dto)
|
||||||
|
{
|
||||||
|
var entity = dto.Adapt<User>(userTypeAdapterConfig);
|
||||||
|
if (string.IsNullOrEmpty(entity.PasswordHash))
|
||||||
|
entity.PasswordHash = db.Users.FirstOrDefault(u => u.Id == dto.Id)?.PasswordHash;
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
private UserExtendedDto Convert(User entity)
|
||||||
|
{
|
||||||
|
var dto = entity.Adapt<UserExtendedDto>();
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#nullable disable
|
||||||
|
}
|
311
AsbCloudInfrastructure/Repository/UserRoleRepository.cs
Normal file
311
AsbCloudInfrastructure/Repository/UserRoleRepository.cs
Normal file
@ -0,0 +1,311 @@
|
|||||||
|
using AsbCloudApp.Comparators;
|
||||||
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Exceptions;
|
||||||
|
using AsbCloudApp.Repositories;
|
||||||
|
using AsbCloudDb;
|
||||||
|
using AsbCloudDb.Model;
|
||||||
|
using AsbCloudInfrastructure.EfCache;
|
||||||
|
using Mapster;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AsbCloudInfrastructure.Repository
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public class UserRoleRepository : IUserRoleRepository
|
||||||
|
{
|
||||||
|
private readonly IAsbCloudDbContext dbContext;
|
||||||
|
private const string userRoleCacheTag = "UserRole";
|
||||||
|
private const string relationUserRoleUserRoleCacheTag = "RelationUserRoleUserRole";
|
||||||
|
private const string relationUserRolePermissionsCacheTag = "RelationUserRolePermissions";
|
||||||
|
private static readonly TimeSpan relationCacheObsolence = TimeSpan.FromMinutes(15);
|
||||||
|
|
||||||
|
public UserRoleRepository(IAsbCloudDbContext dbContext)
|
||||||
|
{
|
||||||
|
this.dbContext = dbContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<int> InsertAsync(UserRoleDto dto, CancellationToken token)
|
||||||
|
{
|
||||||
|
var entity = dto.Adapt<UserRole>();
|
||||||
|
var updatedEntity = await dbContext.UserRoles.AddAsync(entity, token)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
dto.Id = updatedEntity.Entity.Id;
|
||||||
|
await UpdatePermissionsAsync(dto, token);
|
||||||
|
await UpdateIncludedRolesAsync(dto, token);
|
||||||
|
|
||||||
|
dbContext.UserRoles.DropCache();
|
||||||
|
return updatedEntity?.Entity?.Id ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<int> InsertRangeAsync(IEnumerable<UserRoleDto> newItems, CancellationToken token)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<UserRoleDto>> GetAllAsync(CancellationToken token)
|
||||||
|
{
|
||||||
|
var entities = await GetCacheUserRoleAsync(token)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
if (entities is not null)
|
||||||
|
{
|
||||||
|
var dtos = entities.Select(Convert);
|
||||||
|
return dtos;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return new List<UserRoleDto>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserRoleDto? GetOrDefault(int id)
|
||||||
|
{
|
||||||
|
var entity = GetCacheUserRole().FirstOrDefault(x => x.Id == id);
|
||||||
|
if (entity is null)
|
||||||
|
return null;
|
||||||
|
var dto = Convert(entity);
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<UserRoleDto?> GetOrDefaultAsync(int id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var entity = (await GetCacheUserRoleAsync(token)
|
||||||
|
.ConfigureAwait(false)).FirstOrDefault(r => r.Id == id);
|
||||||
|
if (entity is null)
|
||||||
|
return null;
|
||||||
|
var dto = Convert(entity);
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<UserRoleDto>?> GetByNamesAsync(IEnumerable<string> names, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (names?.Any() != true)
|
||||||
|
return null;
|
||||||
|
var entities = (await GetCacheUserRoleAsync(token)).Where(r => names.Contains(r.Caption));
|
||||||
|
if (entities?.Count() != names.Count())
|
||||||
|
throw new ArgumentInvalidException("Invalid role names", nameof(names));
|
||||||
|
var dtos = entities.Select(Convert);
|
||||||
|
return dtos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<int> UpdateAsync(UserRoleDto dto, CancellationToken token)
|
||||||
|
{
|
||||||
|
var entity = Convert(dto);
|
||||||
|
await UpdatePermissionsAsync(dto, token);
|
||||||
|
await UpdateIncludedRolesAsync(dto, token);
|
||||||
|
|
||||||
|
var result = dbContext.UserRoles.Upsert(entity);
|
||||||
|
DropCacheUserRole();
|
||||||
|
return result?.Entity?.Id ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<UserRoleDto>? GetNestedById(int id, int recursionLevel = 7)
|
||||||
|
{
|
||||||
|
var role = GetCacheUserRole().FirstOrDefault(r => r.Id == id);
|
||||||
|
if (role is null)
|
||||||
|
return null;
|
||||||
|
var dto = Convert(role);
|
||||||
|
var roles = new SortedSet<UserRoleDto>(ComparerIId.GetInstance()) { dto };
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<int> DeleteAsync(int id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var entity = (await GetCacheUserRoleAsync(token)).FirstOrDefault(r => r.Id == id);
|
||||||
|
|
||||||
|
if (entity is not null)
|
||||||
|
{
|
||||||
|
var removeEntity = dbContext.UserRoles.Remove(entity);
|
||||||
|
await dbContext.SaveChangesAsync(token);
|
||||||
|
DropCacheUserRole();
|
||||||
|
return removeEntity?.Entity?.Id ?? 0;
|
||||||
|
}
|
||||||
|
else return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
|
||||||
|
{
|
||||||
|
var entities = (await GetCacheUserRoleAsync(token)).Where(r => ids.Contains(r.Id));
|
||||||
|
|
||||||
|
if (entities is not null)
|
||||||
|
{
|
||||||
|
var count = entities.Count();
|
||||||
|
dbContext.UserRoles.RemoveRange(entities);
|
||||||
|
await dbContext.SaveChangesAsync(token);
|
||||||
|
DropCacheUserRole();
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
else return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasPermission(IEnumerable<int> rolesIds, string permissionName)
|
||||||
|
{
|
||||||
|
var permissionInfo = GetCacheRelationUserRolePermissions()
|
||||||
|
.FirstOrDefault(p => p. Permission?.Name.ToLower() == permissionName.ToLower())
|
||||||
|
?.Permission;
|
||||||
|
|
||||||
|
if (permissionInfo is null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (rolesIds.Contains(1))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
var idPermissionInfo = permissionInfo.Id;
|
||||||
|
var roles = GetCacheUserRole()
|
||||||
|
.Where(r => rolesIds.Contains(r.Id));
|
||||||
|
foreach (var role in roles)
|
||||||
|
if (HasPermission(role, idPermissionInfo))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool HasPermission(UserRole userRole, int idPermission, int recursionLevel = 7)
|
||||||
|
{
|
||||||
|
if (userRole.RelationUserRolePermissions.Any(p => p.IdPermission == idPermission))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (recursionLevel <= 0 || userRole.RelationUserRoleUserRoles?.Any() != true)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
foreach (var relation in userRole.RelationUserRoleUserRoles)
|
||||||
|
{
|
||||||
|
var includedRole = GetCacheUserRole()
|
||||||
|
.First(p => p.Id == relation.IdInclude);
|
||||||
|
if (HasPermission(includedRole, idPermission, --recursionLevel))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task UpdateIncludedRolesAsync(UserRoleDto dto, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (dto?.Roles is null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var relations = (await GetCacheRelationUserRoleUserRoleAsync(token).ConfigureAwait(false))
|
||||||
|
.Where(r => r.Id == dto.Id);
|
||||||
|
|
||||||
|
dbContext.RelationUserRoleUserRoles.RemoveRange(relations);
|
||||||
|
|
||||||
|
if (dto.Roles.Any())
|
||||||
|
{
|
||||||
|
var newRelations = dto.Roles.Select(r => new RelationUserRoleUserRole { Id = dto.Id, IdInclude = r.Id });
|
||||||
|
await dbContext.RelationUserRoleUserRoles.AddRangeAsync(newRelations, token);
|
||||||
|
await dbContext.SaveChangesAsync(token);
|
||||||
|
DropCacheRelationUserRoleUserRole();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task UpdatePermissionsAsync(UserRoleDto dto, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (dto?.Permissions is null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var relations = (await GetCacheRelationUserRolePermissionsAsync(token).ConfigureAwait(false))
|
||||||
|
.Where(r => r.IdUserRole == dto.Id);
|
||||||
|
|
||||||
|
dbContext.RelationUserRolePermissions.RemoveRange(relations);
|
||||||
|
|
||||||
|
if (dto.Permissions.Any())
|
||||||
|
{
|
||||||
|
var newRelations = dto.Permissions.Select(p => new RelationUserRolePermission
|
||||||
|
{
|
||||||
|
IdPermission = p.Id,
|
||||||
|
IdUserRole = dto.Id,
|
||||||
|
});
|
||||||
|
|
||||||
|
await dbContext.RelationUserRolePermissions.AddRangeAsync(newRelations, token);
|
||||||
|
await dbContext.SaveChangesAsync(token);
|
||||||
|
DropCacheRelationCompanyWell();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Task<IEnumerable<UserRole>> GetCacheUserRoleAsync(CancellationToken token)
|
||||||
|
=> dbContext.UserRoles
|
||||||
|
.Include(r => r.RelationUserRolePermissions)
|
||||||
|
.Include(r => r.RelationUserRoleUserRoles)
|
||||||
|
.FromCacheAsync(userRoleCacheTag, relationCacheObsolence, token);
|
||||||
|
private IEnumerable<UserRole> GetCacheUserRole()
|
||||||
|
=> dbContext.UserRoles
|
||||||
|
.Include(r => r.RelationUserRolePermissions)
|
||||||
|
.Include(r => r.RelationUserRoleUserRoles)
|
||||||
|
.FromCache(userRoleCacheTag, relationCacheObsolence);
|
||||||
|
private void DropCacheUserRole()
|
||||||
|
=> dbContext.RelationUserUserRoles.DropCache(relationUserRoleUserRoleCacheTag);
|
||||||
|
|
||||||
|
private Task<IEnumerable<RelationUserRoleUserRole>> GetCacheRelationUserRoleUserRoleAsync(CancellationToken token)
|
||||||
|
=> dbContext.RelationUserRoleUserRoles
|
||||||
|
.Include(r => r.IncludeRole)
|
||||||
|
.Include(r => r.Role)
|
||||||
|
.FromCacheAsync(relationUserRoleUserRoleCacheTag, relationCacheObsolence, token);
|
||||||
|
private IEnumerable<RelationUserRoleUserRole> GetCacheRelationUserRoleUserRole()
|
||||||
|
=> dbContext.RelationUserRoleUserRoles
|
||||||
|
.Include(r => r.IncludeRole)
|
||||||
|
.Include(r => r.Role)
|
||||||
|
.FromCache(relationUserRoleUserRoleCacheTag, relationCacheObsolence);
|
||||||
|
private void DropCacheRelationUserRoleUserRole()
|
||||||
|
=> dbContext.RelationUserUserRoles.DropCache(relationUserRoleUserRoleCacheTag);
|
||||||
|
|
||||||
|
private Task<IEnumerable<RelationUserRolePermission>> GetCacheRelationUserRolePermissionsAsync(CancellationToken token)
|
||||||
|
=> dbContext.RelationUserRolePermissions
|
||||||
|
.Include(r => r.UserRole)
|
||||||
|
.Include(r => r.Permission)
|
||||||
|
.FromCacheAsync(relationUserRolePermissionsCacheTag, relationCacheObsolence, token);
|
||||||
|
private IEnumerable<RelationUserRolePermission> GetCacheRelationUserRolePermissions()
|
||||||
|
=> dbContext.RelationUserRolePermissions
|
||||||
|
.Include(r => r.UserRole)
|
||||||
|
.Include(r => r.Permission)
|
||||||
|
.FromCache(relationUserRolePermissionsCacheTag, relationCacheObsolence);
|
||||||
|
private void DropCacheRelationCompanyWell()
|
||||||
|
=> dbContext.RelationUserRolePermissions.DropCache(relationUserRolePermissionsCacheTag);
|
||||||
|
|
||||||
|
private UserRoleDto Convert(UserRole entity)
|
||||||
|
{
|
||||||
|
var dto = entity.Adapt<UserRoleDto>();
|
||||||
|
if (entity.RelationUserRolePermissions?.Any() == true)
|
||||||
|
{
|
||||||
|
dto.Permissions = GetCacheRelationUserRolePermissions()
|
||||||
|
.Where(r => entity.Id == r.IdUserRole)
|
||||||
|
.Select(r => Convert(r.Permission));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entity.RelationUserRoleUserRoles?.Any() == true)
|
||||||
|
{
|
||||||
|
dto.Roles = entity.RelationUserRoleUserRoles.Select(rel =>
|
||||||
|
{
|
||||||
|
var includedRole = GetCacheUserRole().First(r => r.Id == rel.IdInclude);
|
||||||
|
return Convert(includedRole);
|
||||||
|
}).ToList();
|
||||||
|
}
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static PermissionDto Convert(Permission entity)
|
||||||
|
{
|
||||||
|
var dto = entity.Adapt<PermissionDto>();
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static UserRole Convert(UserRoleDto dto)
|
||||||
|
{
|
||||||
|
var entity = dto.Adapt<UserRole>();
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#nullable disable
|
||||||
|
}
|
@ -1,234 +0,0 @@
|
|||||||
using AsbCloudApp.Comparators;
|
|
||||||
using AsbCloudApp.Data;
|
|
||||||
using AsbCloudApp.Exceptions;
|
|
||||||
using AsbCloudApp.Services;
|
|
||||||
using AsbCloudDb.Model;
|
|
||||||
using AsbCloudInfrastructure.Services.Cache;
|
|
||||||
using Mapster;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace AsbCloudInfrastructure.Services
|
|
||||||
{
|
|
||||||
public class UserRoleService : IUserRoleService
|
|
||||||
{
|
|
||||||
private readonly CacheTable<UserRole> cacheUserRoles;
|
|
||||||
private readonly CacheTable<RelationUserRolePermission> cacheRelationUserRolePermissions;
|
|
||||||
private readonly CacheTable<RelationUserRoleUserRole> cacheRelationUserRoleUserRole;
|
|
||||||
|
|
||||||
public ISet<string> Includes { get; } = new SortedSet<string>();
|
|
||||||
|
|
||||||
public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb)
|
|
||||||
{
|
|
||||||
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context, nameof(UserRole.RelationUserRolePermissions), nameof(UserRole.RelationUserRoleUserRoles));
|
|
||||||
cacheRelationUserRolePermissions = cacheDb.GetCachedTable<RelationUserRolePermission>((AsbCloudDbContext)context, nameof(RelationUserRolePermission.Permission));
|
|
||||||
cacheRelationUserRoleUserRole = cacheDb.GetCachedTable<RelationUserRoleUserRole>((AsbCloudDbContext)context, nameof(RelationUserRoleUserRole.IncludeRole), nameof(RelationUserRoleUserRole.Role));
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<int> InsertAsync(UserRoleDto dto, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var entity = dto.Adapt<UserRole>();
|
|
||||||
var updatedEntity = await cacheUserRoles.InsertAsync(entity, token)
|
|
||||||
.ConfigureAwait(false);
|
|
||||||
dto.Id = updatedEntity.Id;
|
|
||||||
await UpdatePermissionsAsync(dto, token);
|
|
||||||
await UpdateIncludedRolesAsync(dto, token);
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<IEnumerable<UserRoleDto>> GetAllAsync(CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var entities = await cacheUserRoles.WhereAsync(token)
|
|
||||||
.ConfigureAwait(false);
|
|
||||||
var dtos = entities?.Select(Convert);
|
|
||||||
return dtos;
|
|
||||||
}
|
|
||||||
public UserRoleDto GetOrDefault(int id)
|
|
||||||
{
|
|
||||||
var entity = cacheUserRoles.FirstOrDefault(r => r.Id == id);
|
|
||||||
if (entity is null)
|
|
||||||
return null;
|
|
||||||
var dto = Convert(entity);
|
|
||||||
return dto;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<UserRoleDto> GetOrDefaultAsync(int id, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var entity = await cacheUserRoles.FirstOrDefaultAsync(r => r.Id == id, token)
|
|
||||||
.ConfigureAwait(false);
|
|
||||||
if (entity is null)
|
|
||||||
return null;
|
|
||||||
var dto = Convert(entity);
|
|
||||||
return dto;
|
|
||||||
}
|
|
||||||
|
|
||||||
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())
|
|
||||||
throw new ArgumentInvalidException("Invalid role names", nameof(names));
|
|
||||||
var dtos = entities.Select(Convert);
|
|
||||||
return dtos;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<int> UpdateAsync(UserRoleDto dto, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var entity = Convert(dto);
|
|
||||||
await UpdatePermissionsAsync(dto, token);
|
|
||||||
await UpdateIncludedRolesAsync(dto, token);
|
|
||||||
|
|
||||||
var result = await cacheUserRoles.UpsertAsync(entity, token)
|
|
||||||
.ConfigureAwait(false);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<UserRoleDto> GetNestedById(int id, int recursionLevel = 7)
|
|
||||||
{
|
|
||||||
var role = cacheUserRoles.FirstOrDefault(r => r.Id == id);
|
|
||||||
if (role is null)
|
|
||||||
return null;
|
|
||||||
var dto = Convert(role);
|
|
||||||
var roles = new SortedSet<UserRoleDto>(ComparerIId.GetInstance()) { dto };
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task UpdatePermissionsAsync(UserRoleDto dto, CancellationToken token)
|
|
||||||
{
|
|
||||||
if (dto?.Permissions is null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
await cacheRelationUserRolePermissions.RemoveAsync(r => r.IdUserRole == dto.Id, token)
|
|
||||||
.ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (dto.Permissions.Any())
|
|
||||||
{
|
|
||||||
var newRelationRoleToPermission = dto.Permissions.Select(p => new RelationUserRolePermission
|
|
||||||
{
|
|
||||||
IdPermission = p.Id,
|
|
||||||
IdUserRole = dto.Id,
|
|
||||||
});
|
|
||||||
|
|
||||||
await cacheRelationUserRolePermissions.InsertAsync(newRelationRoleToPermission, token)
|
|
||||||
.ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task UpdateIncludedRolesAsync(UserRoleDto dto, CancellationToken token)
|
|
||||||
{
|
|
||||||
if (dto?.Roles is null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
await cacheRelationUserRoleUserRole.RemoveAsync(rel => rel.Id == dto.Id, token);
|
|
||||||
|
|
||||||
if (dto.Roles.Any())
|
|
||||||
{
|
|
||||||
var newRelations = dto.Roles.Select(r => new RelationUserRoleUserRole { Id = dto.Id, IdInclude = r.Id });
|
|
||||||
await cacheRelationUserRoleUserRole.UpsertAsync(newRelations, token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
public bool HasPermission(IEnumerable<int> rolesIds, string permissionName)
|
|
||||||
{
|
|
||||||
var permissionInfo = cacheRelationUserRolePermissions
|
|
||||||
.FirstOrDefault(p => p.Permission?.Name.ToLower() == permissionName.ToLower())
|
|
||||||
?.Permission;
|
|
||||||
|
|
||||||
if (permissionInfo is null)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (rolesIds.Contains(1))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
var idPermissionInfo = permissionInfo.Id;
|
|
||||||
var roles = cacheUserRoles.Where(r => rolesIds.Contains(r.Id));
|
|
||||||
foreach (var role in roles)
|
|
||||||
if (HasPermission(role, idPermissionInfo))
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool HasPermission(UserRole userRole, int idPermission, int recursionLevel = 7)
|
|
||||||
{
|
|
||||||
if (userRole.RelationUserRolePermissions.Any(p => p.IdPermission == idPermission))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (recursionLevel <= 0 || userRole.RelationUserRoleUserRoles?.Any() != true)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
foreach (var relation in userRole.RelationUserRoleUserRoles)
|
|
||||||
{
|
|
||||||
var includedRole = cacheUserRoles.First(p => p.Id == relation.IdInclude);
|
|
||||||
if (HasPermission(includedRole, idPermission, --recursionLevel))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static UserRole Convert(UserRoleDto dto)
|
|
||||||
{
|
|
||||||
var entity = dto.Adapt<UserRole>();
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
private UserRoleDto Convert(UserRole entity)
|
|
||||||
{
|
|
||||||
var dto = entity.Adapt<UserRoleDto>();
|
|
||||||
if (entity.RelationUserRolePermissions?.Any() == true)
|
|
||||||
{
|
|
||||||
dto.Permissions = cacheRelationUserRolePermissions
|
|
||||||
.Where(r => entity.Id == r.IdUserRole)
|
|
||||||
.Select(r => Convert(r.Permission));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entity.RelationUserRoleUserRoles?.Any() == true)
|
|
||||||
{
|
|
||||||
dto.Roles = entity.RelationUserRoleUserRoles.Select(rel =>
|
|
||||||
{
|
|
||||||
var includedRole = cacheUserRoles.First(r => r.Id == rel.IdInclude);
|
|
||||||
return Convert(includedRole);
|
|
||||||
}).ToList();
|
|
||||||
}
|
|
||||||
return dto;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static PermissionDto Convert(Permission entity)
|
|
||||||
{
|
|
||||||
var dto = entity.Adapt<PermissionDto>();
|
|
||||||
return dto;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,6 @@
|
|||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
using AsbCloudApp.Exceptions;
|
using AsbCloudApp.Exceptions;
|
||||||
|
using AsbCloudApp.Repositories;
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
using AsbCloudDb.Model;
|
using AsbCloudDb.Model;
|
||||||
using AsbCloudInfrastructure.Services.Cache;
|
using AsbCloudInfrastructure.Services.Cache;
|
||||||
@ -17,7 +18,7 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
private readonly CacheTable<User> cacheUsers;
|
private readonly CacheTable<User> cacheUsers;
|
||||||
private readonly CacheTable<RelationUserUserRole> cacheRelationUserToRoles;
|
private readonly CacheTable<RelationUserUserRole> cacheRelationUserToRoles;
|
||||||
public ISet<string> Includes { get; } = new SortedSet<string>();
|
public ISet<string> Includes { get; } = new SortedSet<string>();
|
||||||
public IUserRoleService RoleService { get; }
|
public IUserRoleRepository RoleService { get; }
|
||||||
|
|
||||||
private static readonly TypeAdapterConfig userTypeAdapterConfig = TypeAdapterConfig<UserExtendedDto, User>
|
private static readonly TypeAdapterConfig userTypeAdapterConfig = TypeAdapterConfig<UserExtendedDto, User>
|
||||||
.NewConfig()
|
.NewConfig()
|
||||||
@ -27,7 +28,7 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
dst => dst.RelationUsersUserRoles)
|
dst => dst.RelationUsersUserRoles)
|
||||||
.Config;
|
.Config;
|
||||||
|
|
||||||
public UserService(IAsbCloudDbContext context, CacheDb cacheDb, IUserRoleService roleService)
|
public UserService(IAsbCloudDbContext context, CacheDb cacheDb, IUserRoleRepository roleService)
|
||||||
{
|
{
|
||||||
var db = (AsbCloudDbContext)context;
|
var db = (AsbCloudDbContext)context;
|
||||||
cacheUsers = cacheDb.GetCachedTable<User>(
|
cacheUsers = cacheDb.GetCachedTable<User>(
|
||||||
|
@ -1,187 +0,0 @@
|
|||||||
using AsbCloudApp.Data;
|
|
||||||
using AsbCloudDb.Model;
|
|
||||||
using AsbCloudInfrastructure.Services;
|
|
||||||
using AsbCloudInfrastructure.Services.Cache;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace AsbCloudWebApi.Tests.ServicesTests
|
|
||||||
{
|
|
||||||
public class UserRoleServiceTest
|
|
||||||
{
|
|
||||||
private readonly AsbCloudDbContext context;
|
|
||||||
private readonly CacheDb cacheDb;
|
|
||||||
|
|
||||||
private readonly List<UserRole> roles = new() {
|
|
||||||
new UserRole { Id = 1_000_001, Caption = "role 1 level 0" },
|
|
||||||
new UserRole { Id = 1_000_002, Caption = "role 2 level 1" },
|
|
||||||
new UserRole { Id = 1_000_003, Caption = "role 3 level 1" },
|
|
||||||
new UserRole { Id = 1_000_004, Caption = "role 4 level 2" },
|
|
||||||
};
|
|
||||||
|
|
||||||
private readonly List<RelationUserRoleUserRole> relationRoleRole = new()
|
|
||||||
{
|
|
||||||
new RelationUserRoleUserRole { Id = 1_000_002, IdInclude = 1_000_001 },
|
|
||||||
new RelationUserRoleUserRole { Id = 1_000_003, IdInclude = 1_000_001 },
|
|
||||||
new RelationUserRoleUserRole { Id = 1_000_004, IdInclude = 1_000_002 },
|
|
||||||
new RelationUserRoleUserRole { Id = 1_000_004, IdInclude = 1_000_003 },
|
|
||||||
};
|
|
||||||
|
|
||||||
private readonly List<Permission> permissions = new()
|
|
||||||
{
|
|
||||||
new Permission { Id = 2_000_001, Name = "permission 1" },
|
|
||||||
new Permission { Id = 2_000_002, Name = "permission 2" },
|
|
||||||
new Permission { Id = 2_000_003, Name = "permission 3" },
|
|
||||||
new Permission { Id = 2_000_004, Name = "permission 4" },
|
|
||||||
};
|
|
||||||
|
|
||||||
private readonly List<RelationUserRolePermission> relationRolePermission = new()
|
|
||||||
{
|
|
||||||
new RelationUserRolePermission { IdUserRole = 1_000_001, IdPermission = 2_000_001 },
|
|
||||||
new RelationUserRolePermission { IdUserRole = 1_000_002, IdPermission = 2_000_002 },
|
|
||||||
new RelationUserRolePermission { IdUserRole = 1_000_003, IdPermission = 2_000_003 },
|
|
||||||
new RelationUserRolePermission { IdUserRole = 1_000_004, IdPermission = 2_000_004 },
|
|
||||||
};
|
|
||||||
|
|
||||||
public UserRoleServiceTest()
|
|
||||||
{
|
|
||||||
cacheDb = new CacheDb();
|
|
||||||
context = TestHelpter.MakeTestContext();
|
|
||||||
context.UserRoles.RemoveRange(roles);
|
|
||||||
context.Permissions.RemoveRange(permissions);
|
|
||||||
context.SaveChanges();
|
|
||||||
context.UserRoles.AddRange(roles);
|
|
||||||
context.Permissions.AddRange(permissions);
|
|
||||||
context.SaveChanges();
|
|
||||||
context.RelationUserRoleUserRoles.AddRange(relationRoleRole);
|
|
||||||
context.RelationUserRolePermissions.AddRange(relationRolePermission);
|
|
||||||
context.SaveChanges();
|
|
||||||
}
|
|
||||||
|
|
||||||
~UserRoleServiceTest()
|
|
||||||
{
|
|
||||||
context.UserRoles.RemoveRange(roles);
|
|
||||||
context.Permissions.RemoveRange(permissions);
|
|
||||||
context.RelationUserRoleUserRoles.RemoveRange(relationRoleRole);
|
|
||||||
context.RelationUserRolePermissions.RemoveRange(relationRolePermission);
|
|
||||||
context.SaveChanges();
|
|
||||||
context.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void GetNestedById_return_4_items()
|
|
||||||
{
|
|
||||||
var service = new UserRoleService(context, cacheDb);
|
|
||||||
var nestedRoles = service.GetNestedById(1_000_004);
|
|
||||||
Assert.Equal(roles.Count, nestedRoles.Count());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void HasPermission_return_true()
|
|
||||||
{
|
|
||||||
var service = new UserRoleService(context, cacheDb);
|
|
||||||
var result = service.HasPermission(new int[] { 1_000_004 }, "permission 1");
|
|
||||||
Assert.True(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void HasPermission_return_false()
|
|
||||||
{
|
|
||||||
var service = new UserRoleService(context, cacheDb);
|
|
||||||
var result = service.HasPermission(new int[] { 1_000_003 }, "permission 2");
|
|
||||||
Assert.False(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task InsertAsync_returns_id()
|
|
||||||
{
|
|
||||||
var service = new UserRoleService(context, cacheDb);
|
|
||||||
var newRole = new UserRoleDto
|
|
||||||
{
|
|
||||||
Caption = "new role",
|
|
||||||
IdType = 0,
|
|
||||||
};
|
|
||||||
var id = await service.InsertAsync(newRole, CancellationToken.None);
|
|
||||||
Assert.NotEqual(0, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task InsertAsync_updates_relation_to_permission()
|
|
||||||
{
|
|
||||||
var service = new UserRoleService(context, cacheDb);
|
|
||||||
var newRole = new UserRoleDto
|
|
||||||
{
|
|
||||||
Caption = "new role",
|
|
||||||
IdType = 0,
|
|
||||||
Permissions = new[] { new PermissionDto { Id = 2_000_001 } },
|
|
||||||
};
|
|
||||||
var id = await service.InsertAsync(newRole, CancellationToken.None);
|
|
||||||
var entity = await service.GetOrDefaultAsync(id);
|
|
||||||
Assert.Equal(newRole.Permissions.Count(), entity.Permissions.Count());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task InsertAsync_updates_relation_to_role()
|
|
||||||
{
|
|
||||||
var service = new UserRoleService(context, cacheDb);
|
|
||||||
var newRole = new UserRoleDto
|
|
||||||
{
|
|
||||||
Caption = "new role",
|
|
||||||
IdType = 0,
|
|
||||||
Roles = new[] { new UserRoleDto { Id = 1_000_001 } }
|
|
||||||
};
|
|
||||||
var id = await service.InsertAsync(newRole, CancellationToken.None);
|
|
||||||
var entity = await service.GetOrDefaultAsync(id);
|
|
||||||
Assert.Equal(newRole.Roles.Count, entity.Roles.Count);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task UpdateAsync_returns_id()
|
|
||||||
{
|
|
||||||
var service = new UserRoleService(context, cacheDb);
|
|
||||||
const int updateId = 1_000_002;
|
|
||||||
var modRole = new UserRoleDto
|
|
||||||
{
|
|
||||||
Id = updateId,
|
|
||||||
Caption = "role 2 level 1"
|
|
||||||
};
|
|
||||||
var id = await service.UpdateAsync(modRole, CancellationToken.None);
|
|
||||||
Assert.Equal(updateId, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task UpdateAsync_updates_relation_to_permission()
|
|
||||||
{
|
|
||||||
var service = new UserRoleService(context, cacheDb);
|
|
||||||
const int updateId = 1_000_002;
|
|
||||||
var modRole = new UserRoleDto
|
|
||||||
{
|
|
||||||
Id = updateId,
|
|
||||||
Caption = "role 2 level 1",
|
|
||||||
Permissions = new[] { new PermissionDto { Id = 2_000_001 } },
|
|
||||||
};
|
|
||||||
var id = await service.UpdateAsync(modRole, CancellationToken.None);
|
|
||||||
var entity = await service.GetOrDefaultAsync(id);
|
|
||||||
Assert.Equal(modRole.Permissions.Count(), entity.Permissions.Count());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task UpdateAsync_updates_relation_to_role()
|
|
||||||
{
|
|
||||||
var service = new UserRoleService(context, cacheDb);
|
|
||||||
const int updateId = 1_000_002;
|
|
||||||
var modRole = new UserRoleDto
|
|
||||||
{
|
|
||||||
Id = updateId,
|
|
||||||
Caption = "role 2 level 1",
|
|
||||||
Roles = new[] { new UserRoleDto { Id = 1_000_001 } }
|
|
||||||
};
|
|
||||||
var id = await service.UpdateAsync(modRole, CancellationToken.None);
|
|
||||||
var entity = await service.GetOrDefaultAsync(id);
|
|
||||||
Assert.Equal(modRole.Roles.Count(), entity.Roles.Count());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +1,5 @@
|
|||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Repositories;
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
using AsbCloudDb.Model;
|
using AsbCloudDb.Model;
|
||||||
using AsbCloudInfrastructure.Services;
|
using AsbCloudInfrastructure.Services;
|
||||||
@ -16,13 +17,13 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
|||||||
{
|
{
|
||||||
private readonly AsbCloudDbContext context;
|
private readonly AsbCloudDbContext context;
|
||||||
private readonly CacheDb cacheDb;
|
private readonly CacheDb cacheDb;
|
||||||
private readonly Mock<IUserRoleService> roleService;
|
private readonly Mock<IUserRoleRepository> roleRepository;
|
||||||
|
|
||||||
public UserServiceTest()
|
public UserServiceTest()
|
||||||
{
|
{
|
||||||
context = TestHelpter.MakeTestContext();
|
context = TestHelpter.MakeTestContext();
|
||||||
cacheDb = new CacheDb();
|
cacheDb = new CacheDb();
|
||||||
roleService = new Mock<IUserRoleService>();
|
roleRepository = new Mock<IUserRoleRepository>();
|
||||||
|
|
||||||
context.Users.RemoveRange(context.Users.Where(u => u.Id > 1));
|
context.Users.RemoveRange(context.Users.Where(u => u.Id > 1));
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
@ -37,7 +38,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task InsertAsync_returns_id()
|
public async Task InsertAsync_returns_id()
|
||||||
{
|
{
|
||||||
var service = new UserService(context, cacheDb, roleService.Object);
|
var service = new UserService(context, cacheDb, roleRepository.Object);
|
||||||
var dto = new UserExtendedDto
|
var dto = new UserExtendedDto
|
||||||
{
|
{
|
||||||
Id = 0,
|
Id = 0,
|
||||||
@ -60,7 +61,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task InsertAsync_busy_login_throws_exceprion()
|
public async Task InsertAsync_busy_login_throws_exceprion()
|
||||||
{
|
{
|
||||||
var service = new UserService(context, cacheDb, roleService.Object);
|
var service = new UserService(context, cacheDb, roleRepository.Object);
|
||||||
var dto = new UserExtendedDto
|
var dto = new UserExtendedDto
|
||||||
{
|
{
|
||||||
Id = 5,
|
Id = 5,
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Repositories;
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@ -12,9 +13,9 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
[Route("api/admin/role")]
|
[Route("api/admin/role")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class AdminUserRoleController : CrudController<UserRoleDto, IUserRoleService>
|
public class AdminUserRoleController : CrudController<UserRoleDto, IUserRoleRepository>
|
||||||
{
|
{
|
||||||
public AdminUserRoleController(IUserRoleService service)
|
public AdminUserRoleController(IUserRoleRepository service)
|
||||||
: base(service)
|
: base(service)
|
||||||
{
|
{
|
||||||
UpdateForbidAsync = async (dto, token) =>
|
UpdateForbidAsync = async (dto, token) =>
|
||||||
|
Loading…
Reference in New Issue
Block a user