From 257f84b00e305c1d015ffd5b40c196bbc0cd8955 Mon Sep 17 00:00:00 2001 From: IGeekFan Date: Fri, 24 May 2024 23:17:08 +0800 Subject: [PATCH] feat: Permission Tree Add Create And Update Delete --- .../Cms/Permissions/IPermissionService.cs | 7 +- .../Permissions/PermissioCreateUpdateDto.cs | 29 ++++++++ .../Cms/Permissions/PermissionDto.cs | 21 +++--- .../LinCms.Application.Contracts.xml | 20 ++++++ .../Cms/Permissions/PermissionProfile.cs | 3 +- .../Cms/Permissions/PermissionService.cs | 67 ++++++++----------- .../Cms/Permissions/TreeBuilder.cs | 51 ++++++++++++++ .../LinCms.Application.csproj | 3 - src/LinCms.Core/Entities/LinPermission.cs | 6 ++ src/LinCms.Core/LinCms.Core.xml | 5 ++ .../Controllers/Cms/PermissionController.cs | 39 +++++++++-- .../PermissionAuthorizationHandler.cs | 2 +- 12 files changed, 190 insertions(+), 63 deletions(-) create mode 100644 src/LinCms.Application.Contracts/Cms/Permissions/PermissioCreateUpdateDto.cs create mode 100644 src/LinCms.Application/Cms/Permissions/TreeBuilder.cs diff --git a/src/LinCms.Application.Contracts/Cms/Permissions/IPermissionService.cs b/src/LinCms.Application.Contracts/Cms/Permissions/IPermissionService.cs index 3ebe4a49..cbfe2761 100644 --- a/src/LinCms.Application.Contracts/Cms/Permissions/IPermissionService.cs +++ b/src/LinCms.Application.Contracts/Cms/Permissions/IPermissionService.cs @@ -8,7 +8,7 @@ namespace LinCms.Cms.Permissions; public interface IPermissionService { Task> GetPermissionTreeNodes(); - Task CheckPermissionAsync(string module, string permission); + Task CheckPermissionAsync(string permission); Task DeletePermissionsAsync(RemovePermissionDto permissionDto); Task DispatchPermissions(DispatchPermissionsDto permissionDto, List permissionDefinition); @@ -17,6 +17,11 @@ public interface IPermissionService List> StructuringPermissions(List permissions); + Task UpdateAsync(int id,PermissioCreateUpdateDto createUpdateDto); + Task CreateAsync(PermissioCreateUpdateDto createUpdateDto); + + Task DeleteAsync(int id); + Task GetAsync(string permissionName); } \ No newline at end of file diff --git a/src/LinCms.Application.Contracts/Cms/Permissions/PermissioCreateUpdateDto.cs b/src/LinCms.Application.Contracts/Cms/Permissions/PermissioCreateUpdateDto.cs new file mode 100644 index 00000000..c7ce91e5 --- /dev/null +++ b/src/LinCms.Application.Contracts/Cms/Permissions/PermissioCreateUpdateDto.cs @@ -0,0 +1,29 @@ +using LinCms.Entities; + +namespace LinCms.Cms.Permissions; + +public class PermissioCreateUpdateDto +{ + + public PermissionType PermissionType { get; set; } + + /// + /// 父级Id + /// + public long ParentId { get; set; } + + /// + /// 所属权限、权限名称,例如:访问首页 + /// + public string Name { get; set; } + + /// + /// 后台路由 + /// + public string Router { get; set; } + + /// + /// 排序 + /// + public int SortCode { get; set; } +} \ No newline at end of file diff --git a/src/LinCms.Application.Contracts/Cms/Permissions/PermissionDto.cs b/src/LinCms.Application.Contracts/Cms/Permissions/PermissionDto.cs index 468fe1e8..6be9bcad 100644 --- a/src/LinCms.Application.Contracts/Cms/Permissions/PermissionDto.cs +++ b/src/LinCms.Application.Contracts/Cms/Permissions/PermissionDto.cs @@ -7,23 +7,23 @@ namespace LinCms.Cms.Permissions; public class PermissionDto : EntityDto { - public string Name { get; set; } - - public PermissionType PermissionType { get; set; } - /// /// 父级Id /// public long ParentId { get; set; } + public string Name { get; set; } + + public PermissionType PermissionType { get; set; } public string Router { get; set; } } -public class PermissionTreeNode : PermissionNode +public class PermissionTreeNode : TreeNode { + public int SortCode { get; set; } public string Router { get; set; } - + public PermissionType PermissionType { get; set; } public DateTime? CreateTime { get; set; } public List Children { get; set; } @@ -34,16 +34,15 @@ public PermissionTreeNode() } -public class PermissionNode +public class TreeNode { public long Id { get; set; } - public PermissionType PermissionType { get; set; } public long ParentId { get; set; } public string Name { get; set; } - public List Children { get; set; } + public List Children { get; set; } - public PermissionNode() + public TreeNode() { - Children = new List(); + Children = new List(); } } \ No newline at end of file diff --git a/src/LinCms.Application.Contracts/LinCms.Application.Contracts.xml b/src/LinCms.Application.Contracts/LinCms.Application.Contracts.xml index e82abdeb..71cd9cad 100644 --- a/src/LinCms.Application.Contracts/LinCms.Application.Contracts.xml +++ b/src/LinCms.Application.Contracts/LinCms.Application.Contracts.xml @@ -780,6 +780,26 @@ 新增用户数(月) + + + 父级Id + + + + + 所属权限、权限名称,例如:访问首页 + + + + + 后台路由 + + + + + 排序 + + 父级Id diff --git a/src/LinCms.Application/Cms/Permissions/PermissionProfile.cs b/src/LinCms.Application/Cms/Permissions/PermissionProfile.cs index 2cae15e2..3204af31 100644 --- a/src/LinCms.Application/Cms/Permissions/PermissionProfile.cs +++ b/src/LinCms.Application/Cms/Permissions/PermissionProfile.cs @@ -8,7 +8,8 @@ public class PermissionProfile : Profile public PermissionProfile() { CreateMap(); - CreateMap(); + CreateMap(); CreateMap(); + CreateMap(); } } \ No newline at end of file diff --git a/src/LinCms.Application/Cms/Permissions/PermissionService.cs b/src/LinCms.Application/Cms/Permissions/PermissionService.cs index 4024242c..e6feb660 100644 --- a/src/LinCms.Application/Cms/Permissions/PermissionService.cs +++ b/src/LinCms.Application/Cms/Permissions/PermissionService.cs @@ -1,44 +1,18 @@ -using System; -using System.Collections.Generic; -using System.Dynamic; -using System.Linq; -using System.Threading.Tasks; -using IGeekFan.FreeKit.Extras.FreeSql; +using IGeekFan.FreeKit.Extras.FreeSql; using IGeekFan.FreeKit.Extras.Security; using LinCms.Common; using LinCms.Data; using LinCms.Entities; using LinCms.Security; +using System; +using System.Collections.Generic; +using System.Dynamic; +using System.Linq; +using System.Security; +using System.Threading.Tasks; namespace LinCms.Cms.Permissions; -public class PermissionTreeBuilder -{ - public List BuildTree(List nodes) - { - var nodeDict = nodes.ToDictionary(n => n.Id); - List roots = new List(); - - foreach (var node in nodes) - { - if (node.ParentId == 0) // 假定ParentId为0表示根节点 - { - roots.Add(node); - } - else - { - if (nodeDict.TryGetValue(node.ParentId, out PermissionTreeNode parentNode)) - { - parentNode.Children.Add(node); - } - } - } - - return roots; // 返回森林中所有根节点的列表 - } - -} - public class PermissionService(IAuditBaseRepository permissionRepository, IAuditBaseRepository groupPermissionRepository, ICurrentUser currentUser) : ApplicationService, IPermissionService @@ -47,19 +21,17 @@ public class PermissionService(IAuditBaseRepository permiss public async Task> GetPermissionTreeNodes() { - var permissionList = await permissionRepository.Select.ToListAsync(); + var permissionList = await permissionRepository.Select.OrderBy(r => r.SortCode).ToListAsync(); var nodes = Mapper.Map, List>(permissionList); - return new PermissionTreeBuilder().BuildTree(nodes); + return new TreeBuilder().BuildPermissionTree(nodes); } - /// /// 检查当前登录的用户的分组权限 /// - /// 模块 /// 权限名 /// - public async Task CheckPermissionAsync(string module, string permission) + public async Task CheckPermissionAsync(string permission) { //默认Admin角色拥有所有权限 if (CurrentUser.IsInGroup(LinConsts.Group.Admin)) return true; @@ -67,7 +39,7 @@ public async Task CheckPermissionAsync(string module, string permission) LinPermission linPermission = await permissionRepository.Where(r => r.PermissionType == PermissionType.Permission && r.Name == permission).FirstAsync(); - if (linPermission == null || groups == null || groups.Length == 0) return false; + if (linPermission == null || groups.Length == 0) return false; bool existPermission = await groupPermissionRepository.Select .AnyAsync(r => groups.Contains(r.GroupId) && r.PermissionId == linPermission.Id); @@ -135,6 +107,23 @@ public List> StructuringPermissions(List(createUpdateDto)); + } + + public async Task DeleteAsync(int id) + { + await permissionRepository.DeleteAsync(id); + } + public Task GetAsync(string permissionName) { return permissionRepository.Where(r => r.Name == permissionName).FirstAsync(); diff --git a/src/LinCms.Application/Cms/Permissions/TreeBuilder.cs b/src/LinCms.Application/Cms/Permissions/TreeBuilder.cs new file mode 100644 index 00000000..398e34b1 --- /dev/null +++ b/src/LinCms.Application/Cms/Permissions/TreeBuilder.cs @@ -0,0 +1,51 @@ +using System.Collections.Generic; +using System.Linq; + +namespace LinCms.Cms.Permissions; + +public record TreeBuilder +{ + public List BuildPermissionTree(List nodes) + { + var nodeDict = nodes.ToDictionary(n => n.Id); + List roots = new List(); + + foreach (var node in nodes) + { + if (node.ParentId == 0) // 假定ParentId为0表示根节点 + { + roots.Add(node); + } + else + { + if (nodeDict.TryGetValue(node.ParentId, out PermissionTreeNode parentNode)) + { + parentNode.Children.Add(node); + } + } + } + return roots; // 返回森林中所有根节点的列表 + } + + public List BuildTree(List nodes) + { + var nodeDict = nodes.ToDictionary(n => n.Id); + List roots = new List(); + + foreach (var node in nodes) + { + if (node.ParentId == 0) // 假定ParentId为0表示根节点 + { + roots.Add(node); + } + else + { + if (nodeDict.TryGetValue(node.ParentId, out TreeNode parentNode)) + { + parentNode.Children.Add(node); + } + } + } + return roots; // 返回森林中所有根节点的列表 + } +} diff --git a/src/LinCms.Application/LinCms.Application.csproj b/src/LinCms.Application/LinCms.Application.csproj index 4a7922d9..0305f73e 100644 --- a/src/LinCms.Application/LinCms.Application.csproj +++ b/src/LinCms.Application/LinCms.Application.csproj @@ -36,8 +36,5 @@ - - - diff --git a/src/LinCms.Core/Entities/LinPermission.cs b/src/LinCms.Core/Entities/LinPermission.cs index 612d297b..23d29dbb 100644 --- a/src/LinCms.Core/Entities/LinPermission.cs +++ b/src/LinCms.Core/Entities/LinPermission.cs @@ -39,6 +39,12 @@ public LinPermission(string name, PermissionType permissionType, string router) /// [Column(StringLength = 200)] public string Router { get; set; } + + /// + /// 排序 + /// + public int SortCode { get; set; } + } public enum PermissionType diff --git a/src/LinCms.Core/LinCms.Core.xml b/src/LinCms.Core/LinCms.Core.xml index 85966f6d..919ab10d 100644 --- a/src/LinCms.Core/LinCms.Core.xml +++ b/src/LinCms.Core/LinCms.Core.xml @@ -1062,6 +1062,11 @@ 后台路由 + + + 排序 + + 用户 diff --git a/src/LinCms.Web/Controllers/Cms/PermissionController.cs b/src/LinCms.Web/Controllers/Cms/PermissionController.cs index d8df9d1f..34df7f91 100644 --- a/src/LinCms.Web/Controllers/Cms/PermissionController.cs +++ b/src/LinCms.Web/Controllers/Cms/PermissionController.cs @@ -15,13 +15,13 @@ namespace LinCms.Controllers.Cms; [Route("cms/admin/permission")] [ApiController] public class PermissionController(IPermissionService permissionService) : ControllerBase -{ +{ /// - /// 查询所有可分配的权限 - /// - /// + /// 查询所有可分配的权限 + /// + /// [HttpGet("tree")] - [LinCmsAuthorize("查询所有可分配的权限", "管理员")] + [LinCmsAuthorize("分配权限", "管理员")] public async Task> GetPermissionTreeNodes() { return await permissionService.GetPermissionTreeNodes(); @@ -33,7 +33,7 @@ public async Task> GetPermissionTreeNodes() /// /// [HttpPost("remove")] - [LinCmsAuthorize("删除多个权限", "管理员")] + [LinCmsAuthorize("分配权限", "管理员")] public async Task RemovePermissions(RemovePermissionDto permissionDto) { await permissionService.DeletePermissionsAsync(permissionDto); @@ -46,11 +46,36 @@ public async Task RemovePermissions(RemovePermissionDto permis /// /// [HttpPost("dispatch/batch")] - [LinCmsAuthorize("分配多个权限", "管理员")] + [LinCmsAuthorize("分配权限", "管理员")] public async Task DispatchPermissions(DispatchPermissionsDto permissionDto) { List permissionDefinitions = ReflexHelper.GetAssemblyLinCmsAttributes(); await permissionService.DispatchPermissions(permissionDto, permissionDefinitions); return UnifyResponseDto.Success("添加权限成功"); } + + + [HttpPost] + [LinCmsAuthorize("权限管理", "管理员")] + public async Task CreateAsync([FromBody] PermissioCreateUpdateDto permissionDto) + { + await permissionService.CreateAsync(permissionDto); + return UnifyResponseDto.Success("新增权限成功"); + } + + [HttpPut("{id}")] + [LinCmsAuthorize("权限管理", "管理员")] + public async Task UpdateAsync(int id, [FromBody] PermissioCreateUpdateDto permissionDto) + { + await permissionService.UpdateAsync(id, permissionDto); + return UnifyResponseDto.Success("修改权限成功"); + } + + [HttpDelete("{id}")] + [LinCmsAuthorize("权限管理", "管理员")] + public async Task UpdateAsync(int id) + { + await permissionService.DeleteAsync(id); + return UnifyResponseDto.Success("删除权限成功"); + } } \ No newline at end of file diff --git a/src/LinCms.Web/Data/Authorization/PermissionAuthorizationHandler.cs b/src/LinCms.Web/Data/Authorization/PermissionAuthorizationHandler.cs index 1c3f6a3b..653ec2ee 100644 --- a/src/LinCms.Web/Data/Authorization/PermissionAuthorizationHandler.cs +++ b/src/LinCms.Web/Data/Authorization/PermissionAuthorizationHandler.cs @@ -21,7 +21,7 @@ protected override async Task HandleRequirementAsync(AuthorizationHandlerContext return; } - if (await permissionService.CheckPermissionAsync(requirement.Module, requirement.Name)) + if (await permissionService.CheckPermissionAsync(requirement.Name)) { context.Succeed(requirement); return;