From 742d9b5b40d8aa519bc91f57e525a2ef9840026e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com> Date: Thu, 27 Jan 2022 22:08:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(SysDeptServiceImpl):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E8=B6=85=E7=BA=A7=E7=AE=A1=E7=90=86=E5=91=98=E9=83=A8=E9=97=A8?= =?UTF-8?q?=E4=B8=BAnull=E6=9F=A5=E8=AF=A2=E9=83=A8=E9=97=A8=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/constant/SystemConstants.java | 8 +++-- .../service/impl/SysDeptServiceImpl.java | 22 ++++++++++++-- .../com/youlai/common/web/util/JwtUtils.java | 30 +++++++++++++++++-- 3 files changed, 51 insertions(+), 9 deletions(-) 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"); + } }