CS2-123: Fixed role permissions create/update operations

This commit is contained in:
KharchenkoVladimir 2021-12-01 16:09:06 +05:00
parent 1d078379b4
commit cf517eabd5
10 changed files with 2960 additions and 85 deletions

View File

@ -1,10 +1,10 @@
namespace AsbCloudApp.Data namespace AsbCloudApp.Data
{ {
public class PermissionDto public class PermissionDto : IId
{ {
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public int IdUserRole { get; set; }
public string Description { get; set; } public int IdPermission { get; set; }
public string BitDescription { get; set; } public int PermissionValue { get; set; }
} }
} }

View File

@ -9,7 +9,6 @@ namespace AsbCloudApp.Data
public string Caption { get; set; } public string Caption { get; set; }
public int? IdParent { get; set; } public int? IdParent { get; set; }
public int IdType { get; set; } public int IdType { get; set; }
public IEnumerable<int> PermissionIds { get; set; }
public IEnumerable<PermissionDto> Permissions { get; set; } public IEnumerable<PermissionDto> Permissions { get; set; }
[JsonIgnore] [JsonIgnore]
public virtual ICollection<UserDto> Users { get; set; } public virtual ICollection<UserDto> Users { get; set; }

File diff suppressed because it is too large Load Diff

View File

@ -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)
{
}
}
}

View File

