2021-12-02 13:35:15 +05:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Collections.Generic;
|
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;
|
2021-12-03 09:44:10 +05:00
|
|
|
using AsbCloudApp.Services;
|
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;
|
2021-12-03 09:44:10 +05:00
|
|
|
private readonly IPermissionService permissionService;
|
2021-11-24 17:38:40 +05:00
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
public List<string> Includes { get; } = new();
|
2021-12-03 15:03:33 +05:00
|
|
|
|
|
|
|
public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb, IPermissionService permissionService)
|
2021-11-24 17:38:40 +05:00
|
|
|
{
|
2021-12-07 18:27:52 +05:00
|
|
|
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context, new [] { nameof(UserRole.Permissions) });
|
2021-12-03 09:44:10 +05:00
|
|
|
this.permissionService = permissionService;
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
var dtos = entities.Adapt<UserRoleDto>();
|
|
|
|
return dtos;
|
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
|
|
|
{
|
2021-12-03 15:03:33 +05:00
|
|
|
var entity = await cacheUserRoles.FirstOrDefaultAsync(r=>r.Id == id, token)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
var dto = entity?.Adapt<UserRoleDto>();
|
|
|
|
return dto;
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
|
|
|
|
2021-12-03 15:03:33 +05:00
|
|
|
public async Task<int> InsertAsync(UserRoleDto dto, CancellationToken token = default)
|
2021-12-02 13:35:15 +05:00
|
|
|
{
|
2021-12-03 15:03:33 +05:00
|
|
|
var entity = dto.Adapt<UserRole>();
|
|
|
|
var updatedEntity = await cacheUserRoles.InsertAsync(entity, token).ConfigureAwait(false);
|
2021-12-07 18:27:52 +05:00
|
|
|
if (dto.Permissions?.Any() != true)
|
|
|
|
return updatedEntity?.Id ?? 0;
|
|
|
|
foreach (var permission in dto.Permissions)
|
|
|
|
permission.IdUserRole = updatedEntity.Id;
|
|
|
|
await permissionService.InsertRangeAsync(dto.Permissions, token).ConfigureAwait(false);
|
|
|
|
await cacheUserRoles.RefreshAsync(true, token)
|
|
|
|
.ConfigureAwait(false);
|
2021-12-03 15:03:33 +05:00
|
|
|
return updatedEntity?.Id ?? 0;
|
2021-12-02 13:35:15 +05:00
|
|
|
}
|
|
|
|
|
2021-12-03 15:03:33 +05:00
|
|
|
public async Task<int> InsertRangeAsync(IEnumerable<UserRoleDto> dtos, CancellationToken token = default)
|
2021-11-29 12:39:28 +05:00
|
|
|
{
|
2021-12-03 15:03:33 +05:00
|
|
|
var entities = dtos.Adapt<UserRole>();
|
|
|
|
return await cacheUserRoles.InsertAsync(entities, token).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<int> UpdateAsync(int id, UserRoleDto dto, CancellationToken token = default)
|
|
|
|
{
|
|
|
|
var entity = dto.Adapt<UserRole>();
|
|
|
|
entity.Id = id;
|
|
|
|
await cacheUserRoles.UpsertAsync(entity, token)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
if(dto.Permissions is not null)
|
|
|
|
{
|
|
|
|
await permissionService.DeleteAllByRoleAsync(id, token)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (dto.Permissions.Any())
|
|
|
|
{
|
|
|
|
foreach (var permission in dto.Permissions)
|
|
|
|
permission.IdUserRole = id;
|
|
|
|
|
|
|
|
await permissionService.InsertRangeAsync(dto.Permissions, token)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
await cacheUserRoles.RefreshAsync(true, token)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
}
|
2021-11-24 17:38:40 +05:00
|
|
|
|
2021-12-03 15:03:33 +05:00
|
|
|
return id;
|
2021-11-29 12:39:28 +05:00
|
|
|
}
|
2021-11-26 17:05:41 +05:00
|
|
|
|
2021-12-03 09:44:10 +05:00
|
|
|
private IEnumerable<Permission> GetNestedPermissions(UserRole role, int counter = 10)
|
2021-11-29 12:39:28 +05:00
|
|
|
{
|
2021-12-07 18:27:52 +05:00
|
|
|
var permissions = role.Permissions.ToList();
|
|
|
|
if (role.IdParent is null)
|
|
|
|
return permissions;
|
|
|
|
if (counter == 0)
|
2021-12-03 09:44:10 +05:00
|
|
|
{
|
2021-12-07 18:27:52 +05:00
|
|
|
Trace.WriteLine($"User role with id: {role.Id} has more than 10 nested childs");
|
|
|
|
return permissions;
|
|
|
|
}
|
|
|
|
var parentRole = cacheUserRoles.FirstOrDefault(r => r.Id == role.IdParent);
|
2021-12-03 15:03:33 +05:00
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
if (parentRole is null)
|
|
|
|
return permissions;
|
2021-12-03 09:44:10 +05:00
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
var parentPermissions = GetNestedPermissions(parentRole, --counter);
|
|
|
|
Merge(ref permissions, parentPermissions);
|
2021-12-03 09:44:10 +05:00
|
|
|
return permissions;
|
2021-11-24 17:38:40 +05:00
|
|
|
}
|
2021-11-25 17:44:07 +05:00
|
|
|
|
2021-12-03 09:44:10 +05:00
|
|
|
private static void Merge(ref List<Permission> permissions, IEnumerable<Permission> newPermissions)
|
2021-12-02 13:35:15 +05:00
|
|
|
{
|
2021-12-03 09:44:10 +05:00
|
|
|
foreach (var newPermission in newPermissions)
|
2021-12-02 13:35:15 +05:00
|
|
|
{
|
2021-12-03 09:44:10 +05:00
|
|
|
var permissionIndex = permissions.FindIndex(p => p.IdPermission == newPermission.IdPermission);
|
|
|
|
if (permissionIndex == -1)
|
|
|
|
permissions.Add(newPermission);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var permission = permissions[permissionIndex];
|
|
|
|
permissions[permissionIndex] = new Permission
|
|
|
|
{
|
|
|
|
IdPermission = permission.IdPermission,
|
|
|
|
IdUserRole = permission.IdUserRole,
|
|
|
|
PermissionValue = permission.PermissionValue | newPermission.PermissionValue,
|
|
|
|
};
|
|
|
|
}
|
2021-12-02 13:35:15 +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-11-24 17:38:40 +05:00
|
|
|
}
|
|
|
|
}
|