From db5e501344a9050afc4c9cfb3b242a4c68dd495d Mon Sep 17 00:00:00 2001 From: rikugun Date: Fri, 3 Apr 2020 15:47:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=20=20=20=E5=A2=9E=E5=8A=A0=E5=AD=90?= =?UTF-8?q?=E7=BA=A7=E9=83=A8=E9=97=A8=E6=95=B0=E6=8D=AE=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/me/zhengjie/config/DataScope.java | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/eladmin-system/src/main/java/me/zhengjie/config/DataScope.java b/eladmin-system/src/main/java/me/zhengjie/config/DataScope.java index 99de2835..69dfb1e6 100644 --- a/eladmin-system/src/main/java/me/zhengjie/config/DataScope.java +++ b/eladmin-system/src/main/java/me/zhengjie/config/DataScope.java @@ -8,6 +8,9 @@ import me.zhengjie.modules.system.service.dto.RoleSmallDto; import me.zhengjie.modules.system.service.dto.UserDto; import me.zhengjie.utils.SecurityUtils; import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; +import org.springframework.util.ObjectUtils; + import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -21,7 +24,7 @@ import java.util.Set; @Component public class DataScope { - private final String[] scopeType = {"全部","本级","自定义"}; + private final String[] scopeType = {"全部","本级","子级","自定义"}; private final UserService userService; @@ -55,9 +58,14 @@ public class DataScope { if (scopeType[1].equals(role.getDataScope())) { deptIds.add(user.getDept().getId()); } + // 存储本级和子级的数据权限 + if (scopeType[2].equals(role.getDataScope())) { + deptIds.add(user.getDept().getId()); + deptIds.addAll(getDeptChildren(deptService.findByPid(user.getDept().getId()))); + } // 存储自定义的数据权限 - if (scopeType[2].equals(role.getDataScope())) { + if (scopeType[3].equals(role.getDataScope())) { Set depts = deptService.findByRoleIds(role.getId()); for (Dept dept : depts) { deptIds.add(dept.getId()); @@ -71,6 +79,34 @@ public class DataScope { return deptIds; } + /** + * 根据部门ID 获取所以子级部门ID,包括本机 + * @param deptPid + * @return + */ + public Set getDeptIdsByPid(Long deptPid){ + Set deptSet = new HashSet<>(); + Set result = new HashSet<>(); + if (!ObjectUtils.isEmpty(deptPid)) { + deptSet.add(deptPid); + deptSet.addAll(getDeptChildren(deptService.findByPid(deptPid))); + } + // 数据权限 + Set deptIds = getDeptIds(); + // 查询条件不为空并且数据权限不为空则取交集 + if (!CollectionUtils.isEmpty(deptIds) && !CollectionUtils.isEmpty(deptSet)){ + // 取交集 + result.addAll(deptSet); + result.retainAll(deptIds); + // 若无交集,则代表无数据权限 + return result; + // 否则取并集 + } else { + result.addAll(deptSet); + result.addAll(deptIds); + return result; + } + } public List getDeptChildren(List deptList) { List list = new ArrayList<>();