mirror of https://gitee.com/stylefeng/roses
【8.3.1】【system】【role】一些通用方法的抽离
parent
a311ae31af
commit
52554644ba
|
@ -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 角色所拥有的菜单id和功能id的集合
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,11 +1,8 @@
|
||||||
package cn.stylefeng.roses.kernel.sys.modular.role.factory;
|
package cn.stylefeng.roses.kernel.sys.modular.role.factory;
|
||||||
|
|
||||||
import cn.hutool.core.convert.Convert;
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.stylefeng.roses.kernel.rule.constants.TreeConstants;
|
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.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.app.entity.SysApp;
|
||||||
import cn.stylefeng.roses.kernel.sys.modular.menu.entity.SysMenu;
|
import cn.stylefeng.roses.kernel.sys.modular.menu.entity.SysMenu;
|
||||||
import cn.stylefeng.roses.kernel.sys.modular.menu.entity.SysMenuOptions;
|
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())) {
|
if (leafMenus.contains(sysMenu.getMenuId())) {
|
||||||
|
@ -117,124 +115,4 @@ public class PermissionAssignFactory {
|
||||||
return optionsResult;
|
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 角色所拥有的菜单id和功能id的集合
|
|
||||||
* @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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import cn.hutool.extra.spring.SpringUtil;
|
||||||
import cn.stylefeng.roses.kernel.event.sdk.publish.BusinessEventPublisher;
|
import cn.stylefeng.roses.kernel.event.sdk.publish.BusinessEventPublisher;
|
||||||
import cn.stylefeng.roses.kernel.log.api.util.BusinessLogUtil;
|
import cn.stylefeng.roses.kernel.log.api.util.BusinessLogUtil;
|
||||||
import cn.stylefeng.roses.kernel.sys.api.SysUserRoleServiceApi;
|
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.request.RoleBindPermissionRequest;
|
||||||
import cn.stylefeng.roses.kernel.sys.api.pojo.role.response.RoleBindPermissionItem;
|
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.api.pojo.role.response.RoleBindPermissionResponse;
|
||||||
|
@ -72,7 +73,7 @@ public class PermissionAssignServiceImpl implements PermissionAssignService {
|
||||||
Set<Long> roleBindMenusAndOptions = this.getRoleBindMenusAndOptions(roleBindPermissionRequest.getRoleId());
|
Set<Long> roleBindMenusAndOptions = this.getRoleBindMenusAndOptions(roleBindPermissionRequest.getRoleId());
|
||||||
|
|
||||||
// 4. 组合结构树和角色绑定的信息,填充选择状态,封装返回结果
|
// 4. 组合结构树和角色绑定的信息,填充选择状态,封装返回结果
|
||||||
return PermissionAssignFactory.fillCheckedFlag(selectTreeStructure, roleBindMenusAndOptions);
|
return RoleBindPermissionFactory.fillCheckedFlag(selectTreeStructure, roleBindMenusAndOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -147,7 +148,7 @@ public class PermissionAssignServiceImpl implements PermissionAssignService {
|
||||||
List<RoleBindPermissionItem> totalResultOptions = PermissionAssignFactory.createMenuOptions(sysMenuOptionsList);
|
List<RoleBindPermissionItem> totalResultOptions = PermissionAssignFactory.createMenuOptions(sysMenuOptionsList);
|
||||||
|
|
||||||
// 将应用、菜单、功能组成返回结果
|
// 将应用、菜单、功能组成返回结果
|
||||||
return PermissionAssignFactory.composeSelectStructure(totalResultApps, totalResultMenus, totalResultOptions);
|
return RoleBindPermissionFactory.composeSelectStructure(totalResultApps, totalResultMenus, totalResultOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -3,12 +3,12 @@ package cn.stylefeng.roses.kernel.sys.modular.role.service.impl;
|
||||||
import cn.hutool.core.collection.ListUtil;
|
import cn.hutool.core.collection.ListUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.extra.spring.SpringUtil;
|
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.request.RoleBindPermissionRequest;
|
||||||
import cn.stylefeng.roses.kernel.sys.api.pojo.role.response.RoleBindPermissionResponse;
|
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.action.RoleBindLimitAction;
|
||||||
import cn.stylefeng.roses.kernel.sys.modular.role.entity.SysRoleLimit;
|
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.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.mapper.SysRoleLimitMapper;
|
||||||
import cn.stylefeng.roses.kernel.sys.modular.role.service.PermissionAssignService;
|
import cn.stylefeng.roses.kernel.sys.modular.role.service.PermissionAssignService;
|
||||||
import cn.stylefeng.roses.kernel.sys.modular.role.service.SysRoleLimitService;
|
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());
|
Set<Long> roleBindLimitList = this.getRoleBindLimitList(roleBindPermissionRequest.getRoleId());
|
||||||
|
|
||||||
// 3. 组合结构和角色绑定的限制信息,填充选择状态,封装返回结果
|
// 3. 组合结构和角色绑定的限制信息,填充选择状态,封装返回结果
|
||||||
return PermissionAssignFactory.fillCheckedFlag(selectTreeStructure, roleBindLimitList);
|
return RoleBindPermissionFactory.fillCheckedFlag(selectTreeStructure, roleBindLimitList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue