CS2-133: Removed Value property from roles to permissions models and services

This commit is contained in:
Харченко Владимир 2021-12-15 16:21:52 +05:00
parent dc636a1d8c
commit 888c28cdf8
15 changed files with 3337 additions and 150 deletions

View File

@ -7,6 +7,5 @@ namespace AsbCloudApp.Data
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public IDictionary<int, string> BitDescription { get; set; }
}
}

View File

@ -2,8 +2,7 @@
{
public class PermissionBaseDto
{
public int IdPermissionInfo { get; set; }
public int Id { get; set; }
public string PermissionName { get; set; }
public int Value { get; set; }
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,214 @@
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace AsbCloudDb.Migrations
{
public partial class RemovedPermissionValues : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_t_permission_t_permission_info_id_permission_info",
table: "t_permission");
migrationBuilder.DropForeignKey(
name: "FK_t_permission_t_user_role_id_user_role",
table: "t_permission");
migrationBuilder.DropTable(
name: "t_permission_info");
migrationBuilder.DropPrimaryKey(
name: "PK_t_permission",
table: "t_permission");
migrationBuilder.DropIndex(
name: "IX_t_permission_id_permission_info",
table: "t_permission");
migrationBuilder.DropColumn(
name: "id_user_role",
table: "t_permission");
migrationBuilder.DropColumn(
name: "id_permission_info",
table: "t_permission");
migrationBuilder.RenameColumn(
name: "value",
table: "t_permission",
newName: "id");
migrationBuilder.AlterTable(
name: "t_permission",
comment: "Разрешения на доступ к данным",
oldComment: "Отношение ролей пользователей и разрешений доступа");
migrationBuilder.AddColumn<int>(
name: "id",
table: "t_relation_user_user_role",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.AlterColumn<int>(
name: "id",
table: "t_permission",
type: "integer",
nullable: false,
oldClrType: typeof(int),
oldType: "integer")
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
migrationBuilder.AddColumn<string>(
name: "description",
table: "t_permission",
type: "character varying(255)",
maxLength: 255,
nullable: true,
comment: "Краткое описание");
migrationBuilder.AddColumn<string>(
name: "name",
table: "t_permission",
type: "character varying(255)",
maxLength: 255,
nullable: true,
comment: "Название");
migrationBuilder.AddPrimaryKey(
name: "PK_t_permission",
table: "t_permission",
column: "id");
migrationBuilder.CreateTable(
name: "t_relation_user_role_permission",
columns: table => new
{
id_user_role = table.Column<int>(type: "integer", nullable: false),
id_permission = table.Column<int>(type: "integer", nullable: false),
id = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_t_relation_user_role_permission", x => new { x.id_user_role, x.id_permission });
table.ForeignKey(
name: "FK_t_relation_user_role_permission_t_permission_id_permission",
column: x => x.id_permission,
principalTable: "t_permission",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_t_relation_user_role_permission_t_user_role_id_user_role",
column: x => x.id_user_role,
principalTable: "t_user_role",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
},
comment: "Отношение ролей пользователей и разрешений доступа");
migrationBuilder.CreateIndex(
name: "IX_t_relation_user_role_permission_id_permission",
table: "t_relation_user_role_permission",
column: "id_permission");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "t_relation_user_role_permission");
migrationBuilder.DropPrimaryKey(
name: "PK_t_permission",
table: "t_permission");
migrationBuilder.DropColumn(
name: "id",
table: "t_relation_user_user_role");
migrationBuilder.DropColumn(
name: "description",
table: "t_permission");
migrationBuilder.DropColumn(
name: "name",
table: "t_permission");
migrationBuilder.RenameColumn(
name: "id",
table: "t_permission",
newName: "value");
migrationBuilder.AlterTable(
name: "t_permission",
comment: "Отношение ролей пользователей и разрешений доступа",
oldComment: "Разрешения на доступ к данным");
migrationBuilder.AlterColumn<int>(
name: "value",
table: "t_permission",
type: "integer",
nullable: false,
oldClrType: typeof(int),
oldType: "integer")
.OldAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
migrationBuilder.AddColumn<int>(
name: "id_user_role",
table: "t_permission",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "id_permission_info",
table: "t_permission",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.AddPrimaryKey(
name: "PK_t_permission",
table: "t_permission",
columns: new[] { "id_user_role", "id_permission_info" });
migrationBuilder.CreateTable(
name: "t_permission_info",
columns: table => new
{
id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
bit_description = table.Column<IDictionary<int, string>>(type: "jsonb", nullable: true, comment: "Описание каждого бита разрешений"),
description = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: true, comment: "Краткое описание"),
name = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: true, comment: "Название")
},
constraints: table =>
{
table.PrimaryKey("PK_t_permission_info", x => x.id);
},
comment: "Разрешения на доступ к данным");
migrationBuilder.CreateIndex(
name: "IX_t_permission_id_permission_info",
table: "t_permission",
column: "id_permission_info");
migrationBuilder.AddForeignKey(
name: "FK_t_permission_t_permission_info_id_permission_info",
table: "t_permission",
column: "id_permission_info",
principalTable: "t_permission_info",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_t_permission_t_user_role_id_user_role",
table: "t_permission",
column: "id_user_role",
principalTable: "t_user_role",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
}
}
}

View File

@ -682,30 +682,6 @@ namespace AsbCloudDb.Migrations
});
modelBuilder.Entity("AsbCloudDb.Model.Permission", b =>
{
b.Property<int>("IdUserRole")
.HasColumnType("integer")
.HasColumnName("id_user_role");
b.Property<int>("IdPermissionInfo")
.HasColumnType("integer")
.HasColumnName("id_permission_info");
b.Property<int>("Value")
.HasColumnType("integer")
.HasColumnName("value");
b.HasKey("IdUserRole", "IdPermissionInfo");
b.HasIndex("IdPermissionInfo");
b.ToTable("t_permission");
b
.HasComment("Отношение ролей пользователей и разрешений доступа");
});
modelBuilder.Entity("AsbCloudDb.Model.PermissionInfo", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
@ -713,11 +689,6 @@ namespace AsbCloudDb.Migrations
.HasColumnName("id")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<IDictionary<int, string>>("BitDescription")
.HasColumnType("jsonb")
.HasColumnName("bit_description")
.HasComment("Описание каждого бита разрешений");
b.Property<string>("Description")
.HasMaxLength(255)
.HasColumnType("character varying(255)")
@ -732,7 +703,7 @@ namespace AsbCloudDb.Migrations
b.HasKey("Id");
b.ToTable("t_permission_info");
b.ToTable("t_permission");
b
.HasComment("Разрешения на доступ к данным");
@ -758,6 +729,30 @@ namespace AsbCloudDb.Migrations
.HasComment("отношение скважин и компаний");
});
modelBuilder.Entity("AsbCloudDb.Model.RelationUserRolePermission", b =>
{
b.Property<int>("IdUserRole")
.HasColumnType("integer")
.HasColumnName("id_user_role");
b.Property<int>("IdPermission")
.HasColumnType("integer")
.HasColumnName("id_permission");
b.Property<int>("Id")
.HasColumnType("integer")
.HasColumnName("id");
b.HasKey("IdUserRole", "IdPermission");
b.HasIndex("IdPermission");
b.ToTable("t_relation_user_role_permission");
b
.HasComment("Отношение ролей пользователей и разрешений доступа");
});
modelBuilder.Entity("AsbCloudDb.Model.RelationUserUserRole", b =>
{
b.Property<int>("IdUser")
@ -768,6 +763,10 @@ namespace AsbCloudDb.Migrations
.HasColumnType("integer")
.HasColumnName("id_user_role");
b.Property<int>("Id")
.HasColumnType("integer")
.HasColumnName("id");
b.HasKey("IdUser", "IdUserRole");
b.HasIndex("IdUserRole");
@ -781,7 +780,8 @@ namespace AsbCloudDb.Migrations
new
{
IdUser = 1,
IdUserRole = 2
IdUserRole = 2,
Id = 0
});
});
@ -2601,25 +2601,6 @@ namespace AsbCloudDb.Migrations
b.Navigation("Well");
});
modelBuilder.Entity("AsbCloudDb.Model.Permission", b =>
{
b.HasOne("AsbCloudDb.Model.PermissionInfo", "PermissionInfo")
.WithMany("Permissions")
.HasForeignKey("IdPermissionInfo")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("AsbCloudDb.Model.UserRole", "UserRole")
.WithMany("Permissions")
.HasForeignKey("IdUserRole")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("PermissionInfo");
b.Navigation("UserRole");
});
modelBuilder.Entity("AsbCloudDb.Model.RelationCompanyWell", b =>
{
b.HasOne("AsbCloudDb.Model.Company", "Company")
@ -2641,6 +2622,25 @@ namespace AsbCloudDb.Migrations
b.Navigation("Well");
});
modelBuilder.Entity("AsbCloudDb.Model.RelationUserRolePermission", b =>
{
b.HasOne("AsbCloudDb.Model.Permission", "Permission")
.WithMany("RelationUserRolePermissions")
.HasForeignKey("IdPermission")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("AsbCloudDb.Model.UserRole", "UserRole")
.WithMany("RelationUserRolePermissions")
.HasForeignKey("IdUserRole")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Permission");
b.Navigation("UserRole");
});
modelBuilder.Entity("AsbCloudDb.Model.RelationUserUserRole", b =>
{
b.HasOne("AsbCloudDb.Model.User", "User")
@ -2903,9 +2903,9 @@ namespace AsbCloudDb.Migrations
b.Navigation("Measures");
});
modelBuilder.Entity("AsbCloudDb.Model.PermissionInfo", b =>
modelBuilder.Entity("AsbCloudDb.Model.Permission", b =>
{
b.Navigation("Permissions");
b.Navigation("RelationUserRolePermissions");
});
modelBuilder.Entity("AsbCloudDb.Model.Telemetry", b =>
@ -2936,7 +2936,7 @@ namespace AsbCloudDb.Migrations
modelBuilder.Entity("AsbCloudDb.Model.UserRole", b =>
{
b.Navigation("Permissions");
b.Navigation("RelationUserRolePermissions");
b.Navigation("RelationUsersUserRoles");
});

View File

@ -41,7 +41,7 @@ namespace AsbCloudDb.Model
public virtual DbSet<DrillFlowChart> DrillFlowChart { get; set; }
public virtual DbSet<RelationUserUserRole> RelationUserUserRoles { get; set; }
public virtual DbSet<Permission> Permissions { get; set; }
public virtual DbSet<PermissionInfo> PermissionInfos { get; set; }
public virtual DbSet<Permission> PermissionInfos { get; set; }
//var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
// .UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True")
@ -254,9 +254,9 @@ namespace AsbCloudDb.Model
entity.HasKey(e => new { e.IdUser, e.IdUserRole });
});
modelBuilder.Entity<Permission>(entity =>
modelBuilder.Entity<RelationUserRolePermission>(entity =>
{
entity.HasKey(e => new { e.IdUserRole, e.IdPermissionInfo });
entity.HasKey(e => new { e.IdUserRole, e.IdPermission });
});
FillData(modelBuilder);

View File

@ -38,7 +38,7 @@ namespace AsbCloudDb.Model
DbSet<DrillFlowChart> DrillFlowChart { get; set; }
DbSet<RelationUserUserRole> RelationUserUserRoles { get; set; }
DbSet<Permission> Permissions { get; set; }
DbSet<PermissionInfo> PermissionInfos { get; set; }
DbSet<Permission> PermissionInfos { get; set; }
DatabaseFacade Database { get; }

View File

@ -1,26 +1,26 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace AsbCloudDb.Model
{
[Table("t_permission"), Comment("Отношение ролей пользователей и разрешений доступа")]
public class Permission
[Table("t_permission"), Comment("Разрешения на доступ к данным")]
public class Permission : IId
{
[Column("id_user_role")]
public int IdUserRole { get; set; }
[Key]
[Column("id")]
public int Id { get; set; }
[Column("id_permission_info")]
public int IdPermissionInfo { get; set; }
[Column("name"), Comment("Название")]
[StringLength(255)]
public string Name { get; set; }
[Column("value")]
public int Value { get; set; }
[Column("description"), Comment("Краткое описание")]
[StringLength(255)]
public string Description { get; set; }
[ForeignKey(nameof(IdUserRole))]
[InverseProperty(nameof(Model.UserRole.Permissions))]
public virtual UserRole UserRole { get; set; }
[ForeignKey(nameof(IdPermissionInfo))]
[InverseProperty(nameof(Model.PermissionInfo.Permissions))]
public virtual PermissionInfo PermissionInfo { get; set; }
[InverseProperty(nameof(RelationUserRolePermission.Permission))]
public virtual ICollection<RelationUserRolePermission> RelationUserRolePermissions { get; set; }
}
}

View File

@ -1,29 +0,0 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace AsbCloudDb.Model
{
[Table("t_permission_info"), Comment("Разрешения на доступ к данным")]
public class PermissionInfo : IId
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("name"), Comment("Название")]
[StringLength(255)]
public string Name { get; set; }
[Column("description"), Comment("Краткое описание")]
[StringLength(255)]
public string Description { get; set; }
[Column("bit_description", TypeName = "jsonb"), Comment("Описание каждого бита разрешений")]
public IDictionary<int, string> BitDescription { get; set; }
[InverseProperty(nameof(Permission.PermissionInfo))]
public virtual ICollection<Permission> Permissions { get; set; }
}
}

