asp.netMVC如何实现动态资源权限?
How to implement dynamic resource permission in asp.net MVC?
我想在我的 ASP.NET MVC 项目中实现动态授权,其中资源权限可以由系统管理员动态分配给用户和角色。我正在使用以下 table 来管理权限。
- 资源
- 资源权限
这些 table 的模型是
public class Resource
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int? ParentResourceId { get; set; }
public string Name { get; set; }
public string DisplayName { get; set; }
public int Order { get; set; }
public Resource ParentResource { get; set; }
public ICollection<Permission> Permissions { get; set; }
}
public class Permission
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int ResourceId { get; set; }
public string Name { get; set; }
public string DisplayName { get; set; }
public int Order { get; set; }
public Resource Resource { get; set; }
}
我需要根据资源层次结构 在 UI 树视图上显示资源权限 ,以便管理员可以检查并分配权限给用户和角色。使用递归函数(如下所示)我可以创建资源层次结构(仅来自资源 table)但不能包含对此层次结构的权限。
目前我试过的代码如下:
public List<PermissionTreeDto> GetPermissions()
{
var permissions = _context.Resources
.Where(r => r.OrganizationId == OrganizationId)
.ToList();
return permissions.Where(r => r.ParentResourceId == null)
.OrderBy(r => r.Order)
.Select(r => new PermissionTreeDto
{
id = 0,
text = r.DisplayName,
@checked = false,
children = GetChildren(permissions, r.Id)
}).ToList();
}
private List<PermissionTreeDto> GetChildren(List<Resource> permissions, int parentId)
{
return permissions.Where(r => r.ParentResourceId == parentId)
.OrderBy(r => r.Order)
.Select(r => new PermissionTreeDto
{
id = 0,
text = r.DisplayName,
@checked = false,
children = GetChildren(permissions, r.Id)
}).ToList();
}
我正在使用 gijgo bootstrap 树视图:
我的模型设计也可能有任何错误。在那种情况下,请提出任何更好的方法来实现我期望的相同目标。
我已经通过这种方式管理了层次结构-
public List<ResourceTreeDto> GetPermissions()
{
var resources = _context.Resources.Include(r=>r.Permissions)
.Where(r => r.OrganizationId == OrganizationId)
.ToList();
return resources.Where(r => r.ParentResourceId == null)
.OrderBy(r => r.Order)
.Select(r => new ResourceTreeDto
{
id = 0,
text = r.DisplayName,
@checked = false,
children = r.Permissions.OrderBy(p=>p.Order).Select(p => new ResourceTreeDto()
{
id = p.Id,
text = p.DisplayName,
@checked = false
}).Union(GetChildren(resources, r.Id))
}).ToList();
}
private IEnumerable<ResourceTreeDto> GetChildren(List<Resource> resources, int parentId)
{
return resources.Where(r => r.ParentResourceId == parentId)
.OrderBy(r => r.Order)
.Select(r => new ResourceTreeDto
{
id = 0,
text = r.DisplayName,
@checked = false,
children = r.Permissions.OrderBy(p => p.Order).Select(p => new ResourceTreeDto()
{
id = p.Id,
text = p.DisplayName,
@checked = false
}).Union(GetChildren(resources, r.Id))
});
}
现在我得到了预期的输出——
我想在我的 ASP.NET MVC 项目中实现动态授权,其中资源权限可以由系统管理员动态分配给用户和角色。我正在使用以下 table 来管理权限。
- 资源
- 资源权限
这些 table 的模型是
public class Resource
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int? ParentResourceId { get; set; }
public string Name { get; set; }
public string DisplayName { get; set; }
public int Order { get; set; }
public Resource ParentResource { get; set; }
public ICollection<Permission> Permissions { get; set; }
}
public class Permission
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int ResourceId { get; set; }
public string Name { get; set; }
public string DisplayName { get; set; }
public int Order { get; set; }
public Resource Resource { get; set; }
}
我需要根据资源层次结构 在 UI 树视图上显示资源权限 ,以便管理员可以检查并分配权限给用户和角色。使用递归函数(如下所示)我可以创建资源层次结构(仅来自资源 table)但不能包含对此层次结构的权限。
目前我试过的代码如下:
public List<PermissionTreeDto> GetPermissions()
{
var permissions = _context.Resources
.Where(r => r.OrganizationId == OrganizationId)
.ToList();
return permissions.Where(r => r.ParentResourceId == null)
.OrderBy(r => r.Order)
.Select(r => new PermissionTreeDto
{
id = 0,
text = r.DisplayName,
@checked = false,
children = GetChildren(permissions, r.Id)
}).ToList();
}
private List<PermissionTreeDto> GetChildren(List<Resource> permissions, int parentId)
{
return permissions.Where(r => r.ParentResourceId == parentId)
.OrderBy(r => r.Order)
.Select(r => new PermissionTreeDto
{
id = 0,
text = r.DisplayName,
@checked = false,
children = GetChildren(permissions, r.Id)
}).ToList();
}
我正在使用 gijgo bootstrap 树视图:
我的模型设计也可能有任何错误。在那种情况下,请提出任何更好的方法来实现我期望的相同目标。
我已经通过这种方式管理了层次结构-
public List<ResourceTreeDto> GetPermissions()
{
var resources = _context.Resources.Include(r=>r.Permissions)
.Where(r => r.OrganizationId == OrganizationId)
.ToList();
return resources.Where(r => r.ParentResourceId == null)
.OrderBy(r => r.Order)
.Select(r => new ResourceTreeDto
{
id = 0,
text = r.DisplayName,
@checked = false,
children = r.Permissions.OrderBy(p=>p.Order).Select(p => new ResourceTreeDto()
{
id = p.Id,
text = p.DisplayName,
@checked = false
}).Union(GetChildren(resources, r.Id))
}).ToList();
}
private IEnumerable<ResourceTreeDto> GetChildren(List<Resource> resources, int parentId)
{
return resources.Where(r => r.ParentResourceId == parentId)
.OrderBy(r => r.Order)
.Select(r => new ResourceTreeDto
{
id = 0,
text = r.DisplayName,
@checked = false,
children = r.Permissions.OrderBy(p => p.Order).Select(p => new ResourceTreeDto()
{
id = p.Id,
text = p.DisplayName,
@checked = false
}).Union(GetChildren(resources, r.Id))
});
}
现在我得到了预期的输出——