forked from ddrilling/AsbCloudServer
CS2-133: Removed permission mask logic from permissions
This commit is contained in:
parent
888c28cdf8
commit
17db1218cc
@ -3,6 +3,6 @@
|
||||
public class PermissionBaseDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string PermissionName { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
@ -10,6 +10,6 @@ namespace AsbCloudApp.Services
|
||||
Task<UserRoleDto> GetByNameAsync(string name, CancellationToken token = default);
|
||||
List<UserRoleDto> GetNestedById(int id, int counter = 10);
|
||||
IEnumerable<PermissionBaseDto> GetNestedPermissions(IEnumerable<UserRoleDto> roles);
|
||||
bool HasPermission(IEnumerable<int> rolesIds, string permissionName, int permissionMask = 0);
|
||||
bool HasPermission(IEnumerable<int> rolesIds, string permissionName);
|
||||
}
|
||||
}
|
@ -10,6 +10,6 @@ namespace AsbCloudApp.Services
|
||||
IEnumerable<UserRoleDto> GetRolesByIdUser(int idUser);
|
||||
bool HasAnyRoleOf(int idUser, IEnumerable<string> roleNames);
|
||||
bool HasAnyRoleOf(int idUser, IEnumerable<int> roleIds);
|
||||
public bool HasPermission(int idUser, string permissionName, int permissionMask = 0);
|
||||
public bool HasPermission(int idUser, string permissionName);
|
||||
}
|
||||
}
|
||||
|
2969
AsbCloudDb/Migrations/20211216072650_RemovedIdFromPermissionRelations.Designer.cs
generated
Normal file
2969
AsbCloudDb/Migrations/20211216072650_RemovedIdFromPermissionRelations.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,35 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace AsbCloudDb.Migrations
|
||||
{
|
||||
public partial class RemovedIdFromPermissionRelations : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "id",
|
||||
table: "t_relation_user_user_role");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "id",
|
||||
table: "t_relation_user_role_permission");
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<int>(
|
||||
name: "id",
|
||||
table: "t_relation_user_user_role",
|
||||
type: "integer",
|
||||
nullable: false,
|
||||
defaultValue: 0);
|
||||
|
||||
migrationBuilder.AddColumn<int>(
|
||||
name: "id",
|
||||
table: "t_relation_user_role_permission",
|
||||
type: "integer",
|
||||
nullable: false,
|
||||
defaultValue: 0);
|
||||
}
|
||||
}
|
||||
}
|
@ -739,10 +739,6 @@ namespace AsbCloudDb.Migrations
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id_permission");
|
||||
|
||||
b.Property<int>("Id")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id");
|
||||
|
||||
b.HasKey("IdUserRole", "IdPermission");
|
||||
|
||||
b.HasIndex("IdPermission");
|
||||
@ -763,10 +759,6 @@ namespace AsbCloudDb.Migrations
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id_user_role");
|
||||
|
||||
b.Property<int>("Id")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id");
|
||||
|
||||
b.HasKey("IdUser", "IdUserRole");
|
||||
|
||||
b.HasIndex("IdUserRole");
|
||||
@ -780,8 +772,7 @@ namespace AsbCloudDb.Migrations
|
||||
new
|
||||
{
|
||||
IdUser = 1,
|
||||
IdUserRole = 2,
|
||||
Id = 0
|
||||
IdUserRole = 2
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -7,10 +7,6 @@ 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; }
|
||||
|
||||
|
@ -7,10 +7,6 @@ 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; }
|
||||
|
||||
|
@ -116,7 +116,7 @@ namespace AsbCloudInfrastructure.Services
|
||||
new PermissionBaseDto
|
||||
{
|
||||
Id = newPermission.Id,
|
||||
PermissionName = newPermission.Name ??
|
||||
Name = newPermission.Name ??
|
||||
cachePermission.FirstOrDefault(p => p.Id == newPermission.Id).Name
|
||||
});
|
||||
}
|
||||
@ -137,7 +137,7 @@ namespace AsbCloudInfrastructure.Services
|
||||
{
|
||||
Id = p.Id,
|
||||
IdUserRole = roleDto.Id,
|
||||
PermissionName = p.PermissionName
|
||||
Name = p.Name
|
||||
});
|
||||
|
||||
await permissionService.InsertRangeAsync(newPermissions, token)
|
||||
@ -171,20 +171,17 @@ namespace AsbCloudInfrastructure.Services
|
||||
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, int permissionMask = 0)
|
||||
public bool HasPermission(IEnumerable<int> rolesIds, string permissionName)
|
||||
{
|
||||
var permissionInfo = cachePermission.FirstOrDefault(p => p.Name.ToLower() == permissionName.ToLower());
|
||||
|
||||
if (permissionInfo is null)
|
||||
return false;
|
||||
|
||||
if (permissionMask == 0)
|
||||
permissionMask = -1;
|
||||
|
||||
var idPermissionInfo = permissionInfo.Id;
|
||||
var roles = cacheUserRoles.Where(r => rolesIds.Contains(r.Id));
|
||||
foreach (var role in roles)
|
||||
if (HasPermission(role, idPermissionInfo, permissionMask))
|
||||
if (HasPermission(role, idPermissionInfo))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
@ -152,15 +152,14 @@ namespace AsbCloudInfrastructure.Services
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool HasPermission(int idUser, string permissionName, int permissionMask = 0)
|
||||
public bool HasPermission(int idUser, string permissionName)
|
||||
{
|
||||
var relationsToRoles = cacheRelationUserToRoles.Where(r=>r.IdUser == idUser);
|
||||
if (relationsToRoles is null)
|
||||
return false;
|
||||
|
||||
return RoleService.HasPermission(relationsToRoles.Select(r => r.IdUserRole),
|
||||
permissionName,
|
||||
permissionMask);
|
||||
permissionName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ namespace AsbCloudWebApi.Controllers
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(IEnumerable<PermissionDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||
[Permission]
|
||||
public async Task<IActionResult> GetByIdRoleAsync(int idRole, CancellationToken token = default)
|
||||
{
|
||||
var result = await permissionService.GetByIdRoleAsync(idRole, token);
|
||||
|
@ -42,10 +42,9 @@ namespace AsbCloudWebApi.Middlewares
|
||||
.GetMetadata<Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor>()
|
||||
?.ControllerName
|
||||
.ToLower();
|
||||
|
||||
var permissionMask = permission.Mask;
|
||||
|
||||
var userService = context.RequestServices.GetRequiredService<IUserService>();
|
||||
var isAuthorized = userService.HasPermission((int)idUser, permissionName, permissionMask);
|
||||
var isAuthorized = userService.HasPermission((int)idUser, permissionName);
|
||||
|
||||
if(isAuthorized)
|
||||
await next?.Invoke(context);
|
||||
|
@ -49,7 +49,6 @@ namespace AsbCloudWebApi
|
||||
public static SortedSet<string> Registered { get; } = new SortedSet<string>();
|
||||
|
||||
public string Name { get; set; }
|
||||
public int Mask { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Проверка наличия у пользователя разрешения.
|
||||
@ -59,7 +58,6 @@ namespace AsbCloudWebApi
|
||||
public PermissionAttribute(string name = default, int mask = -1)
|
||||
{
|
||||
Name = name;
|
||||
Mask = mask;
|
||||
Registered.Add(name);
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ namespace ConsoleApp1
|
||||
{
|
||||
static void Main(/*string[] args*/)
|
||||
{
|
||||
ControllerLoadTester.TestControllerRoute();
|
||||
DbDemoDataService.AddDemoData();
|
||||
//.GetAwaiter().GetResult();
|
||||
|
||||
Console.WriteLine("End of Test");
|
||||
|
Loading…
Reference in New Issue
Block a user