forked from ddrilling/AsbCloudServer
CS2-123: Fixed role permissions create/update operations
This commit is contained in:
parent
1d078379b4
commit
cf517eabd5
@ -1,10 +1,10 @@
|
||||
namespace AsbCloudApp.Data
|
||||
{
|
||||
public class PermissionDto
|
||||
public class PermissionDto : IId
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string BitDescription { get; set; }
|
||||
public int IdUserRole { get; set; }
|
||||
public int IdPermission { get; set; }
|
||||
public int PermissionValue { get; set; }
|
||||
}
|
||||
}
|
@ -9,7 +9,6 @@ namespace AsbCloudApp.Data
|
||||
public string Caption { get; set; }
|
||||
public int? IdParent { get; set; }
|
||||
public int IdType { get; set; }
|
||||
public IEnumerable<int> PermissionIds { get; set; }
|
||||
public IEnumerable<PermissionDto> Permissions { get; set; }
|
||||
[JsonIgnore]
|
||||
public virtual ICollection<UserDto> Users { get; set; }
|
||||
|
2893
AsbCloudDb/Migrations/20211201105010_Renamed_Permission_Role_Files.Designer.cs
generated
Normal file
2893
AsbCloudDb/Migrations/20211201105010_Renamed_Permission_Role_Files.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,17 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace AsbCloudDb.Migrations
|
||||
{
|
||||
public partial class Renamed_Permission_Role_Files : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -683,7 +683,7 @@ namespace AsbCloudDb.Migrations
|
||||
|
||||
modelBuilder.Entity("AsbCloudDb.Model.Permission", b =>
|
||||
{
|
||||
b.Property<int>("IdRole")
|
||||
b.Property<int>("IdUserRole")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id_user_role");
|
||||
|
||||
@ -695,7 +695,7 @@ namespace AsbCloudDb.Migrations
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("permission_value");
|
||||
|
||||
b.HasKey("IdRole", "IdPermission");
|
||||
b.HasKey("IdUserRole", "IdPermission");
|
||||
|
||||
b.HasIndex("IdPermission");
|
||||
|
||||
@ -2545,7 +2545,7 @@ namespace AsbCloudDb.Migrations
|
||||
|
||||
b.HasOne("AsbCloudDb.Model.UserRole", "UserRole")
|
||||
.WithMany("Permissions")
|
||||
.HasForeignKey("IdRole")
|
||||
.HasForeignKey("IdUserRole")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
|
@ -255,7 +255,7 @@ namespace AsbCloudDb.Model
|
||||
|
||||
modelBuilder.Entity<Permission>(entity =>
|
||||
{
|
||||
entity.HasKey(e => new { e.IdRole, e.IdPermission });
|
||||
entity.HasKey(e => new { e.IdUserRole, e.IdPermission });
|
||||
});
|
||||
|
||||
FillData(modelBuilder);
|
||||
|
@ -1,6 +1,5 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace AsbCloudDb.Model
|
||||
{
|
||||
@ -8,7 +7,7 @@ namespace AsbCloudDb.Model
|
||||
public class Permission
|
||||
{
|
||||
[Column("id_user_role")]
|
||||
public int IdRole { get; set; }
|
||||
public int IdUserRole { get; set; }
|
||||
|
||||
[Column("id_permission")]
|
||||
public int IdPermission { get; set; }
|
||||
@ -16,7 +15,7 @@ namespace AsbCloudDb.Model
|
||||
[Column("permission_value")]
|
||||
public int PermissionValue { get; set; }
|
||||
|
||||
[ForeignKey(nameof(IdRole))]
|
||||
[ForeignKey(nameof(IdUserRole))]
|
||||
[InverseProperty(nameof(Model.UserRole.Permissions))]
|
||||
public virtual UserRole UserRole { get; set; }
|
||||
|
||||
|
@ -189,13 +189,13 @@ namespace AsbCloudDevOperations
|
||||
demoContext.RelationUserRolesPermissions.AddRange(
|
||||
new Permission()
|
||||
{
|
||||
IdRole = 2,
|
||||
IdUserRole = 2,
|
||||
IdPermission = 1,
|
||||
PermissionValue = 143
|
||||
},
|
||||
new Permission()
|
||||
{
|
||||
IdRole = 2,
|
||||
IdUserRole = 2,
|
||||
IdPermission = 2,
|
||||
PermissionValue = 12
|
||||
}
|
||||
|
@ -189,7 +189,7 @@ namespace AsbCloudInfrastructure.Services
|
||||
{
|
||||
var rolesIds = userRoles.Select(r => r.Id);
|
||||
var userPermissionsInfo = cacheUserRolesPermissions.Where(p =>
|
||||
rolesIds.Contains(p.IdRole))
|
||||
rolesIds.Contains(p.IdUserRole))
|
||||
.Select(perm => new { perm.IdPermission, perm.PermissionValue });
|
||||
|
||||
return userPermissionsInfo.Select(p => new
|
||||
|
@ -1,5 +1,3 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@ -13,15 +11,15 @@ namespace AsbCloudInfrastructure.Services
|
||||
public class UserRoleService : CrudServiceBase<UserRoleDto, UserRole>
|
||||
{
|
||||
private readonly CacheTable<UserRole> cacheUserRoles;
|
||||
private readonly CacheTable<PermissionInfo> cachePermissions;
|
||||
private readonly CacheTable<Permission> cacheUserRolesPermissions;
|
||||
private readonly CacheTable<PermissionInfo> cachePermissionsInfo;
|
||||
private readonly CacheTable<Permission> cachePermissions;
|
||||
private int counter = 0;
|
||||
|
||||
public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb) : base(context)
|
||||
{
|
||||
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context);
|
||||
cachePermissions = cacheDb.GetCachedTable<PermissionInfo>((AsbCloudDbContext)context);
|
||||
cacheUserRolesPermissions =
|
||||
cachePermissionsInfo = cacheDb.GetCachedTable<PermissionInfo>((AsbCloudDbContext)context);
|
||||
cachePermissions =
|
||||
cacheDb.GetCachedTable<Permission>((AsbCloudDbContext)context);
|
||||
}
|
||||
|
||||
@ -44,84 +42,53 @@ namespace AsbCloudInfrastructure.Services
|
||||
: FillUserRoleWithPermissions(roleDto);
|
||||
}
|
||||
|
||||
public override async Task<int> InsertAsync(UserRoleDto dto, CancellationToken token = default)
|
||||
{
|
||||
|
||||
dto.PermissionIds = GetAncestorsPermissionIds(dto.Id, dto.PermissionIds, dto.IdParent, ref counter);
|
||||
|
||||
var newRoleId = await base.InsertAsync(dto, token);
|
||||
|
||||
if (dto.PermissionIds == default)
|
||||
return newRoleId;
|
||||
|
||||
foreach (var pId in dto.PermissionIds)
|
||||
{
|
||||
var relation = new Permission()
|
||||
{
|
||||
IdRole = newRoleId,
|
||||
IdPermission = pId
|
||||
};
|
||||
|
||||
context.RelationUserRolesPermissions.Add(relation);
|
||||
}
|
||||
|
||||
return await context.SaveChangesAsync(token);
|
||||
}
|
||||
|
||||
public override async Task<int> UpdateAsync(int id, UserRoleDto item, CancellationToken token = default)
|
||||
{
|
||||
foreach (var p in item.Permissions)
|
||||
p.IdUserRole = item.Id;
|
||||
|
||||
var result = await base.UpdateAsync(id, item, token);
|
||||
|
||||
if (item.PermissionIds == default)
|
||||
return result;
|
||||
|
||||
await cacheUserRolesPermissions.RemoveAsync(r => r.IdRole == item.Id, token)
|
||||
await cachePermissions.RemoveAsync(r => r.IdUserRole == item.Id, token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var newRelations = item.PermissionIds.Select(p => new Permission()
|
||||
{
|
||||
IdRole = item.Id,
|
||||
IdPermission = p
|
||||
});
|
||||
await cacheUserRolesPermissions.InsertAsync(newRelations, token);
|
||||
|
||||
var newPermissions = item.Permissions.Adapt<Permission>();
|
||||
|
||||
await cachePermissions.InsertAsync(newPermissions, token);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private UserRoleDto FillUserRoleWithPermissions(UserRoleDto roleDto)
|
||||
{
|
||||
var rolePermissionIds = cacheUserRolesPermissions.Where(c =>
|
||||
c.IdRole == roleDto.Id).Select(p => p.IdPermission);
|
||||
roleDto.Permissions = cachePermissions.Where(permission => rolePermissionIds.Contains(permission.Id))
|
||||
.Adapt<PermissionDto>();
|
||||
roleDto.Permissions = cachePermissions.Where(c =>
|
||||
c.IdUserRole == roleDto.Id).Adapt<PermissionDto>();
|
||||
|
||||
return roleDto;
|
||||
}
|
||||
|
||||
private IEnumerable<int> GetAncestorsPermissionIds(int idRole, IEnumerable<int> currentPermissionsIds,
|
||||
int? idParent, ref int counter)
|
||||
{
|
||||
//var currentPermissionsIds = userRoleDto.PermissionIds ?? new List<int>();
|
||||
|
||||
if (idParent == default)
|
||||
return currentPermissionsIds;
|
||||
|
||||
if (counter > 10)
|
||||
{
|
||||
Trace.WriteLine($"User role with id: {idRole} has more than 10 nested parents");
|
||||
return currentPermissionsIds;
|
||||
}
|
||||
|
||||
var parentRole = cacheUserRoles.FirstOrDefault(r => r.Id == idParent)
|
||||
.Adapt<UserRoleDto>();
|
||||
var parentRolePermissionsIds = cacheUserRolesPermissions.Where(p =>
|
||||
p.IdRole == parentRole.Id).Select(perm => perm.IdPermission);
|
||||
var resultPermissions = currentPermissionsIds.Union(parentRolePermissionsIds);
|
||||
|
||||
counter++;
|
||||
|
||||
return GetAncestorsPermissionIds(parentRole.Id, resultPermissions,
|
||||
parentRole.IdParent, ref counter);
|
||||
}
|
||||
// private IEnumerable<int> GetAncestorsPermissionIds(int idRole, IEnumerable<int> currentPermissionsIds,
|
||||
// int? idParent, ref int counter)
|
||||
// {
|
||||
// if (idParent == default)
|
||||
// return currentPermissionsIds;
|
||||
//
|
||||
// if (counter > 10)
|
||||
// {
|
||||
// Trace.WriteLine($"User role with id: {idRole} has more than 10 nested parents");
|
||||
// return currentPermissionsIds;
|
||||
// }
|
||||
//
|
||||
// var parentRole = cacheUserRoles.FirstOrDefault(r => r.Id == idParent)
|
||||
// .Adapt<UserRoleDto>();
|
||||
// var parentRolePermissionsIds = cachePermissions.Where(p =>
|
||||
// p.IdUserRole == parentRole.Id).Select(perm => perm.IdPermission);
|
||||
// var resultPermissions = currentPermissionsIds.Union(parentRolePermissionsIds);
|
||||
//
|
||||
// counter++;
|
||||
//
|
||||
// return GetAncestorsPermissionIds(parentRole.Id, resultPermissions,
|
||||
// parentRole.IdParent, ref counter);
|
||||
// }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user