From e8862af3a6e32f9d11f86da1b84e854257973673 Mon Sep 17 00:00:00 2001 From: IGeekFan Date: Fri, 24 May 2024 01:02:28 +0800 Subject: [PATCH] permission to tree permission add permissiontype --- .../Cms/Admins/IAdminService.cs | 9 --- .../Cms/Permissions/IPermissionService.cs | 4 +- .../Cms/Permissions/PermissionDto.cs | 43 ++++++++--- .../LinCms.Application.Contracts.xml | 5 ++ .../Cms/Admin/AdminService.cs | 23 ------ .../Cms/Permissions/PermissionProfile.cs | 2 + .../Cms/Permissions/PermissionService.cs | 75 +++++++++---------- .../LinCms.Application.csproj | 4 + src/LinCms.Core/Entities/LinPermission.cs | 31 ++++---- src/LinCms.Core/LinCms.Core.xml | 8 +- .../FreeSql/DataSeedContributor.cs | 41 +++++++++- .../Controllers/Cms/AdminController.cs | 4 +- .../Controllers/Cms/PermissionController.cs | 20 ++--- src/LinCms.Web/LinCms.Web.xml | 2 +- src/LinCms.Web/Utils/ReflexHelper.cs | 2 +- 15 files changed, 144 insertions(+), 129 deletions(-) delete mode 100644 src/LinCms.Application.Contracts/Cms/Admins/IAdminService.cs delete mode 100644 src/LinCms.Application/Cms/Admin/AdminService.cs diff --git a/src/LinCms.Application.Contracts/Cms/Admins/IAdminService.cs b/src/LinCms.Application.Contracts/Cms/Admins/IAdminService.cs deleted file mode 100644 index 23fa41ba..00000000 --- a/src/LinCms.Application.Contracts/Cms/Admins/IAdminService.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Collections.Generic; -using LinCms.Cms.Permissions; - -namespace LinCms.Cms.Admins; - -public interface IAdminService -{ - IDictionary> GetAllStructualPermissions(); -} \ No newline at end of file diff --git a/src/LinCms.Application.Contracts/Cms/Permissions/IPermissionService.cs b/src/LinCms.Application.Contracts/Cms/Permissions/IPermissionService.cs index 2ed05146..3ebe4a49 100644 --- a/src/LinCms.Application.Contracts/Cms/Permissions/IPermissionService.cs +++ b/src/LinCms.Application.Contracts/Cms/Permissions/IPermissionService.cs @@ -7,7 +7,7 @@ namespace LinCms.Cms.Permissions; public interface IPermissionService { - IDictionary> GetAllStructualPermissions(); + Task> GetPermissionTreeNodes(); Task CheckPermissionAsync(string module, string permission); Task DeletePermissionsAsync(RemovePermissionDto permissionDto); @@ -19,6 +19,4 @@ public interface IPermissionService Task GetAsync(string permissionName); - Task> GetTreePermissionListAsync(); - } \ 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 5ca66b62..468fe1e8 100644 --- a/src/LinCms.Application.Contracts/Cms/Permissions/PermissionDto.cs +++ b/src/LinCms.Application.Contracts/Cms/Permissions/PermissionDto.cs @@ -1,30 +1,49 @@ using System; using System.Collections.Generic; using IGeekFan.FreeKit.Extras.AuditEntity; +using LinCms.Entities; namespace LinCms.Cms.Permissions; public class PermissionDto : EntityDto { - public PermissionDto(string name, string module, string router) - { - Name = name; - Module = module; - Router = router; - } - public string Name { get; set; } - public string Module { get; set; } + + public PermissionType PermissionType { get; set; } + + /// + /// 父级Id + /// + public long ParentId { get; set; } + public string Router { get; set; } } -public class TreePermissionDto +public class PermissionTreeNode : PermissionNode { - public string Rowkey { get; set; } - public string Name { get; set; } public string Router { get; set; } + public DateTime? CreateTime { get; set; } - public List Children { get; set; } + public List Children { get; set; } + + public PermissionTreeNode() + { + Children = new List(); + } +} + +public class PermissionNode +{ + 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 PermissionNode() + { + 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 471fd3f5..e82abdeb 100644 --- a/src/LinCms.Application.Contracts/LinCms.Application.Contracts.xml +++ b/src/LinCms.Application.Contracts/LinCms.Application.Contracts.xml @@ -780,6 +780,11 @@ 新增用户数(月) + + + 父级Id + + 键 diff --git a/src/LinCms.Application/Cms/Admin/AdminService.cs b/src/LinCms.Application/Cms/Admin/AdminService.cs deleted file mode 100644 index 6a1f3159..00000000 --- a/src/LinCms.Application/Cms/Admin/AdminService.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using IGeekFan.FreeKit.Extras.FreeSql; -using LinCms.Cms.Admins; -using LinCms.Cms.Permissions; -using LinCms.Entities; - -namespace LinCms.Cms.Admin; - -public class AdminService(IAuditBaseRepository permissionRepository) : ApplicationService, IAdminService -{ - public IDictionary> GetAllStructualPermissions() - { - return permissionRepository.Select.ToList() - .GroupBy(r => r.Module) - .ToDictionary( - group => group.Key, - group => - Mapper.Map>(group.ToList()) - ); - - } -} \ No newline at end of file diff --git a/src/LinCms.Application/Cms/Permissions/PermissionProfile.cs b/src/LinCms.Application/Cms/Permissions/PermissionProfile.cs index 6bc8f12e..2cae15e2 100644 --- a/src/LinCms.Application/Cms/Permissions/PermissionProfile.cs +++ b/src/LinCms.Application/Cms/Permissions/PermissionProfile.cs @@ -8,5 +8,7 @@ public class PermissionProfile : Profile public PermissionProfile() { 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 9ec53c8a..4024242c 100644 --- a/src/LinCms.Application/Cms/Permissions/PermissionService.cs +++ b/src/LinCms.Application/Cms/Permissions/PermissionService.cs @@ -12,24 +12,47 @@ 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 { private readonly ICurrentUser _currentUser = currentUser; - public IDictionary> GetAllStructualPermissions() + public async Task> GetPermissionTreeNodes() { - return permissionRepository.Select.ToList() - .GroupBy(r => r.Module) - .ToDictionary( - group => group.Key, - group => - Mapper.Map>(group.ToList()) - ); - + var permissionList = await permissionRepository.Select.ToListAsync(); + var nodes = Mapper.Map, List>(permissionList); + return new PermissionTreeBuilder().BuildTree(nodes); } + /// /// 检查当前登录的用户的分组权限 /// @@ -42,7 +65,7 @@ public async Task CheckPermissionAsync(string module, string permission) if (CurrentUser.IsInGroup(LinConsts.Group.Admin)) return true; long[] groups = CurrentUser.FindGroupIds().Select(long.Parse).ToArray(); - LinPermission linPermission = await permissionRepository.Where(r => r.Module == module && r.Name == permission).FirstAsync(); + LinPermission linPermission = await permissionRepository.Where(r => r.PermissionType == PermissionType.Permission && r.Name == permission).FirstAsync(); if (linPermission == null || groups == null || groups.Length == 0) return false; @@ -85,7 +108,7 @@ public async Task> GetPermissionByGroupIds(List groupI public List> StructuringPermissions(List permissions) { - var groupPermissions = permissions.GroupBy(r => r.Module).Select(r => new + var groupPermissions = permissions.Where(r => r.PermissionType == PermissionType.Folder).GroupBy(r => r.Name).Select(r => new { r.Key, Children = r.Select(u => u.Name).ToList() @@ -116,34 +139,4 @@ public Task GetAsync(string permissionName) { return permissionRepository.Where(r => r.Name == permissionName).FirstAsync(); } - - public async Task> GetTreePermissionListAsync() - { - var permissions = await permissionRepository.Select.ToListAsync(); - - List treePermissionDtos = permissions.GroupBy(r => r.Module).Select(r => - new TreePermissionDto - { - Rowkey = Guid.NewGuid().ToString(), - Children = new List(), - Name = r.Key, - }).ToList(); - - - foreach (var permission in treePermissionDtos) - { - var childrens = permissions.Where(u => u.Module == permission.Name) - .Select(r => new TreePermissionDto - { - Rowkey = r.Id.ToString(), - Name = r.Name, - Router = r.Router, - CreateTime = r.CreateTime - }) - .ToList(); - permission.Children = childrens; - } - - return treePermissionDtos; - } } \ No newline at end of file diff --git a/src/LinCms.Application/LinCms.Application.csproj b/src/LinCms.Application/LinCms.Application.csproj index 254772ed..4a7922d9 100644 --- a/src/LinCms.Application/LinCms.Application.csproj +++ b/src/LinCms.Application/LinCms.Application.csproj @@ -36,4 +36,8 @@ + + + + diff --git a/src/LinCms.Core/Entities/LinPermission.cs b/src/LinCms.Core/Entities/LinPermission.cs index 930844d3..612d297b 100644 --- a/src/LinCms.Core/Entities/LinPermission.cs +++ b/src/LinCms.Core/Entities/LinPermission.cs @@ -14,35 +14,36 @@ public LinPermission() { } - public LinPermission(string name, string module, string router) + public LinPermission(string name, PermissionType permissionType, string router) { - Name = name ?? throw new ArgumentNullException(nameof(name)); - Module = module ?? throw new ArgumentNullException(nameof(module)); - Router = router ?? throw new ArgumentNullException(nameof(router)); + Name = name; + PermissionType = permissionType; + Router = router; } + public PermissionType PermissionType { get; set; } + /// - /// 所属权限、权限名称,例如:访问首页 + /// 父级Id /// - [Column(StringLength = 60)] - public string Name { get; set; } + public long ParentId { get; set; } /// - /// 权限所属模块,例如:人员管理 + /// 所属权限、权限名称,例如:访问首页 /// - [Column(StringLength = 50)] - public string Module { get; set; } + [Column(StringLength = 60)] + public string Name { get; set; } /// /// 后台路由 /// [Column(StringLength = 200)] public string Router { get; set; } - - public override string ToString() - { - return base.ToString() + $" Auth:{Name}、Module:{Module}"; - } } + public enum PermissionType + { + Folder = 0, + Permission = 1 + } } diff --git a/src/LinCms.Core/LinCms.Core.xml b/src/LinCms.Core/LinCms.Core.xml index ab1055ad..85966f6d 100644 --- a/src/LinCms.Core/LinCms.Core.xml +++ b/src/LinCms.Core/LinCms.Core.xml @@ -1047,14 +1047,14 @@ 权限表 - + - 所属权限、权限名称,例如:访问首页 + 父级Id - + - 权限所属模块,例如:人员管理 + 所属权限、权限名称,例如:访问首页 diff --git a/src/LinCms.Infrastructure/FreeSql/DataSeedContributor.cs b/src/LinCms.Infrastructure/FreeSql/DataSeedContributor.cs index c40f6582..971740cf 100644 --- a/src/LinCms.Infrastructure/FreeSql/DataSeedContributor.cs +++ b/src/LinCms.Infrastructure/FreeSql/DataSeedContributor.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Reflection.PortableExecutable; using System.Threading; using System.Threading.Tasks; using IGeekFan.FreeKit.Extras.FreeSql; @@ -47,7 +48,7 @@ public async Task SeedPermissionAsync(List linCmsAttribute List insertPermissions = new(); List updatePermissions = new(); - List allPermissions = await _permissionRepository.Select.ToListAsync(cancellationToken); + List allPermissions = await _permissionRepository.Select.Where(r => r.PermissionType == PermissionType.Permission).ToListAsync(cancellationToken); Expression> expression = u => false; Expression> permissionExpression = u => false; @@ -64,19 +65,51 @@ public async Task SeedPermissionAsync(List linCmsAttribute effectRows += await _groupPermissionRepository.DeleteAsync(expression, cancellationToken); _logger.LogInformation($"删除了{effectRows}条数据"); + + #region Module 目录 + var allModules = await _permissionRepository.Select.Where(r => r.PermissionType == PermissionType.Folder).ToListAsync(cancellationToken); + + var permissionDefinitionsByModules = linCmsAttributes.GroupBy(r => r.Module).ToList(); + + var insertMoudles = new List(); + foreach (var module in permissionDefinitionsByModules) + { + LinPermission permissionEntity = allModules.FirstOrDefault(u => u.Name == module.Key); + if (permissionEntity == null) + { + insertMoudles.Add(new LinPermission() + { + PermissionType = PermissionType.Folder, + Name = module.Key, + ParentId = 0 + }); + } + } + await _permissionRepository.InsertAsync(insertMoudles, cancellationToken); + #endregion + + allModules = await _permissionRepository.Select.Where(r => r.PermissionType == PermissionType.Folder).ToListAsync(cancellationToken); + linCmsAttributes.ForEach(r => { - LinPermission permissionEntity = allPermissions.FirstOrDefault(u => u.Module == r.Module && u.Name == r.Permission); + LinPermission permissionEntity = allPermissions.FirstOrDefault(u => u.Name == r.Permission); + + var parentId = allModules.Where(u => u.Name == r.Module).First().Id; if (permissionEntity == null) { - insertPermissions.Add(new LinPermission(r.Permission, r.Module, r.Router)); + insertPermissions.Add(new LinPermission(r.Permission, PermissionType.Permission, r.Router) + { + ParentId = parentId + }); } else { - bool routerExist = allPermissions.Any(u => u.Module == r.Module && u.Name == r.Permission && u.Router == r.Router); + bool routerExist = allPermissions.Any(u => u.Name == r.Permission && u.Router == r.Router); if (!routerExist) { permissionEntity.Router = r.Router; + permissionEntity.ParentId = parentId; + permissionEntity.PermissionType = PermissionType.Permission; updatePermissions.Add(permissionEntity); } } diff --git a/src/LinCms.Web/Controllers/Cms/AdminController.cs b/src/LinCms.Web/Controllers/Cms/AdminController.cs index b7bb2ba2..632eac62 100644 --- a/src/LinCms.Web/Controllers/Cms/AdminController.cs +++ b/src/LinCms.Web/Controllers/Cms/AdminController.cs @@ -14,10 +14,8 @@ namespace LinCms.Controllers.Cms; [ApiExplorerSettings(GroupName = "cms")] [Route("cms/admin")] [ApiController] -public class AdminController(IUserService userSevice, IAdminService adminService) : ControllerBase +public class AdminController(IUserService userSevice) : ControllerBase { - private readonly IAdminService _adminService = adminService; - /// /// 用户信息分页列表项 /// diff --git a/src/LinCms.Web/Controllers/Cms/PermissionController.cs b/src/LinCms.Web/Controllers/Cms/PermissionController.cs index 10c51b09..d8df9d1f 100644 --- a/src/LinCms.Web/Controllers/Cms/PermissionController.cs +++ b/src/LinCms.Web/Controllers/Cms/PermissionController.cs @@ -15,16 +15,16 @@ namespace LinCms.Controllers.Cms; [Route("cms/admin/permission")] [ApiController] public class PermissionController(IPermissionService permissionService) : ControllerBase -{ +{ /// - /// 查询所有可分配的权限 - /// - /// - [HttpGet] + /// 查询所有可分配的权限 + /// + /// + [HttpGet("tree")] [LinCmsAuthorize("查询所有可分配的权限", "管理员")] - public IDictionary> GetAllPermissions() + public async Task> GetPermissionTreeNodes() { - return permissionService.GetAllStructualPermissions(); + return await permissionService.GetPermissionTreeNodes(); } /// @@ -53,10 +53,4 @@ public async Task DispatchPermissions(DispatchPermissionsDto p await permissionService.DispatchPermissions(permissionDto, permissionDefinitions); return UnifyResponseDto.Success("添加权限成功"); } - - [HttpGet("tree-list")] - public Task> GetTreePermissionListAsync() - { - return permissionService.GetTreePermissionListAsync(); - } } \ No newline at end of file diff --git a/src/LinCms.Web/LinCms.Web.xml b/src/LinCms.Web/LinCms.Web.xml index 0153709a..118777bf 100644 --- a/src/LinCms.Web/LinCms.Web.xml +++ b/src/LinCms.Web/LinCms.Web.xml @@ -632,7 +632,7 @@ 权限 - + 查询所有可分配的权限 diff --git a/src/LinCms.Web/Utils/ReflexHelper.cs b/src/LinCms.Web/Utils/ReflexHelper.cs index f8db8702..6af36332 100644 --- a/src/LinCms.Web/Utils/ReflexHelper.cs +++ b/src/LinCms.Web/Utils/ReflexHelper.cs @@ -172,7 +172,7 @@ public static dynamic AuthorizationConvertToTree(List list public static List> AuthsConvertToTree(List listAuths) { - var groupAuths = listAuths.GroupBy(r => r.Module).Select(r => new + var groupAuths = listAuths.Where(r=>r.PermissionType==PermissionType.Folder).GroupBy(r => r.Name).Select(r => new { r.Key, Children = r.Select(u => u.Name).ToList()