mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:19:19 +08:00
fix: 完善权限菜单向下方继承向上可见
This commit is contained in:
+72
-86
@@ -83,6 +83,27 @@ public class CompanyMenuServiceImpl extends ServiceImpl<CompanyMenuMapper, Compa
|
||||
// 保存公司菜单
|
||||
this.save(companyMenu);
|
||||
|
||||
// 分配父菜单时同步展开子孙菜单关联
|
||||
List<Long> expandedMenuIds = menuService.expandMenuIdsDownward(
|
||||
List.of(createCompanyMenuDTO.getMenuId()));
|
||||
for (Long menuId : expandedMenuIds) {
|
||||
if (menuId.equals(createCompanyMenuDTO.getMenuId())) {
|
||||
continue;
|
||||
}
|
||||
LambdaQueryWrapper<CompanyMenuPO> childWrapper = new LambdaQueryWrapper<>();
|
||||
childWrapper.eq(CompanyMenuPO::getCompanyId, createCompanyMenuDTO.getCompanyId())
|
||||
.eq(CompanyMenuPO::getMenuId, menuId);
|
||||
if (this.count(childWrapper) == 0) {
|
||||
CompanyMenuPO childCompanyMenu = new CompanyMenuPO();
|
||||
childCompanyMenu.setCompanyId(createCompanyMenuDTO.getCompanyId());
|
||||
childCompanyMenu.setMenuId(menuId);
|
||||
childCompanyMenu.setStatus(1);
|
||||
childCompanyMenu.setCreateBy(SecurityUtils.getCurrentUserId());
|
||||
this.save(childCompanyMenu);
|
||||
}
|
||||
assignPermissionsForMenu(createCompanyMenuDTO.getCompanyId(), menuId);
|
||||
}
|
||||
|
||||
// 自动分配该菜单关联的权限给公司
|
||||
assignPermissionsForMenu(createCompanyMenuDTO.getCompanyId(), createCompanyMenuDTO.getMenuId());
|
||||
|
||||
@@ -92,37 +113,8 @@ public class CompanyMenuServiceImpl extends ServiceImpl<CompanyMenuMapper, Compa
|
||||
|
||||
@Override
|
||||
public List<MenuVO> getCompanyMenusForRoute(Long companyId) {
|
||||
// 查询公司所有菜单
|
||||
LambdaQueryWrapper<CompanyMenuPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CompanyMenuPO::getCompanyId, companyId)
|
||||
.eq(CompanyMenuPO::getStatus, 1); // 只查询启用的菜单
|
||||
|
||||
List<CompanyMenuPO> companyMenus = this.list(wrapper);
|
||||
|
||||
if (companyMenus.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 获取菜单ID列表
|
||||
List<Long> menuIds = companyMenus.stream()
|
||||
.map(CompanyMenuPO::getMenuId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 查询系统菜单信息(直接使用菜单表的字段,不再使用自定义字段)
|
||||
LambdaQueryWrapper<MenuPO> menuWrapper = new LambdaQueryWrapper<>();
|
||||
menuWrapper.in(MenuPO::getId, menuIds)
|
||||
.eq(MenuPO::getStatus, 1) // 只查询启用的菜单
|
||||
.orderByAsc(MenuPO::getOrder);
|
||||
|
||||
List<MenuPO> menus = menuService.list(menuWrapper);
|
||||
|
||||
return menus.stream()
|
||||
.map(menu -> {
|
||||
MenuVO menuVO = new MenuVO();
|
||||
BeanCopyUtils.copyProperties(menu, menuVO);
|
||||
return menuVO;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
List<Long> assignedMenuIds = getCompanyMenuIds(companyId);
|
||||
return menuService.getMenusForRoute(assignedMenuIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -148,41 +140,40 @@ public class CompanyMenuServiceImpl extends ServiceImpl<CompanyMenuMapper, Compa
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void batchUpdateCompanyMenus(Long companyId, List<Long> menuIds) {
|
||||
// 分配父菜单时向下展开所有子孙
|
||||
List<Long> expandedMenuIds = menuService.expandMenuIdsDownward(
|
||||
menuIds != null ? menuIds : List.of());
|
||||
|
||||
// 查询公司现有菜单
|
||||
LambdaQueryWrapper<CompanyMenuPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CompanyMenuPO::getCompanyId, companyId);
|
||||
List<CompanyMenuPO> existingMenus = this.list(wrapper);
|
||||
|
||||
// 构建现有菜单ID到CompanyMenuPO的映射
|
||||
|
||||
Map<Long, CompanyMenuPO> existingMenuMap = existingMenus.stream()
|
||||
.collect(Collectors.toMap(CompanyMenuPO::getMenuId, cm -> cm));
|
||||
|
||||
// 1. 处理新增的菜单(接收到的菜单ID在公司现有菜单中没有的)
|
||||
for (Long menuId : menuIds) {
|
||||
|
||||
// 1. 处理新增的菜单
|
||||
for (Long menuId : expandedMenuIds) {
|
||||
if (!existingMenuMap.containsKey(menuId)) {
|
||||
// 新增菜单
|
||||
CompanyMenuPO newCompanyMenu = new CompanyMenuPO();
|
||||
newCompanyMenu.setCompanyId(companyId);
|
||||
newCompanyMenu.setMenuId(menuId);
|
||||
newCompanyMenu.setStatus(1); // 启用状态
|
||||
newCompanyMenu.setStatus(1);
|
||||
this.save(newCompanyMenu);
|
||||
// 自动分配该菜单关联的权限给公司
|
||||
assignPermissionsForMenu(companyId, menuId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 2. 处理现有菜单的状态
|
||||
for (CompanyMenuPO existingMenu : existingMenus) {
|
||||
Long menuId = existingMenu.getMenuId();
|
||||
|
||||
if (menuIds.contains(menuId)) {
|
||||
// 接收到的菜单ID列表中有这个菜单,启用它
|
||||
|
||||
if (expandedMenuIds.contains(menuId)) {
|
||||
if (existingMenu.getStatus() == 0) {
|
||||
existingMenu.setStatus(1);
|
||||
this.updateById(existingMenu);
|
||||
}
|
||||
} else {
|
||||
// 接收到的菜单ID列表中没有这个菜单,停用它
|
||||
if (existingMenu.getStatus() == 1) {
|
||||
existingMenu.setStatus(0);
|
||||
this.updateById(existingMenu);
|
||||
@@ -242,56 +233,51 @@ public class CompanyMenuServiceImpl extends ServiceImpl<CompanyMenuMapper, Compa
|
||||
*/
|
||||
@Override
|
||||
public List<CompanyMenuVO> getCompanyMenuTree() {
|
||||
// 获取公司所有菜单列表
|
||||
List<CompanyMenuVO> menuList = getCompanyMenus();
|
||||
|
||||
// 构建树形结构
|
||||
return buildCompanyMenuTree(menuList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建公司菜单树
|
||||
*/
|
||||
private List<CompanyMenuVO> buildCompanyMenuTree(List<CompanyMenuVO> menuList) {
|
||||
// 按父ID分组
|
||||
Map<Long, List<CompanyMenuVO>> parentMap = menuList.stream()
|
||||
.collect(Collectors.groupingBy(menu -> menu.getParentId() != null ? menu.getParentId() : 0L));
|
||||
|
||||
// 递归构建树
|
||||
return buildCompanyMenuTreeRecursive(parentMap, 0L, new ArrayList<>(), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归构建公司菜单树形结构(带循环引用检测)
|
||||
*/
|
||||
private List<CompanyMenuVO> buildCompanyMenuTreeRecursive(Map<Long, List<CompanyMenuVO>> parentMap, Long parentId,
|
||||
List<Long> visitedIds, int depth) {
|
||||
// 防止无限递归,设置最大深度限制
|
||||
if (depth > 100) {
|
||||
throw new BusinessException("菜单层级过深,可能存在循环引用");
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
if (companyId == null) {
|
||||
throw new BusinessException("无法获取公司信息");
|
||||
}
|
||||
|
||||
List<CompanyMenuVO> children = parentMap.get(parentId);
|
||||
if (children == null) {
|
||||
List<Long> assignedMenuIds = getCompanyMenuIds(companyId);
|
||||
if (assignedMenuIds.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
for (CompanyMenuVO menu : children) {
|
||||
// 检查是否已经访问过该节点,防止循环引用
|
||||
if (visitedIds.contains(menu.getId())) {
|
||||
throw new BusinessException("检测到菜单循环引用,菜单ID: " + menu.getId());
|
||||
}
|
||||
// 向上可见 + 向下继承展示
|
||||
List<MenuVO> visibleTree = menuService.getVisibleMenuTree(assignedMenuIds);
|
||||
|
||||
// 创建新访问路径,包含当前节点
|
||||
List<Long> newVisitedIds = new ArrayList<>(visitedIds);
|
||||
newVisitedIds.add(menu.getId());
|
||||
LambdaQueryWrapper<CompanyMenuPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CompanyMenuPO::getCompanyId, companyId)
|
||||
.eq(CompanyMenuPO::getStatus, 1);
|
||||
Map<Long, CompanyMenuPO> companyMenuMap = this.list(wrapper).stream()
|
||||
.collect(Collectors.toMap(CompanyMenuPO::getMenuId, cm -> cm));
|
||||
|
||||
// 递归构建子菜单
|
||||
List<CompanyMenuVO> childList = buildCompanyMenuTreeRecursive(parentMap, menu.getId(), newVisitedIds, depth + 1);
|
||||
menu.setChildren(new ArrayList<MenuVO>(childList));
|
||||
return convertMenuTreeToCompanyMenuVO(visibleTree, companyMenuMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 MenuVO 树转为 CompanyMenuVO 树
|
||||
*/
|
||||
private List<CompanyMenuVO> convertMenuTreeToCompanyMenuVO(
|
||||
List<MenuVO> menuTree,
|
||||
Map<Long, CompanyMenuPO> companyMenuMap) {
|
||||
if (menuTree == null || menuTree.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
return children;
|
||||
List<CompanyMenuVO> result = new ArrayList<>();
|
||||
for (MenuVO menu : menuTree) {
|
||||
CompanyMenuVO companyMenuVO = new CompanyMenuVO();
|
||||
BeanCopyUtils.copyProperties(menu, companyMenuVO);
|
||||
CompanyMenuPO companyMenu = companyMenuMap.get(menu.getId());
|
||||
if (companyMenu != null) {
|
||||
companyMenuVO.setCompanyMenuId(companyMenu.getId());
|
||||
}
|
||||
if (menu.getChildren() != null && !menu.getChildren().isEmpty()) {
|
||||
companyMenuVO.setChildren(new ArrayList<>(convertMenuTreeToCompanyMenuVO(menu.getChildren(), companyMenuMap)));
|
||||
}
|
||||
result.add(companyMenuVO);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+29
-23
@@ -16,7 +16,9 @@ import top.xyquan.ecomdmbbackend.modules.sys.permission.model.vo.PermissionVO;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -36,14 +38,14 @@ public class CompanyPermissionServiceImpl extends ServiceImpl<CompanyPermissionM
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void batchAssignCompanyPermissions(BatchAssignCompanyPermissionDTO batchAssignDTO) {
|
||||
Long companyId = batchAssignDTO.getCompanyId();
|
||||
List<Long> permissionIds = batchAssignDTO.getPermissionIds();
|
||||
List<Long> expandedPermissionIds = expandPermissionIdsDownward(batchAssignDTO.getPermissionIds());
|
||||
|
||||
LambdaQueryWrapper<CompanyPermissionPO> deleteWrapper = new LambdaQueryWrapper<>();
|
||||
deleteWrapper.eq(CompanyPermissionPO::getCompanyId, companyId);
|
||||
this.remove(deleteWrapper);
|
||||
|
||||
List<CompanyPermissionPO> companyPermissions = new ArrayList<>();
|
||||
for (Long permissionId : permissionIds) {
|
||||
for (Long permissionId : expandedPermissionIds) {
|
||||
CompanyPermissionPO companyPermission = new CompanyPermissionPO();
|
||||
companyPermission.setCompanyId(companyId);
|
||||
companyPermission.setPermissionId(permissionId);
|
||||
@@ -55,7 +57,7 @@ public class CompanyPermissionServiceImpl extends ServiceImpl<CompanyPermissionM
|
||||
this.saveBatch(companyPermissions);
|
||||
}
|
||||
|
||||
log.info("批量分配公司权限完成,公司ID:{},权限数量:{}", companyId, permissionIds.size());
|
||||
log.info("批量分配公司权限完成,公司ID:{},权限数量:{}", companyId, expandedPermissionIds.size());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -92,26 +94,10 @@ public class CompanyPermissionServiceImpl extends ServiceImpl<CompanyPermissionM
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void batchUpdateCompanyPermissions(Long companyId, List<Long> permissionIds) {
|
||||
LambdaQueryWrapper<CompanyPermissionPO> deleteWrapper = new LambdaQueryWrapper<>();
|
||||
deleteWrapper.eq(CompanyPermissionPO::getCompanyId, companyId);
|
||||
this.remove(deleteWrapper);
|
||||
|
||||
if (permissionIds != null && !permissionIds.isEmpty()) {
|
||||
List<CompanyPermissionPO> companyPermissions = new ArrayList<>();
|
||||
for (Long permissionId : permissionIds) {
|
||||
CompanyPermissionPO companyPermission = new CompanyPermissionPO();
|
||||
companyPermission.setCompanyId(companyId);
|
||||
companyPermission.setPermissionId(permissionId);
|
||||
companyPermission.setCreateBy(currentUserContext.getCurrentUserId());
|
||||
companyPermissions.add(companyPermission);
|
||||
}
|
||||
if (!companyPermissions.isEmpty()) {
|
||||
this.saveBatch(companyPermissions);
|
||||
}
|
||||
}
|
||||
|
||||
log.info("批量更新公司权限完成,公司ID:{},权限数量:{}", companyId,
|
||||
permissionIds != null ? permissionIds.size() : 0);
|
||||
BatchAssignCompanyPermissionDTO batchAssignDTO = new BatchAssignCompanyPermissionDTO();
|
||||
batchAssignDTO.setCompanyId(companyId);
|
||||
batchAssignDTO.setPermissionIds(permissionIds);
|
||||
batchAssignCompanyPermissions(batchAssignDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -156,4 +142,24 @@ public class CompanyPermissionServiceImpl extends ServiceImpl<CompanyPermissionM
|
||||
.in(CompanyPermissionPO::getPermissionId, allIds);
|
||||
return this.count(wrapper) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分配父权限时向下展开所有子孙权限 ID
|
||||
*/
|
||||
private List<Long> expandPermissionIdsDownward(List<Long> permissionIds) {
|
||||
if (permissionIds == null || permissionIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
Set<Long> expanded = new LinkedHashSet<>();
|
||||
for (Long permissionId : permissionIds) {
|
||||
if (permissionId == null) {
|
||||
continue;
|
||||
}
|
||||
List<Long> childIds = companyPermissionMapper.selectChildPermissionIds(permissionId);
|
||||
if (childIds != null) {
|
||||
expanded.addAll(childIds);
|
||||
}
|
||||
}
|
||||
return new ArrayList<>(expanded);
|
||||
}
|
||||
}
|
||||
|
||||
+10
@@ -10,6 +10,7 @@ import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.position.menu.model.dto.BatchAssignPositionMenuDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.position.menu.model.vo.PositionMenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.position.menu.service.PositionMenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.menu.model.vo.MenuVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -91,4 +92,13 @@ public class PositionPermissionController {
|
||||
positionMenuService.batchUpdatePositionMenus(positionId, menuIds);
|
||||
return ApiResponse.success("职位菜单更新成功");
|
||||
}
|
||||
|
||||
@Operation(summary = "获取职位可见菜单树", description = "获取职位已分配菜单的可见树(向上可见父级,向下展示子孙)")
|
||||
@GetMapping("/menu-tree/{positionId}")
|
||||
@RequirePermission(value = "org:position-menu:assign", description = "获取职位可见菜单树", allowSuper = true)
|
||||
public ApiResponse<List<MenuVO>> getPositionVisibleMenuTree(
|
||||
@Parameter(description = "职位ID") @PathVariable Long positionId) {
|
||||
List<MenuVO> menuTree = positionMenuService.getPositionVisibleMenuTree(positionId);
|
||||
return ApiResponse.success("获取职位可见菜单树成功", menuTree);
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -75,4 +75,9 @@ public interface PositionMenuService {
|
||||
* @param menuIds 菜单ID列表
|
||||
*/
|
||||
void batchUpdatePositionMenus(Long positionId, List<Long> menuIds);
|
||||
|
||||
/**
|
||||
* 获取岗位可见菜单树(向上可见 + 向下继承展示)
|
||||
*/
|
||||
List<MenuVO> getPositionVisibleMenuTree(Long positionId);
|
||||
}
|
||||
|
||||
+18
-63
@@ -24,6 +24,7 @@ import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -61,16 +62,15 @@ public class PositionMenuServiceImpl extends ServiceImpl<PositionMenuMapper, Pos
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void batchAssignPositionMenus(BatchAssignPositionMenuDTO batchAssignDTO) {
|
||||
Long positionId = batchAssignDTO.getPositionId();
|
||||
List<Long> menuIds = batchAssignDTO.getMenuIds();
|
||||
// 分配父菜单时向下展开所有子孙
|
||||
List<Long> expandedMenuIds = menuService.expandMenuIdsDownward(batchAssignDTO.getMenuIds());
|
||||
|
||||
// 先删除该岗位的所有菜单关联
|
||||
LambdaQueryWrapper<PositionMenuPO> deleteWrapper = new LambdaQueryWrapper<>();
|
||||
deleteWrapper.eq(PositionMenuPO::getPositionId, positionId);
|
||||
this.remove(deleteWrapper);
|
||||
|
||||
// 批量插入新的菜单关联
|
||||
List<PositionMenuPO> positionMenus = new ArrayList<>();
|
||||
for (Long menuId : menuIds) {
|
||||
for (Long menuId : expandedMenuIds) {
|
||||
PositionMenuPO positionMenu = new PositionMenuPO();
|
||||
positionMenu.setPositionId(positionId);
|
||||
positionMenu.setMenuId(menuId);
|
||||
@@ -82,8 +82,7 @@ public class PositionMenuServiceImpl extends ServiceImpl<PositionMenuMapper, Pos
|
||||
this.saveBatch(positionMenus);
|
||||
}
|
||||
|
||||
// 自动分配每个菜单关联的权限给岗位
|
||||
for (Long menuId : menuIds) {
|
||||
for (Long menuId : expandedMenuIds) {
|
||||
assignPermissionsForPosition(positionId, menuId);
|
||||
}
|
||||
}
|
||||
@@ -235,11 +234,8 @@ public class PositionMenuServiceImpl extends ServiceImpl<PositionMenuMapper, Pos
|
||||
*/
|
||||
@Override
|
||||
public boolean hasPositionMenu(Long positionId, Long menuId) {
|
||||
LambdaQueryWrapper<PositionMenuPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(PositionMenuPO::getPositionId, positionId)
|
||||
.eq(PositionMenuPO::getMenuId, menuId);
|
||||
|
||||
return this.count(wrapper) > 0;
|
||||
List<Long> assignedMenuIds = getPositionMenuIds(positionId);
|
||||
return menuService.hasMenuAccess(assignedMenuIds, menuId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -249,34 +245,8 @@ public class PositionMenuServiceImpl extends ServiceImpl<PositionMenuMapper, Pos
|
||||
*/
|
||||
@Override
|
||||
public List<MenuVO> getPositionMenusForRoute(Long positionId) {
|
||||
// 使用现有的方法获取岗位菜单
|
||||
List<PositionMenuVO> positionMenus = this.getMenusByPositionId(positionId);
|
||||
|
||||
if (positionMenus.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 获取菜单ID列表
|
||||
List<Long> menuIds = positionMenus.stream()
|
||||
.map(PositionMenuVO::getMenuId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 查询系统菜单信息
|
||||
LambdaQueryWrapper<MenuPO> menuWrapper = new LambdaQueryWrapper<>();
|
||||
menuWrapper.in(MenuPO::getId, menuIds)
|
||||
.eq(MenuPO::getStatus, 1) // 只查询启用的菜单
|
||||
.orderByAsc(MenuPO::getOrder);
|
||||
|
||||
List<MenuPO> menus = menuService.list(menuWrapper);
|
||||
|
||||
// 转换为MenuVO
|
||||
return menus.stream()
|
||||
.map(menu -> {
|
||||
MenuVO menuVO = new MenuVO();
|
||||
BeanCopyUtils.copyProperties(menu, menuVO);
|
||||
return menuVO;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
List<Long> assignedMenuIds = getPositionMenuIds(positionId);
|
||||
return menuService.getMenusForRoute(assignedMenuIds);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -306,31 +276,16 @@ public class PositionMenuServiceImpl extends ServiceImpl<PositionMenuMapper, Pos
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void batchUpdatePositionMenus(Long positionId, List<Long> menuIds) {
|
||||
// 先删除该岗位的所有菜单关联
|
||||
LambdaQueryWrapper<PositionMenuPO> deleteWrapper = new LambdaQueryWrapper<>();
|
||||
deleteWrapper.eq(PositionMenuPO::getPositionId, positionId);
|
||||
this.remove(deleteWrapper);
|
||||
BatchAssignPositionMenuDTO batchAssignDTO = new BatchAssignPositionMenuDTO();
|
||||
batchAssignDTO.setPositionId(positionId);
|
||||
batchAssignDTO.setMenuIds(menuIds);
|
||||
batchAssignPositionMenus(batchAssignDTO);
|
||||
}
|
||||
|
||||
// 批量插入新的菜单关联
|
||||
if (menuIds != null && !menuIds.isEmpty()) {
|
||||
List<PositionMenuPO> positionMenus = new ArrayList<>();
|
||||
for (Long menuId : menuIds) {
|
||||
PositionMenuPO positionMenu = new PositionMenuPO();
|
||||
positionMenu.setPositionId(positionId);
|
||||
positionMenu.setMenuId(menuId);
|
||||
positionMenu.setCreateBy(currentUserContext.getCurrentUserId());
|
||||
positionMenus.add(positionMenu);
|
||||
}
|
||||
|
||||
if (!positionMenus.isEmpty()) {
|
||||
this.saveBatch(positionMenus);
|
||||
}
|
||||
|
||||
// 自动分配每个菜单关联的权限给岗位
|
||||
for (Long menuId : menuIds) {
|
||||
assignPermissionsForPosition(positionId, menuId);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public List<MenuVO> getPositionVisibleMenuTree(Long positionId) {
|
||||
List<Long> assignedMenuIds = getPositionMenuIds(positionId);
|
||||
return menuService.getVisibleMenuTree(assignedMenuIds);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+30
-29
@@ -17,7 +17,9 @@ import top.xyquan.ecomdmbbackend.modules.sys.permission.model.po.PermissionPO;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -41,16 +43,14 @@ public class PositionPermissionServiceImpl extends ServiceImpl<PositionPermissio
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void batchAssignPositionPermissions(BatchAssignPositionPermissionDTO batchAssignDTO) {
|
||||
Long positionId = batchAssignDTO.getPositionId();
|
||||
List<Long> permissionIds = batchAssignDTO.getPermissionIds();
|
||||
List<Long> expandedPermissionIds = expandPermissionIdsDownward(batchAssignDTO.getPermissionIds());
|
||||
|
||||
// 先删除该岗位的所有权限关联
|
||||
LambdaQueryWrapper<PositionPermissionPO> deleteWrapper = new LambdaQueryWrapper<>();
|
||||
deleteWrapper.eq(PositionPermissionPO::getPositionId, positionId);
|
||||
this.remove(deleteWrapper);
|
||||
|
||||
// 批量插入新的权限关联
|
||||
List<PositionPermissionPO> positionPermissions = new ArrayList<>();
|
||||
for (Long permissionId : permissionIds) {
|
||||
for (Long permissionId : expandedPermissionIds) {
|
||||
PositionPermissionPO positionPermission = new PositionPermissionPO();
|
||||
positionPermission.setPositionId(positionId);
|
||||
positionPermission.setPermissionId(permissionId);
|
||||
@@ -61,8 +61,8 @@ public class PositionPermissionServiceImpl extends ServiceImpl<PositionPermissio
|
||||
if (!positionPermissions.isEmpty()) {
|
||||
this.saveBatch(positionPermissions);
|
||||
}
|
||||
|
||||
log.info("批量分配岗位权限完成,岗位ID:{},权限数量:{}", positionId, permissionIds.size());
|
||||
|
||||
log.info("批量分配岗位权限完成,岗位ID:{},权限数量:{}", positionId, expandedPermissionIds.size());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -198,29 +198,10 @@ public class PositionPermissionServiceImpl extends ServiceImpl<PositionPermissio
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void batchUpdatePositionPermissions(Long positionId, List<Long> permissionIds) {
|
||||
// 先删除该岗位的所有权限关联
|
||||
LambdaQueryWrapper<PositionPermissionPO> deleteWrapper = new LambdaQueryWrapper<>();
|
||||
deleteWrapper.eq(PositionPermissionPO::getPositionId, positionId);
|
||||
this.remove(deleteWrapper);
|
||||
|
||||
// 批量插入新的权限关联
|
||||
if (permissionIds != null && !permissionIds.isEmpty()) {
|
||||
List<PositionPermissionPO> positionPermissions = new ArrayList<>();
|
||||
for (Long permissionId : permissionIds) {
|
||||
PositionPermissionPO positionPermission = new PositionPermissionPO();
|
||||
positionPermission.setPositionId(positionId);
|
||||
positionPermission.setPermissionId(permissionId);
|
||||
positionPermission.setCreateBy(currentUserContext.getCurrentUserId());
|
||||
positionPermissions.add(positionPermission);
|
||||
}
|
||||
|
||||
if (!positionPermissions.isEmpty()) {
|
||||
this.saveBatch(positionPermissions);
|
||||
}
|
||||
}
|
||||
|
||||
log.info("批量更新岗位权限完成,岗位ID:{},权限数量:{}", positionId,
|
||||
permissionIds != null ? permissionIds.size() : 0);
|
||||
BatchAssignPositionPermissionDTO batchAssignDTO = new BatchAssignPositionPermissionDTO();
|
||||
batchAssignDTO.setPositionId(positionId);
|
||||
batchAssignDTO.setPermissionIds(permissionIds);
|
||||
batchAssignPositionPermissions(batchAssignDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -249,4 +230,24 @@ public class PositionPermissionServiceImpl extends ServiceImpl<PositionPermissio
|
||||
this.saveBatch(toAdd);
|
||||
log.info("追加岗位权限完成,岗位ID:{},新增权限数量:{}", positionId, toAdd.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* 分配父权限时向下展开所有子孙权限 ID
|
||||
*/
|
||||
private List<Long> expandPermissionIdsDownward(List<Long> permissionIds) {
|
||||
if (permissionIds == null || permissionIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
Set<Long> expanded = new LinkedHashSet<>();
|
||||
for (Long permissionId : permissionIds) {
|
||||
if (permissionId == null) {
|
||||
continue;
|
||||
}
|
||||
List<Long> childIds = positionPermissionMapper.selectChildPermissionIds(permissionId);
|
||||
if (childIds != null) {
|
||||
expanded.addAll(childIds);
|
||||
}
|
||||
}
|
||||
return new ArrayList<>(expanded);
|
||||
}
|
||||
}
|
||||
|
||||
+10
@@ -21,4 +21,14 @@ public interface MenuMapper extends BaseMapper<MenuPO> {
|
||||
* @return 权限列表
|
||||
*/
|
||||
List<PermissionVO> selectPermissionsByMenuId(@Param("menuId") Long menuId);
|
||||
|
||||
/**
|
||||
* 递归查询指定菜单ID下的所有子菜单ID(包括自己)
|
||||
*/
|
||||
List<Long> selectChildMenuIds(@Param("menuId") Long menuId);
|
||||
|
||||
/**
|
||||
* 递归查询指定菜单ID的所有祖先ID(从自己到根节点)
|
||||
*/
|
||||
List<Long> selectAncestorMenuIds(@Param("menuId") Long menuId);
|
||||
}
|
||||
|
||||
+37
@@ -7,7 +7,9 @@ import top.xyquan.ecomdmbbackend.modules.sys.menu.model.dto.UpdateMenuDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.menu.model.po.MenuPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.menu.model.vo.MenuVO;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 系统菜单服务接口
|
||||
@@ -65,4 +67,39 @@ public interface MenuService extends IService<MenuPO> {
|
||||
* @param batchSortMenuDTO 批量排序信息
|
||||
*/
|
||||
void batchSortMenu(BatchSortMenuDTO batchSortMenuDTO);
|
||||
|
||||
/**
|
||||
* 分配时向下展开:每个选中菜单及其所有子孙
|
||||
*/
|
||||
List<Long> expandMenuIdsDownward(List<Long> menuIds);
|
||||
|
||||
/**
|
||||
* 运行时路由可达菜单ID(向下继承)
|
||||
*/
|
||||
Set<Long> expandRouteMenuIds(Collection<Long> assignedMenuIds);
|
||||
|
||||
/**
|
||||
* 树展示可见ID:已分配节点的祖先 + 子孙
|
||||
*/
|
||||
Set<Long> resolveVisibleMenuIds(Collection<Long> assignedMenuIds);
|
||||
|
||||
/**
|
||||
* 根据已分配菜单构建可见菜单树(向上可见 + 向下继承展示)
|
||||
*/
|
||||
List<MenuVO> getVisibleMenuTree(Collection<Long> assignedMenuIds);
|
||||
|
||||
/**
|
||||
* 根据已分配菜单获取路由用扁平菜单列表
|
||||
*/
|
||||
List<MenuVO> getMenusForRoute(Collection<Long> assignedMenuIds);
|
||||
|
||||
/**
|
||||
* 检查目标菜单是否在分配范围内(含父→子继承)
|
||||
*/
|
||||
boolean hasMenuAccess(Collection<Long> assignedMenuIds, Long targetMenuId);
|
||||
|
||||
/**
|
||||
* 从全量树中过滤出可见节点
|
||||
*/
|
||||
List<MenuVO> filterVisibleMenuTree(List<MenuVO> fullTree, Set<Long> visibleIds);
|
||||
}
|
||||
|
||||
+133
@@ -18,8 +18,12 @@ import top.xyquan.ecomdmbbackend.modules.sys.menu.service.MenuService;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -235,6 +239,135 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuPO> implements
|
||||
log.info("批量菜单排序完成");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Long> expandMenuIdsDownward(List<Long> menuIds) {
|
||||
if (menuIds == null || menuIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
// 分配父菜单时自动包含所有子孙;仅选子菜单时只展开自身
|
||||
Set<Long> expanded = new LinkedHashSet<>();
|
||||
for (Long menuId : menuIds) {
|
||||
if (menuId == null) {
|
||||
continue;
|
||||
}
|
||||
List<Long> childIds = baseMapper.selectChildMenuIds(menuId);
|
||||
if (childIds != null) {
|
||||
expanded.addAll(childIds);
|
||||
}
|
||||
}
|
||||
return new ArrayList<>(expanded);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Long> expandRouteMenuIds(Collection<Long> assignedMenuIds) {
|
||||
Set<Long> routeIds = new HashSet<>();
|
||||
if (assignedMenuIds == null) {
|
||||
return routeIds;
|
||||
}
|
||||
for (Long menuId : assignedMenuIds) {
|
||||
if (menuId == null) {
|
||||
continue;
|
||||
}
|
||||
List<Long> childIds = baseMapper.selectChildMenuIds(menuId);
|
||||
if (childIds != null) {
|
||||
routeIds.addAll(childIds);
|
||||
}
|
||||
}
|
||||
return routeIds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Long> resolveVisibleMenuIds(Collection<Long> assignedMenuIds) {
|
||||
Set<Long> visibleIds = new HashSet<>();
|
||||
if (assignedMenuIds == null) {
|
||||
return visibleIds;
|
||||
}
|
||||
for (Long menuId : assignedMenuIds) {
|
||||
if (menuId == null) {
|
||||
continue;
|
||||
}
|
||||
List<Long> ancestors = baseMapper.selectAncestorMenuIds(menuId);
|
||||
List<Long> descendants = baseMapper.selectChildMenuIds(menuId);
|
||||
if (ancestors != null) {
|
||||
visibleIds.addAll(ancestors);
|
||||
}
|
||||
if (descendants != null) {
|
||||
visibleIds.addAll(descendants);
|
||||
}
|
||||
}
|
||||
return visibleIds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuVO> getVisibleMenuTree(Collection<Long> assignedMenuIds) {
|
||||
if (assignedMenuIds == null || assignedMenuIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
Set<Long> visibleIds = resolveVisibleMenuIds(assignedMenuIds);
|
||||
List<MenuVO> fullTree = getMenuTree();
|
||||
return filterVisibleMenuTree(fullTree, visibleIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuVO> getMenusForRoute(Collection<Long> assignedMenuIds) {
|
||||
if (assignedMenuIds == null || assignedMenuIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
Set<Long> routeIds = expandRouteMenuIds(assignedMenuIds);
|
||||
Set<Long> visibleIds = new HashSet<>(routeIds);
|
||||
for (Long menuId : routeIds) {
|
||||
List<Long> ancestors = baseMapper.selectAncestorMenuIds(menuId);
|
||||
if (ancestors != null) {
|
||||
visibleIds.addAll(ancestors);
|
||||
}
|
||||
}
|
||||
return listMenusByIds(visibleIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMenuAccess(Collection<Long> assignedMenuIds, Long targetMenuId) {
|
||||
if (assignedMenuIds == null || assignedMenuIds.isEmpty() || targetMenuId == null) {
|
||||
return false;
|
||||
}
|
||||
Set<Long> routeIds = expandRouteMenuIds(assignedMenuIds);
|
||||
return routeIds.contains(targetMenuId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuVO> filterVisibleMenuTree(List<MenuVO> fullTree, Set<Long> visibleIds) {
|
||||
if (fullTree == null || fullTree.isEmpty() || visibleIds == null || visibleIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
List<MenuVO> result = new ArrayList<>();
|
||||
for (MenuVO node : fullTree) {
|
||||
if (visibleIds.contains(node.getId())) {
|
||||
MenuVO filteredNode = new MenuVO();
|
||||
BeanCopyUtils.copyProperties(node, filteredNode);
|
||||
if (node.getChildren() != null && !node.getChildren().isEmpty()) {
|
||||
filteredNode.setChildren(filterVisibleMenuTree(node.getChildren(), visibleIds));
|
||||
}
|
||||
result.add(filteredNode);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 ID 批量查询启用状态的菜单并转为 VO
|
||||
*/
|
||||
private List<MenuVO> listMenusByIds(Set<Long> menuIds) {
|
||||
if (menuIds == null || menuIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
LambdaQueryWrapper<MenuPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.in(MenuPO::getId, menuIds)
|
||||
.eq(MenuPO::getStatus, 1)
|
||||
.orderByAsc(MenuPO::getOrder);
|
||||
return this.list(wrapper).stream()
|
||||
.map(this::convertToVO)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建菜单树
|
||||
*/
|
||||
|
||||
+11
-10
@@ -90,20 +90,21 @@ public class UserMenuServiceImpl implements UserMenuService {
|
||||
// owner 角色获取公司所有菜单
|
||||
menus = companyMenuService.getCompanyMenusForRoute(companyIdForUser);
|
||||
} else {
|
||||
// member 角色根据职位获取菜单,同时需要公司有该菜单(取交集)
|
||||
// member 角色:公司菜单与岗位菜单取交集(均支持向下继承)
|
||||
if (member.getPositionId() == null) {
|
||||
menus = List.of();
|
||||
} else {
|
||||
// 1. 先查公司有哪些菜单
|
||||
List<Long> companyMenuIds = companyMenuService.getCompanyMenuIds(companyIdForUser);
|
||||
Set<Long> companyMenuIdSet = Set.copyOf(companyMenuIds);
|
||||
|
||||
// 2. 再查职位有哪些菜单
|
||||
List<MenuVO> positionMenus = positionMenuService.getPositionMenusForRoute(member.getPositionId());
|
||||
|
||||
// 3. 取交集:职位菜单必须在公司菜单范围内
|
||||
List<Long> companyAssigned = companyMenuService.getCompanyMenuIds(companyIdForUser);
|
||||
List<Long> positionAssigned = positionMenuService.getPositionMenuIds(member.getPositionId());
|
||||
|
||||
List<MenuVO> companyMenus = menuService.getMenusForRoute(companyAssigned);
|
||||
Set<Long> companyRouteMenuIds = companyMenus.stream()
|
||||
.map(MenuVO::getId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
List<MenuVO> positionMenus = menuService.getMenusForRoute(positionAssigned);
|
||||
menus = positionMenus.stream()
|
||||
.filter(menu -> companyMenuIdSet.contains(menu.getId()))
|
||||
.filter(menu -> companyRouteMenuIds.contains(menu.getId()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
+2
-4
@@ -42,11 +42,9 @@ public class PermissionCheckServiceImpl implements PermissionCheckService {
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. 先检查公司是否分配了该权限
|
||||
// 1. 先检查公司是否分配了该权限(支持父→子继承)
|
||||
if (companyId != null) {
|
||||
// 获取公司所有权限ID,然后根据权限码查找对应权限ID
|
||||
List<String> companyPermissionCodes = companyPermissionService.getCompanyPermissionCodes(companyId);
|
||||
if (!companyPermissionCodes.contains(permissionCode)) {
|
||||
if (!companyPermissionService.hasCompanyPermissionByCode(companyId, permissionCode)) {
|
||||
log.debug("公司[{}]未分配权限[{}]", companyId, permissionCode);
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user