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<>();