From 5935688f8e6ca514ac1997eb19c8666c73c00456 Mon Sep 17 00:00:00 2001 From: DaxPay Date: Mon, 17 Feb 2025 15:36:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=8F=9C=E5=8D=95=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=97=B6=E6=94=AF=E6=8C=81=E6=A3=80=E6=9F=A5=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=BD=A2=E6=88=90=E5=BE=AA=E7=8E=AF=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/permission/PermMenuService.java | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/bootx-platform/bootx-platform-service/service-iam/src/main/java/cn/bootx/platform/iam/service/permission/PermMenuService.java b/bootx-platform/bootx-platform-service/service-iam/src/main/java/cn/bootx/platform/iam/service/permission/PermMenuService.java index f668ed55..317c052e 100644 --- a/bootx-platform/bootx-platform-service/service-iam/src/main/java/cn/bootx/platform/iam/service/permission/PermMenuService.java +++ b/bootx-platform/bootx-platform-service/service-iam/src/main/java/cn/bootx/platform/iam/service/permission/PermMenuService.java @@ -11,6 +11,7 @@ import cn.bootx.platform.iam.param.permission.PermMenuParam; import cn.bootx.platform.iam.result.permission.PermMenuResult; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.copier.CopyOptions; +import cn.hutool.core.collection.CollUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.cache.annotation.CacheEvict; @@ -19,6 +20,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.Comparator; import java.util.List; +import java.util.Objects; /** * 菜单权限 @@ -55,13 +57,17 @@ public class PermMenuService { @Transactional(rollbackFor = Exception.class) public void update(PermMenuParam param) { PermMenu permMenu = permMenuManager.findById(param.getId()).orElseThrow(() -> new DataNotExistException("菜单不存在")); - BeanUtil.copyProperties(param, permMenu, CopyOptions.create().ignoreNullValue()); // 判断是否是一级菜单,是的话清空父菜单ID if (param.isRoot()) { permMenu.setPid(null); } - // TODO 检查上级菜单是否出现了循环依赖 + // 检查上级菜单是否出现了循环依赖 + List tree = this.tree(param.getClientCode()); + if (this.isDescendant(permMenu.toResult(), param.getPid())) { + throw new BizException("上级菜单不能是自身或下级菜单"); + } + BeanUtil.copyProperties(param, permMenu, CopyOptions.create().ignoreNullValue()); permMenuManager.updateById(permMenu); } @@ -107,6 +113,33 @@ public class PermMenuService { permMenuManager.deleteById(id); } + /** + * 判断某个菜单是否是另一个菜单的下级菜单 + * + * @param menu 当前菜单 + * @param pid 要设置的父菜单 + * @return 是否为下级菜单 + */ + private boolean isDescendant(PermMenuResult menu, Long pid) { + if (CollUtil.isEmpty(menu.getChildren())) { + return false; + } + // 如果是否为自身 + if (Objects.equals(menu.getId(), pid)) { + return true; + } + // 检测是否为子孙菜单 + for (var child : menu.getChildren()) { + if (child.getId().equals(pid)) { + return true; + } + if (this.isDescendant(child, pid)) { + return true; + } + } + return false; + } + /** * 菜单树(查看全部) */