Commit 74efc603829346a85690dd2a097a2033fc2d13a2
Committed by
Gitee
Merge branch 'feature/db' of gitee.com:zhijiantianya/ruoyi-vue-pro into feature/db
Signed-off-by: dhb52 <dhb52@126.com>
Showing
4 changed files
with
59 additions
and
3 deletions
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java
| ... | ... | @@ -109,7 +109,7 @@ public class AuthController { |
| 109 | 109 | // 1.3 获得菜单列表 |
| 110 | 110 | Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId)); |
| 111 | 111 | List<MenuDO> menuList = menuService.getMenuList(menuIds); |
| 112 | - menuList.removeIf(menu -> !CommonStatusEnum.ENABLE.getStatus().equals(menu.getStatus())); // 移除禁用的菜单 | |
| 112 | + menuList = menuService.filterDisableMenus(menuList); | |
| 113 | 113 | |
| 114 | 114 | // 2. 拼接结果返回 |
| 115 | 115 | return success(AuthConvert.INSTANCE.convert(user, roles, menuList)); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/MenuController.java
| ... | ... | @@ -72,6 +72,7 @@ public class MenuController { |
| 72 | 72 | public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuList() { |
| 73 | 73 | List<MenuDO> list = menuService.getMenuListByTenant( |
| 74 | 74 | new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 75 | + list = menuService.filterDisableMenus(list); | |
| 75 | 76 | list.sort(Comparator.comparing(MenuDO::getSort)); |
| 76 | 77 | return success(BeanUtils.toBean(list, MenuSimpleRespVO.class)); |
| 77 | 78 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuService.java
| ... | ... | @@ -53,6 +53,14 @@ public interface MenuService { |
| 53 | 53 | List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO); |
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | + * 过滤掉关闭的菜单及其子菜单 | |
| 57 | + * | |
| 58 | + * @param list 菜单列表 | |
| 59 | + * @return 过滤后的菜单列表 | |
| 60 | + */ | |
| 61 | + List<MenuDO> filterDisableMenus(List<MenuDO> list); | |
| 62 | + | |
| 63 | + /** | |
| 56 | 64 | * 筛选菜单列表 |
| 57 | 65 | * |
| 58 | 66 | * @param reqVO 筛选条件请求 VO | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.permission; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.hutool.core.util.ObjUtil; | |
| 5 | +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | |
| 4 | 6 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 5 | 7 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; |
| 6 | 8 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuSaveVO; |
| ... | ... | @@ -19,11 +21,11 @@ import org.springframework.context.annotation.Lazy; |
| 19 | 21 | import org.springframework.stereotype.Service; |
| 20 | 22 | import org.springframework.transaction.annotation.Transactional; |
| 21 | 23 | |
| 22 | -import java.util.Collection; | |
| 23 | -import java.util.List; | |
| 24 | +import java.util.*; | |
| 24 | 25 | |
| 25 | 26 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 26 | 27 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| 28 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; | |
| 27 | 29 | import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT; |
| 28 | 30 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 29 | 31 | |
| ... | ... | @@ -106,6 +108,7 @@ public class MenuServiceImpl implements MenuService { |
| 106 | 108 | |
| 107 | 109 | @Override |
| 108 | 110 | public List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO) { |
| 111 | + // 查询所有菜单,并过滤掉关闭的节点 | |
| 109 | 112 | List<MenuDO> menus = getMenuList(reqVO); |
| 110 | 113 | // 开启多租户的情况下,需要过滤掉未开通的菜单 |
| 111 | 114 | tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId()))); |
| ... | ... | @@ -113,6 +116,50 @@ public class MenuServiceImpl implements MenuService { |
| 113 | 116 | } |
| 114 | 117 | |
| 115 | 118 | @Override |
| 119 | + public List<MenuDO> filterDisableMenus(List<MenuDO> menuList) { | |
| 120 | + if (CollUtil.isEmpty(menuList)){ | |
| 121 | + return Collections.emptyList(); | |
| 122 | + } | |
| 123 | + Map<Long, MenuDO> menuMap = convertMap(menuList, MenuDO::getId); | |
| 124 | + | |
| 125 | + // 遍历 menu 菜单,查找不是禁用的菜单,添加到 enabledMenus 结果 | |
| 126 | + List<MenuDO> enabledMenus = new ArrayList<>(); | |
| 127 | + Set<Long> disabledMenuCache = new HashSet<>(); // 存下递归搜索过被禁用的菜单,防止重复的搜索 | |
| 128 | + for (MenuDO menu : menuList) { | |
| 129 | + if (isMenuDisabled(menu, menuMap, disabledMenuCache)) { | |
| 130 | + continue; | |
| 131 | + } | |
| 132 | + enabledMenus.add(menu); | |
| 133 | + } | |
| 134 | + return enabledMenus; | |
| 135 | + } | |
| 136 | + | |
| 137 | + private boolean isMenuDisabled(MenuDO node, Map<Long, MenuDO> menuMap, Set<Long> disabledMenuCache) { | |
| 138 | + // 如果已经判定是禁用的节点,直接结束 | |
| 139 | + if (disabledMenuCache.contains(node.getId())) { | |
| 140 | + return true; | |
| 141 | + } | |
| 142 | + | |
| 143 | + // 1. 遍历到 parentId 为根节点,则无需判断 | |
| 144 | + Long parentId = node.getParentId(); | |
| 145 | + if (ObjUtil.equal(parentId, ID_ROOT)) { | |
| 146 | + if (CommonStatusEnum.isDisable(node.getStatus())) { | |
| 147 | + disabledMenuCache.add(node.getId()); | |
| 148 | + return true; | |
| 149 | + } | |
| 150 | + return false; | |
| 151 | + } | |
| 152 | + | |
| 153 | + // 2. 继续遍历 parent 节点 | |
| 154 | + MenuDO parent = menuMap.get(parentId); | |
| 155 | + if (parent == null || isMenuDisabled(parent, menuMap, disabledMenuCache)) { | |
| 156 | + disabledMenuCache.add(node.getId()); | |
| 157 | + return true; | |
| 158 | + } | |
| 159 | + return false; | |
| 160 | + } | |
| 161 | + | |
| 162 | + @Override | |
| 116 | 163 | public List<MenuDO> getMenuList(MenuListReqVO reqVO) { |
| 117 | 164 | return menuMapper.selectList(reqVO); |
| 118 | 165 | } | ... | ... |