From 874287a4c02d0a980d8b97c4a691b4f37ec5e812 Mon Sep 17 00:00:00 2001
From: houzhongjian <houzhongyi@126.com>
Date: 星期二, 03 十二月 2024 08:47:04 +0800
Subject: [PATCH] 1、security模块ExpressionUrlAuthorizationConfigurer改为AuthorizeHttpRequestsConfigurer 2、全局registry.antMatchers改为registry.requestMatchers 3、增加积木报表

---
 iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/permission/MenuServiceImpl.java |   77 +++++++++++++++++++++++++++++++++++++-
 1 files changed, 74 insertions(+), 3 deletions(-)

diff --git a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/permission/MenuServiceImpl.java b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/permission/MenuServiceImpl.java
index 1497663..ea51802 100644
--- a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/permission/MenuServiceImpl.java
+++ b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/permission/MenuServiceImpl.java
@@ -11,11 +11,14 @@
 import com.iailab.module.system.controller.admin.permission.vo.menu.MenuSaveVO;
 import com.iailab.module.system.controller.admin.tenant.vo.packages.TenantPackageSaveReqVO;
 import com.iailab.module.system.dal.dataobject.app.AppDO;
+import com.iailab.module.system.dal.dataobject.app.AppMenuDO;
 import com.iailab.module.system.dal.dataobject.permission.MenuDO;
 import com.iailab.module.system.dal.dataobject.permission.RoleDO;
 import com.iailab.module.system.dal.dataobject.permission.RoleMenuDO;
 import com.iailab.module.system.dal.dataobject.tenant.TenantDO;
 import com.iailab.module.system.dal.dataobject.tenant.TenantPackageDO;
+import com.iailab.module.system.dal.mysql.app.AppMapper;
+import com.iailab.module.system.dal.mysql.app.AppMenuMapper;
 import com.iailab.module.system.dal.mysql.permission.MenuMapper;
 import com.iailab.module.system.dal.mysql.permission.RoleMenuMapper;
 import com.iailab.module.system.dal.redis.RedisKeyConstants;
@@ -24,6 +27,7 @@
 import com.iailab.module.system.service.tenant.TenantPackageService;
 import com.iailab.module.system.service.tenant.TenantService;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.context.annotation.Lazy;
@@ -71,6 +75,10 @@
 
     @Resource
     private RoleMenuMapper roleMenuMapper;
+    @Autowired
+    private AppMapper appMapper;
+    @Autowired
+    private AppMenuMapper appMenuMapper;
 
     @Override
     @CacheEvict(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, key = "#createReqVO.permission",
@@ -118,8 +126,10 @@
         //菜单归属租户和应用
         Long tenantId = getTenantId();
         AppDO appDO = appService.getAppByTenantId(tenantId);
-        updateObj.setTenantId(tenantId);
-        updateObj.setAppId(appDO.getId());
+        if(appDO.getTenantId() != 1) {
+            updateObj.setTenantId(tenantId);
+            updateObj.setAppId(appDO.getId());
+        }
         menuMapper.updateById(updateObj);
     }
 
@@ -186,6 +196,37 @@
         return enabledMenus;
     }
 
+    @Override
+    public List<MenuDO> filterMenus(List<MenuDO> menuList, String type) {
+        if (CollUtil.isEmpty(menuList)){
+            return Collections.emptyList();
+        }
+        Map<Long, MenuDO> menuMap = convertMap(menuList, MenuDO::getId);
+        LambdaQueryWrapper<AppDO> queryWrapper = new LambdaQueryWrapper<>();
+
+        //查询所有的系统应用菜单
+        if("system".equals(type)) {
+            queryWrapper.eq(AppDO::getType, 0);
+        } else if("app".equals(type)) {
+            queryWrapper.eq(AppDO::getType, 1);
+        }
+        List<AppDO> appDOS = appMapper.selectList(queryWrapper);
+        List<Long> appIds = appDOS.stream().map(AppDO::getId).collect(Collectors.toList());
+        List<MenuDO> menuDOS = menuMapper.selectList(new LambdaQueryWrapper<MenuDO>().in(MenuDO::getAppId, appIds));
+        List<Long> systemMenuIds = menuDOS.stream().map(MenuDO::getId).collect(Collectors.toList());
+
+        // 遍历 menu 菜单,查找不是禁用的菜单,添加到 系统菜单(应用菜单) 结果
+        List<MenuDO> systemMenus = new ArrayList<>();
+        Set<Long> appMenuCache = new HashSet<>(); // 存下递归搜索过被禁用的菜单,防止重复的搜索
+        for (MenuDO menu : menuList) {
+            if (isAppMenu(menu, menuMap, appMenuCache, systemMenuIds)) {
+                continue;
+            }
+            systemMenus.add(menu);
+        }
+        return systemMenus;
+    }
+
     private boolean isMenuDisabled(MenuDO node, Map<Long, MenuDO> menuMap, Set<Long> disabledMenuCache) {
         // 如果已经判定是禁用的节点,直接结束
         if (disabledMenuCache.contains(node.getId())) {
@@ -211,6 +252,31 @@
         return false;
     }
 
+    private boolean isAppMenu(MenuDO node, Map<Long, MenuDO> menuMap, Set<Long> menuCache, List<Long> systemMenuIds) {
+        // 如果已经判定是禁用的节点,直接结束
+        if (menuCache.contains(node.getId())) {
+            return true;
+        }
+
+        // 2. 遍历到 parentId 为根节点,则无需判断
+        Long parentId = node.getParentId();
+        if (ObjUtil.equal(parentId, ID_ROOT)) {
+            if (!systemMenuIds.contains(node.getId())) {
+                menuCache.add(node.getId());
+                return true;
+            }
+            return false;
+        }
+
+        // 3. 继续遍历 parent 节点
+        MenuDO parent = menuMap.get(parentId);
+        if (parent == null || isAppMenu(parent, menuMap, menuCache, systemMenuIds)) {
+            menuCache.add(node.getId());
+            return true;
+        }
+        return false;
+    }
+
     @Override
     public List<MenuDO> getMenuList(MenuListReqVO reqVO) {
         return menuMapper.selectList(reqVO);
@@ -218,8 +284,13 @@
 
     @Override
     public List<MenuDO> getAppMenuList(Long tenantId, MenuListReqVO reqVO) {
-        List<MenuDO> menuDOS = menuMapper.selectAppMenuList(tenantId, reqVO);
+        List<MenuDO> menuDOS = menuMapper.selectAppMenuList(reqVO);
+        menuDOS = filterMenus(menuDOS, "app");
         Set<Long> menuDOIds = menuDOS.stream().map(MenuDO::getId).collect(Collectors.toSet());
+        TenantDO tenant = tenantService.getTenant(tenantId);
+        TenantPackageDO tenantPackage = tenantPackageService.getTenantPackage(tenant.getPackageId());
+        Set<Long> tenantMenuIds = tenantPackage.getMenuIds();
+        menuDOS = menuDOS.stream().filter(menuDO -> tenantMenuIds.contains(menuDO.getId())).collect(Collectors.toList());
         // 获得角色列表
         Set<Long> roleIds = permissionService.getUserRoleIdListByUserId(getLoginUserId());
         List<RoleDO> roles = roleService.getRoleList(roleIds);

--
Gitblit v1.9.3