From 52554644ba3c42c74a61bbf2b24a395e47745411 Mon Sep 17 00:00:00 2001 From: stylefeng Date: Thu, 31 Oct 2024 00:41:03 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=908.3.1=E3=80=91=E3=80=90system=E3=80=91?= =?UTF-8?q?=E3=80=90role=E3=80=91=E4=B8=80=E4=BA=9B=E9=80=9A=E7=94=A8?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=9A=84=E6=8A=BD=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../factory/RoleBindPermissionFactory.java | 141 ++++++++++++++++++ .../role/factory/PermissionAssignFactory.java | 126 +--------------- .../impl/PermissionAssignServiceImpl.java | 5 +- .../service/impl/SysRoleLimitServiceImpl.java | 4 +- 4 files changed, 148 insertions(+), 128 deletions(-) create mode 100644 kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/factory/RoleBindPermissionFactory.java diff --git a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/factory/RoleBindPermissionFactory.java b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/factory/RoleBindPermissionFactory.java new file mode 100644 index 000000000..3b387d497 --- /dev/null +++ b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/factory/RoleBindPermissionFactory.java @@ -0,0 +1,141 @@ +package cn.stylefeng.roses.kernel.sys.api.factory; + +import cn.hutool.core.convert.Convert; +import cn.hutool.core.util.ObjectUtil; +import cn.stylefeng.roses.kernel.rule.tree.factory.DefaultTreeBuildFactory; +import cn.stylefeng.roses.kernel.sys.api.pojo.role.response.RoleBindPermissionItem; +import cn.stylefeng.roses.kernel.sys.api.pojo.role.response.RoleBindPermissionResponse; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +/** + * 权限分配相关的实体创建 + * + * @author fengshuonan + * @since 2023/6/13 16:30 + */ +public class RoleBindPermissionFactory { + + /** + * 组合成角色绑定权限需要的详情信息,一颗树形结构,选中状态都是未选中 + * + * @param apps 应用信息 + * @param menus 菜单信息 + * @param options 功能信息 + * @author fengshuonan + * @since 2023/6/13 17:43 + */ + public static RoleBindPermissionResponse composeSelectStructure(List apps, List menus, List options) { + + // 定义全选属性 + RoleBindPermissionResponse roleBindPermissionResponse = new RoleBindPermissionResponse(); + roleBindPermissionResponse.setChecked(false); + + // 【2024年10月30日】把菜单上的功能挂载到菜单上 + for (RoleBindPermissionItem menuItem : menus) { + for (RoleBindPermissionItem optionItem : options) { + if (Convert.toLong(optionItem.getNodeParentId()).equals(Convert.toLong(menuItem.getNodeId()))) { + List functionList = menuItem.getFunctionList(); + if (ObjectUtil.isEmpty(functionList)) { + functionList = new ArrayList<>(); + } + functionList.add(optionItem); + menuItem.setFunctionList(functionList); + } + } + } + + // 合并应用菜单和功能,并构建树形结构 + apps.addAll(menus); + + List roleBindPermissionItems = new DefaultTreeBuildFactory().doTreeBuild(apps); + roleBindPermissionResponse.setAppPermissionList(roleBindPermissionItems); + + return roleBindPermissionResponse; + } + + /** + * 将空状态的权限树,填充角色绑定的权限 + * + * @param roleBindPermissionResponse 空状态的角色权限树(未设置选中状态) + * @param rolePermissions 角色所拥有的菜单id和功能id的集合 + * @author fengshuonan + * @since 2023/6/13 19:00 + */ + public static RoleBindPermissionResponse fillCheckedFlag(RoleBindPermissionResponse roleBindPermissionResponse, Set rolePermissions) { + + List appList = roleBindPermissionResponse.getAppPermissionList(); + + // 开始填充菜单和功能的选中状态 + fillSubItemCheckedFlag(appList, rolePermissions); + + // 填充应用的选中状态 + for (RoleBindPermissionItem appItem : appList) { + fillParentCheckedFlag(appItem); + } + + // 填充全选的选中状态 + roleBindPermissionResponse.setChecked(true); + for (RoleBindPermissionItem appItem : appList) { + if (!appItem.getChecked()) { + roleBindPermissionResponse.setChecked(false); + } + } + + return roleBindPermissionResponse; + } + + /** + * 填充子节点的选中状态 + *

+ * 根据执行的角色权限参数匹配判断 + * + * @author fengshuonan + * @since 2023/6/13 19:21 + */ + private static void fillSubItemCheckedFlag(List beFilled, Set rolePermissionList) { + + if (ObjectUtil.isEmpty(beFilled) || ObjectUtil.isEmpty(rolePermissionList)) { + return; + } + + for (RoleBindPermissionItem roleBindPermissionItem : beFilled) { + if (rolePermissionList.contains(Convert.toLong(roleBindPermissionItem.getNodeId()))) { + roleBindPermissionItem.setChecked(true); + } + + fillSubItemCheckedFlag(roleBindPermissionItem.getChildren(), rolePermissionList); + } + } + + /** + * 填充父级的节点的选中状态 + *

+ * 如果所有子集都选中了,则选中所有的父级状态 + * + * @author fengshuonan + * @since 2023/6/13 19:25 + */ + private static void fillParentCheckedFlag(RoleBindPermissionItem appItem) { + + if (ObjectUtil.isEmpty(appItem)) { + return; + } + + appItem.setChecked(true); + for (RoleBindPermissionItem menuItem : appItem.getChildren()) { + if (!menuItem.getChecked()) { + appItem.setChecked(false); + } + + for (RoleBindPermissionItem options : menuItem.getChildren()) { + if (!options.getChecked()) { + appItem.setChecked(false); + } + } + } + } + +} diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/factory/PermissionAssignFactory.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/factory/PermissionAssignFactory.java index de0e35d46..80efedd06 100644 --- a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/factory/PermissionAssignFactory.java +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/factory/PermissionAssignFactory.java @@ -1,11 +1,8 @@ package cn.stylefeng.roses.kernel.sys.modular.role.factory; -import cn.hutool.core.convert.Convert; import cn.hutool.core.util.ObjectUtil; import cn.stylefeng.roses.kernel.rule.constants.TreeConstants; -import cn.stylefeng.roses.kernel.rule.tree.factory.DefaultTreeBuildFactory; import cn.stylefeng.roses.kernel.sys.api.pojo.role.response.RoleBindPermissionItem; -import cn.stylefeng.roses.kernel.sys.api.pojo.role.response.RoleBindPermissionResponse; import cn.stylefeng.roses.kernel.sys.modular.app.entity.SysApp; import cn.stylefeng.roses.kernel.sys.modular.menu.entity.SysMenu; import cn.stylefeng.roses.kernel.sys.modular.menu.entity.SysMenuOptions; @@ -54,7 +51,8 @@ public class PermissionAssignFactory { } // 转化结构 - RoleBindPermissionItem roleBindPermissionItem = new RoleBindPermissionItem(sysMenu.getMenuId(), sysMenu.getMenuParentId(), sysMenu.getMenuName(), PermissionNodeTypeEnum.MENU.getCode(), false); + RoleBindPermissionItem roleBindPermissionItem = new RoleBindPermissionItem(sysMenu.getMenuId(), sysMenu.getMenuParentId(), sysMenu.getMenuName(), PermissionNodeTypeEnum.MENU.getCode(), + false); // 判断是否是叶子节点 if (leafMenus.contains(sysMenu.getMenuId())) { @@ -117,124 +115,4 @@ public class PermissionAssignFactory { return optionsResult; } - /** - * 组合成角色绑定权限需要的详情信息,一颗树形结构,选中状态都是未选中 - * - * @param apps 应用信息 - * @param menus 菜单信息 - * @param options 功能信息 - * @author fengshuonan - * @since 2023/6/13 17:43 - */ - public static RoleBindPermissionResponse composeSelectStructure(List apps, List menus, List options) { - - // 定义全选属性 - RoleBindPermissionResponse roleBindPermissionResponse = new RoleBindPermissionResponse(); - roleBindPermissionResponse.setChecked(false); - - // 【2024年10月30日】把菜单上的功能挂载到菜单上 - for (RoleBindPermissionItem menuItem : menus) { - for (RoleBindPermissionItem optionItem : options) { - if (Convert.toLong(optionItem.getNodeParentId()).equals(Convert.toLong(menuItem.getNodeId()))) { - List functionList = menuItem.getFunctionList(); - if (ObjectUtil.isEmpty(functionList)) { - functionList = new ArrayList<>(); - } - functionList.add(optionItem); - menuItem.setFunctionList(functionList); - } - } - } - - // 合并应用菜单和功能,并构建树形结构 - apps.addAll(menus); - - List roleBindPermissionItems = new DefaultTreeBuildFactory().doTreeBuild(apps); - roleBindPermissionResponse.setAppPermissionList(roleBindPermissionItems); - - return roleBindPermissionResponse; - } - - /** - * 将空状态的权限树,填充角色绑定的权限 - * - * @param roleBindPermissionResponse 空状态的角色权限树(未设置选中状态) - * @param rolePermissions 角色所拥有的菜单id和功能id的集合 - * @author fengshuonan - * @since 2023/6/13 19:00 - */ - public static RoleBindPermissionResponse fillCheckedFlag(RoleBindPermissionResponse roleBindPermissionResponse, Set rolePermissions) { - - List appList = roleBindPermissionResponse.getAppPermissionList(); - - // 开始填充菜单和功能的选中状态 - fillSubItemCheckedFlag(appList, rolePermissions); - - // 填充应用的选中状态 - for (RoleBindPermissionItem appItem : appList) { - fillParentCheckedFlag(appItem); - } - - // 填充全选的选中状态 - roleBindPermissionResponse.setChecked(true); - for (RoleBindPermissionItem appItem : appList) { - if (!appItem.getChecked()) { - roleBindPermissionResponse.setChecked(false); - } - } - - return roleBindPermissionResponse; - } - - /** - * 填充子节点的选中状态 - *

- * 根据执行的角色权限参数匹配判断 - * - * @author fengshuonan - * @since 2023/6/13 19:21 - */ - private static void fillSubItemCheckedFlag(List beFilled, Set rolePermissionList) { - - if (ObjectUtil.isEmpty(beFilled) || ObjectUtil.isEmpty(rolePermissionList)) { - return; - } - - for (RoleBindPermissionItem roleBindPermissionItem : beFilled) { - if (rolePermissionList.contains(Convert.toLong(roleBindPermissionItem.getNodeId()))) { - roleBindPermissionItem.setChecked(true); - } - - fillSubItemCheckedFlag(roleBindPermissionItem.getChildren(), rolePermissionList); - } - } - - /** - * 填充父级的节点的选中状态 - *

- * 如果所有子集都选中了,则选中所有的父级状态 - * - * @author fengshuonan - * @since 2023/6/13 19:25 - */ - private static void fillParentCheckedFlag(RoleBindPermissionItem appItem) { - - if (ObjectUtil.isEmpty(appItem)) { - return; - } - - appItem.setChecked(true); - for (RoleBindPermissionItem menuItem : appItem.getChildren()) { - if (!menuItem.getChecked()) { - appItem.setChecked(false); - } - - for (RoleBindPermissionItem options : menuItem.getChildren()) { - if (!options.getChecked()) { - appItem.setChecked(false); - } - } - } - } - } diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/service/impl/PermissionAssignServiceImpl.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/service/impl/PermissionAssignServiceImpl.java index f4b4f2f29..b766f6ce2 100644 --- a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/service/impl/PermissionAssignServiceImpl.java +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/service/impl/PermissionAssignServiceImpl.java @@ -5,6 +5,7 @@ import cn.hutool.extra.spring.SpringUtil; import cn.stylefeng.roses.kernel.event.sdk.publish.BusinessEventPublisher; import cn.stylefeng.roses.kernel.log.api.util.BusinessLogUtil; import cn.stylefeng.roses.kernel.sys.api.SysUserRoleServiceApi; +import cn.stylefeng.roses.kernel.sys.api.factory.RoleBindPermissionFactory; import cn.stylefeng.roses.kernel.sys.api.pojo.role.request.RoleBindPermissionRequest; import cn.stylefeng.roses.kernel.sys.api.pojo.role.response.RoleBindPermissionItem; import cn.stylefeng.roses.kernel.sys.api.pojo.role.response.RoleBindPermissionResponse; @@ -72,7 +73,7 @@ public class PermissionAssignServiceImpl implements PermissionAssignService { Set roleBindMenusAndOptions = this.getRoleBindMenusAndOptions(roleBindPermissionRequest.getRoleId()); // 4. 组合结构树和角色绑定的信息,填充选择状态,封装返回结果 - return PermissionAssignFactory.fillCheckedFlag(selectTreeStructure, roleBindMenusAndOptions); + return RoleBindPermissionFactory.fillCheckedFlag(selectTreeStructure, roleBindMenusAndOptions); } @Override @@ -147,7 +148,7 @@ public class PermissionAssignServiceImpl implements PermissionAssignService { List totalResultOptions = PermissionAssignFactory.createMenuOptions(sysMenuOptionsList); // 将应用、菜单、功能组成返回结果 - return PermissionAssignFactory.composeSelectStructure(totalResultApps, totalResultMenus, totalResultOptions); + return RoleBindPermissionFactory.composeSelectStructure(totalResultApps, totalResultMenus, totalResultOptions); } @Override diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/service/impl/SysRoleLimitServiceImpl.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/service/impl/SysRoleLimitServiceImpl.java index 1675ad47a..ae0892f84 100644 --- a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/service/impl/SysRoleLimitServiceImpl.java +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/service/impl/SysRoleLimitServiceImpl.java @@ -3,12 +3,12 @@ package cn.stylefeng.roses.kernel.sys.modular.role.service.impl; import cn.hutool.core.collection.ListUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.extra.spring.SpringUtil; +import cn.stylefeng.roses.kernel.sys.api.factory.RoleBindPermissionFactory; import cn.stylefeng.roses.kernel.sys.api.pojo.role.request.RoleBindPermissionRequest; import cn.stylefeng.roses.kernel.sys.api.pojo.role.response.RoleBindPermissionResponse; import cn.stylefeng.roses.kernel.sys.modular.role.action.RoleBindLimitAction; import cn.stylefeng.roses.kernel.sys.modular.role.entity.SysRoleLimit; import cn.stylefeng.roses.kernel.sys.modular.role.enums.RoleLimitTypeEnum; -import cn.stylefeng.roses.kernel.sys.modular.role.factory.PermissionAssignFactory; import cn.stylefeng.roses.kernel.sys.modular.role.mapper.SysRoleLimitMapper; import cn.stylefeng.roses.kernel.sys.modular.role.service.PermissionAssignService; import cn.stylefeng.roses.kernel.sys.modular.role.service.SysRoleLimitService; @@ -43,7 +43,7 @@ public class SysRoleLimitServiceImpl extends ServiceImpl roleBindLimitList = this.getRoleBindLimitList(roleBindPermissionRequest.getRoleId()); // 3. 组合结构和角色绑定的限制信息,填充选择状态,封装返回结果 - return PermissionAssignFactory.fillCheckedFlag(selectTreeStructure, roleBindLimitList); + return RoleBindPermissionFactory.fillCheckedFlag(selectTreeStructure, roleBindLimitList); } @Override