@ -683,7 +683,7 @@ namespace AsbCloudDb.Migrations
modelBuilder.Entity("AsbCloudDb.Model.Permission", b => modelBuilder.Entity("AsbCloudDb.Model.Permission", b =>
{ {
b.Property<int>("IdRole") b.Property<int>("IdUserRole")
.HasColumnType("integer") .HasColumnType("integer")
.HasColumnName("id_user_role"); .HasColumnName("id_user_role");
@ -695,7 +695,7 @@ namespace AsbCloudDb.Migrations
.HasColumnType("integer") .HasColumnType("integer")
.HasColumnName("permission_value"); .HasColumnName("permission_value");
b.HasKey("IdRole", "IdPermission"); b.HasKey("IdUserRole", "IdPermission");
b.HasIndex("IdPermission"); b.HasIndex("IdPermission");
@ -2545,7 +2545,7 @@ namespace AsbCloudDb.Migrations
b.HasOne("AsbCloudDb.Model.UserRole", "UserRole") b.HasOne("AsbCloudDb.Model.UserRole", "UserRole")
.WithMany("Permissions") .WithMany("Permissions")
.HasForeignKey("IdRole") .HasForeignKey("IdUserRole")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();

View File

@ -255,7 +255,7 @@ namespace AsbCloudDb.Model
modelBuilder.Entity<Permission>(entity => modelBuilder.Entity<Permission>(entity =>
{ {
entity.HasKey(e => new { e.IdRole, e.IdPermission }); entity.HasKey(e => new { e.IdUserRole, e.IdPermission });
}); });
FillData(modelBuilder); FillData(modelBuilder);

View File

@ -1,6 +1,5 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace AsbCloudDb.Model namespace AsbCloudDb.Model
{ {
@ -8,7 +7,7 @@ namespace AsbCloudDb.Model
public class Permission public class Permission
{ {
[Column("id_user_role")] [Column("id_user_role")]
public int IdRole { get; set; } public int IdUserRole { get; set; }
[Column("id_permission")] [Column("id_permission")]
public int IdPermission { get; set; } public int IdPermission { get; set; }
@ -16,7 +15,7 @@ namespace AsbCloudDb.Model
[Column("permission_value")] [Column("permission_value")]
public int PermissionValue { get; set; } public int PermissionValue { get; set; }
[ForeignKey(nameof(IdRole))] [ForeignKey(nameof(IdUserRole))]
[InverseProperty(nameof(Model.UserRole.Permissions))] [InverseProperty(nameof(Model.UserRole.Permissions))]
public virtual UserRole UserRole { get; set; } public virtual UserRole UserRole { get; set; }

View File

@ -189,13 +189,13 @@ namespace AsbCloudDevOperations
demoContext.RelationUserRolesPermissions.AddRange( demoContext.RelationUserRolesPermissions.AddRange(
new Permission() new Permission()
{ {
IdRole = 2, IdUserRole = 2,
IdPermission = 1, IdPermission = 1,
PermissionValue = 143 PermissionValue = 143
}, },
new Permission() new Permission()
{ {
IdRole = 2, IdUserRole = 2,
IdPermission = 2, IdPermission = 2,
PermissionValue = 12 PermissionValue = 12
} }

View File

@ -189,7 +189,7 @@ namespace AsbCloudInfrastructure.Services
{ {
var rolesIds = userRoles.Select(r => r.Id); var rolesIds = userRoles.Select(r => r.Id);
var userPermissionsInfo = cacheUserRolesPermissions.Where(p => var userPermissionsInfo = cacheUserRolesPermissions.Where(p =>
rolesIds.Contains(p.IdRole)) rolesIds.Contains(p.IdUserRole))
.Select(perm => new { perm.IdPermission, perm.PermissionValue }); .Select(perm => new { perm.IdPermission, perm.PermissionValue });
return userPermissionsInfo.Select(p => new return userPermissionsInfo.Select(p => new

View File

@ -1,5 +1,3 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -13,15 +11,15 @@ namespace AsbCloudInfrastructure.Services
public class UserRoleService : CrudServiceBase<UserRoleDto, UserRole> public class UserRoleService : CrudServiceBase<UserRoleDto, UserRole>
{ {
private readonly CacheTable<UserRole> cacheUserRoles; private readonly CacheTable<UserRole> cacheUserRoles;
private readonly CacheTable<PermissionInfo> cachePermissions; private readonly CacheTable<PermissionInfo> cachePermissionsInfo;
private readonly CacheTable<Permission> cacheUserRolesPermissions; private readonly CacheTable<Permission> cachePermissions;
private int counter = 0; private int counter = 0;
public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb) : base(context) public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb) : base(context)
{ {
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context); cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context);
cachePermissions = cacheDb.GetCachedTable<PermissionInfo>((AsbCloudDbContext)context); cachePermissionsInfo = cacheDb.GetCachedTable<PermissionInfo>((AsbCloudDbContext)context);
cacheUserRolesPermissions = cachePermissions =
cacheDb.GetCachedTable<Permission>((AsbCloudDbContext)context); cacheDb.GetCachedTable<Permission>((AsbCloudDbContext)context);
} }
@ -44,84 +42,53 @@ namespace AsbCloudInfrastructure.Services
: FillUserRoleWithPermissions(roleDto); : 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) 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); var result = await base.UpdateAsync(id, item, token);
if (item.PermissionIds == default) await cachePermissions.RemoveAsync(r => r.IdUserRole == item.Id, token)
return result;
await cacheUserRolesPermissions.RemoveAsync(r => r.IdRole == item.Id, token)
.ConfigureAwait(false); .ConfigureAwait(false);
var newRelations = item.PermissionIds.Select(p => new Permission() var newPermissions = item.Permissions.Adapt<Permission>();
{
IdRole = item.Id, await cachePermissions.InsertAsync(newPermissions, token);
IdPermission = p
});
await cacheUserRolesPermissions.InsertAsync(newRelations, token);
return result; return result;
} }
private UserRoleDto FillUserRoleWithPermissions(UserRoleDto roleDto) private UserRoleDto FillUserRoleWithPermissions(UserRoleDto roleDto)
{ {
var rolePermissionIds = cacheUserRolesPermissions.Where(c => roleDto.Permissions = cachePermissions.Where(c =>
c.IdRole == roleDto.Id).Select(p => p.IdPermission); c.IdUserRole == roleDto.Id).Adapt<PermissionDto>();
roleDto.Permissions = cachePermissions.Where(permission => rolePermissionIds.Contains(permission.Id))
.Adapt<PermissionDto>();
return roleDto; return roleDto;
} }
private IEnumerable<int> GetAncestorsPermissionIds(int idRole, IEnumerable<int> currentPermissionsIds, // private IEnumerable<int> GetAncestorsPermissionIds(int idRole, IEnumerable<int> currentPermissionsIds,
int? idParent, ref int counter) // int? idParent, ref int counter)
{ // {
//var currentPermissionsIds = userRoleDto.PermissionIds ?? new List<int>(); // if (idParent == default)
// return currentPermissionsIds;
if (idParent == default) //
return currentPermissionsIds; // if (counter > 10)
// {
if (counter > 10) // Trace.WriteLine($"User role with id: {idRole} has more than 10 nested parents");
{ // return currentPermissionsIds;
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 parentRole = cacheUserRoles.FirstOrDefault(r => r.Id == idParent) // var parentRolePermissionsIds = cachePermissions.Where(p =>
.Adapt<UserRoleDto>(); // p.IdUserRole == parentRole.Id).Select(perm => perm.IdPermission);
var parentRolePermissionsIds = cacheUserRolesPermissions.Where(p => // var resultPermissions = currentPermissionsIds.Union(parentRolePermissionsIds);
p.IdRole == parentRole.Id).Select(perm => perm.IdPermission); //
var resultPermissions = currentPermissionsIds.Union(parentRolePermissionsIds); // counter++;
//
counter++; // return GetAncestorsPermissionIds(parentRole.Id, resultPermissions,
// parentRole.IdParent, ref counter);
return GetAncestorsPermissionIds(parentRole.Id, resultPermissions, // }
parentRole.IdParent, ref counter);
}
} }
} }