View File

@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace AsbCloudDb.Model
{
[Table("t_relation_user_role_permission"), Comment("Отношение ролей пользователей и разрешений доступа")]
public class RelationUserRolePermission
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("id_user_role")]
public int IdUserRole { get; set; }
[Column("id_permission")]
public int IdPermission { get; set; }
[ForeignKey(nameof(IdUserRole))]
[InverseProperty(nameof(Model.UserRole.RelationUserRolePermissions))]
public virtual UserRole UserRole { get; set; }
[ForeignKey(nameof(IdPermission))]
[InverseProperty(nameof(Model.Permission.RelationUserRolePermissions))]
public virtual Permission Permission { get; set; }
}
}

View File

@ -7,6 +7,10 @@ namespace AsbCloudDb.Model
[Table("t_relation_user_user_role"), Comment("Отношение пользователей и ролей")]
public class RelationUserUserRole
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("id_user")]
public int IdUser { get; set; }

View File

@ -27,7 +27,7 @@ namespace AsbCloudDb.Model
[InverseProperty(nameof(RelationUserUserRole.UserRole))]
public virtual ICollection<RelationUserUserRole> RelationUsersUserRoles { get; set; }
[InverseProperty(nameof(Permission.UserRole))]
public virtual ICollection<Permission> Permissions { get; set; }
[InverseProperty(nameof(RelationUserRolePermission.UserRole))]
public virtual ICollection<RelationUserRolePermission> RelationUserRolePermissions { get; set; }
}
}

