diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/constant/SystemConstants.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/constant/SystemConstants.java index 184e417c7..69a4f04de 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/constant/SystemConstants.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/constant/SystemConstants.java @@ -9,7 +9,7 @@ package com.youlai.admin.constant; public interface SystemConstants { /** - * 根部门ID + * 根部门ID */ Long ROOT_DEPT_ID = 0l; @@ -23,6 +23,8 @@ public interface SystemConstants { */ String DEFAULT_USER_PASSWORD = "123456"; - - + /** + * 超级管理员角色编码 + */ + String ROOT_ROLE_CODE = "ROOT"; } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysDeptServiceImpl.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysDeptServiceImpl.java index b5600d26a..5c7e2e83f 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysDeptServiceImpl.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysDeptServiceImpl.java @@ -120,8 +120,24 @@ public class SysDeptServiceImpl extends ServiceImpl impl .eq(SysDept::getStatus, GlobalConstants.STATUS_YES) .orderByAsc(SysDept::getSort) ); - SysDept sysDept = this.getById(JwtUtils.getJwtPayload().getLong("deptId")); - List deptSelectList = recursionTreeSelectList(sysDept.getParentId(), deptList); + + boolean isRoot = JwtUtils.isRoot(); + Long parentId; + if (isRoot) { // 超级管理员 + parentId = SystemConstants.ROOT_DEPT_ID; + } else { + Long deptId = JwtUtils.getDeptId(); + if (deptId == null) { + return Collections.emptyList(); + } + SysDept dept = this.getById(deptId); + if (dept == null) { + return Collections.emptyList(); + } + parentId = dept.getParentId(); + + } + List deptSelectList = recursionTreeSelectList(parentId, deptList); return deptSelectList; } @@ -161,7 +177,7 @@ public class SysDeptServiceImpl extends ServiceImpl impl // 生成部门树路径 String treePath = generateDeptTreePath(dept); dept.setTreePath(treePath); - + boolean result = this.saveOrUpdate(dept); Assert.isTrue(result, "保存部门出错"); return dept.getId(); diff --git a/youlai-common/common-web/src/main/java/com/youlai/common/web/util/JwtUtils.java b/youlai-common/common-web/src/main/java/com/youlai/common/web/util/JwtUtils.java index fdc0713ba..a1b923c07 100644 --- a/youlai-common/common-web/src/main/java/com/youlai/common/web/util/JwtUtils.java +++ b/youlai-common/common-web/src/main/java/com/youlai/common/web/util/JwtUtils.java @@ -1,5 +1,6 @@ package com.youlai.common.web.util; +import cn.hutool.core.collection.CollectionUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.youlai.common.constant.SecurityConstants; @@ -11,6 +12,8 @@ import org.springframework.web.context.request.ServletRequestAttributes; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; /** @@ -27,7 +30,7 @@ public class JwtUtils { if (null == payload) { throw new BizException("请传入认证头"); } - JSONObject jsonObject = JSONUtil.parseObj(URLDecoder.decode(payload,StandardCharsets.UTF_8.name())); + JSONObject jsonObject = JSONUtil.parseObj(URLDecoder.decode(payload, StandardCharsets.UTF_8.name())); return jsonObject; } @@ -41,6 +44,16 @@ public class JwtUtils { return id; } + /** + * 解析JWT获取用户ID + * + * @return + */ + public static Long getDeptId() { + Long id = getJwtPayload().getLong("deptId"); + return id; + } + /** * 解析JWT获取获取用户名 * @@ -52,18 +65,29 @@ public class JwtUtils { } - /** * JWT获取用户角色列表 * * @return 角色列表 */ public static List getRoles() { - List roles = null; + List roles; JSONObject payload = getJwtPayload(); if (payload.containsKey(SecurityConstants.JWT_AUTHORITIES_KEY)) { roles = payload.getJSONArray(SecurityConstants.JWT_AUTHORITIES_KEY).toList(String.class); + } else { + roles = Collections.emptyList(); } return roles; } + + /** + * 是否「超级管理员」 + * + * @return + */ + public static boolean isRoot() { + List roles = getRoles(); + return CollectionUtil.isNotEmpty(roles) && roles.contains("ROOT"); + } }