【8.3.1】【system】【role】一些通用方法的抽离

master
stylefeng 2024-10-31 00:41:03 +08:00
parent a311ae31af
commit 52554644ba
4 changed files with 148 additions and 128 deletions

View File

@ -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<RoleBindPermissionItem> apps, List<RoleBindPermissionItem> menus, List<RoleBindPermissionItem> 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<RoleBindPermissionItem> functionList = menuItem.getFunctionList();
if (ObjectUtil.isEmpty(functionList)) {
functionList = new ArrayList<>();
}
functionList.add(optionItem);
menuItem.setFunctionList(functionList);
}
}
}
// 合并应用菜单和功能,并构建树形结构
apps.addAll(menus);
List<RoleBindPermissionItem> roleBindPermissionItems = new DefaultTreeBuildFactory<RoleBindPermissionItem>().doTreeBuild(apps);
roleBindPermissionResponse.setAppPermissionList(roleBindPermissionItems);
return roleBindPermissionResponse;
}
/**
*
*
* @param roleBindPermissionResponse
* @param rolePermissions idid
* @author fengshuonan
* @since 2023/6/13 19:00
*/
public static RoleBindPermissionResponse fillCheckedFlag(RoleBindPermissionResponse roleBindPermissionResponse, Set<Long> rolePermissions) {
List<RoleBindPermissionItem> 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;
}
/**
*
* <p>
*
*
* @author fengshuonan
* @since 2023/6/13 19:21
*/
private static void fillSubItemCheckedFlag(List<RoleBindPermissionItem> beFilled, Set<Long> 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);
}
}
/**
*
* <p>
*
*
* @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);
}
}
}
}
}

View File

@ -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<RoleBindPermissionItem> apps, List<RoleBindPermissionItem> menus, List<RoleBindPermissionItem> 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<RoleBindPermissionItem> functionList = menuItem.getFunctionList();
if (ObjectUtil.isEmpty(functionList)) {
functionList = new ArrayList<>();
}
functionList.add(optionItem);
menuItem.setFunctionList(functionList);
}
}
}
// 合并应用菜单和功能,并构建树形结构
apps.addAll(menus);
List<RoleBindPermissionItem> roleBindPermissionItems = new DefaultTreeBuildFactory<RoleBindPermissionItem>().doTreeBuild(apps);
roleBindPermissionResponse.setAppPermissionList(roleBindPermissionItems);
return roleBindPermissionResponse;
}
/**
*
*
* @param roleBindPermissionResponse
* @param rolePermissions idid
* @author fengshuonan
* @since 2023/6/13 19:00
*/
public static RoleBindPermissionResponse fillCheckedFlag(RoleBindPermissionResponse roleBindPermissionResponse, Set<Long> rolePermissions) {
List<RoleBindPermissionItem> 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;
}
/**
*
* <p>
*
*
* @author fengshuonan
* @since 2023/6/13 19:21
*/
private static void fillSubItemCheckedFlag(List<RoleBindPermissionItem> beFilled, Set<Long> 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);
}
}
/**
*
* <p>
*
*
* @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);
}
}
}
}
}

View File

@ -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<Long> 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<RoleBindPermissionItem> totalResultOptions = PermissionAssignFactory.createMenuOptions(sysMenuOptionsList);
// 将应用、菜单、功能组成返回结果
return PermissionAssignFactory.composeSelectStructure(totalResultApps, totalResultMenus, totalResultOptions);
return RoleBindPermissionFactory.composeSelectStructure(totalResultApps, totalResultMenus, totalResultOptions);
}
@Override

View File

@ -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<SysRoleLimitMapper, Sys
Set<Long> roleBindLimitList = this.getRoleBindLimitList(roleBindPermissionRequest.getRoleId());
// 3. 组合结构和角色绑定的限制信息,填充选择状态,封装返回结果
return PermissionAssignFactory.fillCheckedFlag(selectTreeStructure, roleBindLimitList);
return RoleBindPermissionFactory.fillCheckedFlag(selectTreeStructure, roleBindLimitList);
}
@Override