View File

@ -66,7 +66,7 @@ namespace AsbCloudInfrastructure
// admin crud services:
services.AddTransient<ICrudService<WellDto>, CrudServiceBase<WellDto, Well>>();
services.AddTransient<ICrudService<TelemetryDto>, CrudServiceBase<TelemetryDto, Telemetry>>();
services.AddTransient<ICrudService<PermissionInfoDto>, CrudServiceBase<PermissionInfoDto, PermissionInfo>>();
services.AddTransient<ICrudService<PermissionInfoDto>, CrudServiceBase<PermissionInfoDto, Permission>>();
services.AddTransient<ICrudService<DrillParamsDto>, DrillParamsService>();
services.AddTransient<ICrudService<DepositDto>, CrudServiceBase<DepositDto, Deposit>>();
services.AddTransient<ICrudService<CompanyDto>, CrudServiceBase<CompanyDto, Company>>();

View File

@ -11,23 +11,26 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
public class PermissionService : IPermissionService, IConverter<PermissionDto, Permission>
public class PermissionService : IPermissionService
{
private readonly CacheTable<RelationUserRolePermission> cacheUserRolePermission;
private readonly CacheTable<Permission> cachePermission;
public PermissionService(IAsbCloudDbContext db, CacheDb cacheDb)
{
cachePermission = cacheDb.GetCachedTable<Permission>(
cacheUserRolePermission = cacheDb.GetCachedTable<RelationUserRolePermission>(
(AsbCloudDbContext)db,
new string[] { nameof(Permission.PermissionInfo) });
new string[] { nameof(RelationUserRolePermission.Permission) });
cachePermission = cacheDb.GetCachedTable<Permission>(
(AsbCloudDbContext)db);
}
public async Task<IEnumerable<PermissionDto>> GetByIdRoleAsync(int idRole, CancellationToken token)
{
var entities = await cachePermission
var entities = await cacheUserRolePermission
.WhereAsync(p => p.IdUserRole == idRole, token)
.ConfigureAwait(false);
var dto = entities.Select(Convert);
var dto = entities.Select(e => e.Permission).Adapt<PermissionDto>();
return dto;
}
@ -47,21 +50,21 @@ namespace AsbCloudInfrastructure.Services
public Task<int> DeleteAsync(int idUserRole, int idPermission, CancellationToken token)
{
bool predicate(Permission p) => p.IdUserRole == idUserRole && p.IdPermissionInfo == idPermission;
bool predicate(RelationUserRolePermission p) => p.IdUserRole == idUserRole && p.IdPermission == idPermission;
return DeleteAsync(predicate, token);
}
public Task<int> DeleteAllByRoleAsync(int idUserRole, CancellationToken token)
{
bool predicate(Permission p) => p.IdUserRole == idUserRole;
bool predicate(RelationUserRolePermission p) => p.IdUserRole == idUserRole;
return DeleteAsync(predicate, token);
}
private async Task<int> DeleteAsync(Func<Permission, bool> predicate, CancellationToken token)
private async Task<int> DeleteAsync(Func<RelationUserRolePermission, bool> predicate, CancellationToken token)
{
var count = (await cachePermission.WhereAsync(predicate, token).ConfigureAwait(false)).Count();
var count = (await cacheUserRolePermission.WhereAsync(predicate, token).ConfigureAwait(false)).Count();
if (count > 0)
await cachePermission.RemoveAsync(predicate, token)
await cacheUserRolePermission.RemoveAsync(predicate, token)
.ConfigureAwait(false);
return count;
}
@ -71,13 +74,5 @@ namespace AsbCloudInfrastructure.Services
var entity = src.Adapt<Permission>();
return entity;
}
public PermissionDto Convert(Permission src)
{
var dto = src.Adapt<PermissionDto>();
dto.PermissionName = src.PermissionInfo?.Name;
return dto;
}
}
}

View File

@ -15,14 +15,14 @@ namespace AsbCloudInfrastructure.Services
public class UserRoleService : IUserRoleService
{
private readonly CacheTable<UserRole> cacheUserRoles;
private readonly CacheTable<PermissionInfo> cachePermissionInfo;
private readonly CacheTable<Permission> cachePermission;
private readonly IPermissionService permissionService;
public List<string> Includes { get; } = new();
public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb, IPermissionService permissionService)
{
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context, new [] { nameof(UserRole.Permissions) });
cachePermissionInfo = cacheDb.GetCachedTable<PermissionInfo>((AsbCloudDbContext)context);
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context, new [] { nameof(UserRole.RelationUserRolePermissions) });
cachePermission = cacheDb.GetCachedTable<Permission>((AsbCloudDbContext)context);
this.permissionService = permissionService;
}
@ -106,19 +106,18 @@ namespace AsbCloudInfrastructure.Services
foreach (var newPermission in rolePermissions)
{
if (permissions.ContainsKey(newPermission.IdPermissionInfo))
if (permissions.ContainsKey(newPermission.Id))
{
permissions[newPermission.IdPermissionInfo].Value |= newPermission.Value;
permissions[newPermission.Id] = newPermission.Adapt<PermissionBaseDto>();
}
else
{
permissions.Add(newPermission.IdPermissionInfo,
permissions.Add(newPermission.Id,
new PermissionBaseDto
{
IdPermissionInfo = newPermission.IdPermissionInfo,
PermissionName = newPermission.PermissionInfo?.Name ??
cachePermissionInfo.FirstOrDefault(p => p.Id == newPermission.IdPermissionInfo).Name,
Value = newPermission.Value,
Id = newPermission.Id,
PermissionName = newPermission.Name ??
cachePermission.FirstOrDefault(p => p.Id == newPermission.Id).Name
});
}
}
@ -136,10 +135,9 @@ namespace AsbCloudInfrastructure.Services
var newPermissions = roleDto.Permissions.Select(p => new PermissionDto
{
IdPermissionInfo = p.IdPermissionInfo,
Id = p.Id,
IdUserRole = roleDto.Id,
PermissionName = p.PermissionName,
Value = p.Value,
PermissionName = p.PermissionName
});
await permissionService.InsertRangeAsync(newPermissions, token)
@ -148,22 +146,23 @@ namespace AsbCloudInfrastructure.Services
private IEnumerable<Permission> GetNestedPermissions(UserRole role, int recursionLevel = 7)
{
var permissions = role.Permissions.ToList();
var permissionRelations = role.RelationUserRolePermissions.ToList();
var permissionIds = permissionRelations.Select(p => p.IdPermission);
if (role.IdParent is null)
return permissions;
return cachePermission.Where(c => permissionIds.Contains(c.Id));
if (recursionLevel == 0)
{
Trace.WriteLine($"User role with id: {role.Id} has more than 10 nested childs");
return permissions;
Trace.WriteLine($"User role with id: {role.Id} has more than 10 nested children");
cachePermission.Where(c => permissionIds.Contains(c.Id));
}
var parentRole = cacheUserRoles.FirstOrDefault(r => r.Id == role.IdParent);
if (parentRole is null)
return permissions;
return cachePermission.Where(c => permissionIds.Contains(c.Id));
var parentPermissions = GetNestedPermissions(parentRole, --recursionLevel);
return permissions.Union(parentPermissions);
return cachePermission.Where(c => permissionIds.Contains(c.Id)).Union(parentPermissions);
}
public Task<int> DeleteAsync(int id, CancellationToken token = default)
@ -174,7 +173,7 @@ namespace AsbCloudInfrastructure.Services
public bool HasPermission(IEnumerable<int> rolesIds, string permissionName, int permissionMask = 0)
{
var permissionInfo = cachePermissionInfo.FirstOrDefault(p => p.Name.ToLower() == permissionName.ToLower());
var permissionInfo = cachePermission.FirstOrDefault(p => p.Name.ToLower() == permissionName.ToLower());
if (permissionInfo is null)
return false;
@ -190,14 +189,14 @@ namespace AsbCloudInfrastructure.Services
return false;
}
private bool HasPermission(UserRole userRole, int idPermissionInfo, int permissionMask, int recursionLevel = 7)
private bool HasPermission(UserRole userRole, int idPermission, int recursionLevel = 7)
{
if (userRole.Permissions.Any(p => p.IdPermissionInfo == idPermissionInfo && (p.Value & permissionMask) > 0))
if (userRole.RelationUserRolePermissions.Any(p => p.IdPermission == idPermission))
return true;
if (userRole.IdParent is not null && recursionLevel > 0)
{
var parentRole = cacheUserRoles.FirstOrDefault(p => p.Id == userRole.IdParent);
return HasPermission(parentRole, idPermissionInfo, permissionMask, --recursionLevel);
return HasPermission(parentRole, idPermission, --recursionLevel);
}
return false;
}