增加子级部门数据权限
pull/321/head
rikugun 2020-04-03 15:47:49 +08:00
parent 0548d8486d
commit db5e501344
1 changed files with 38 additions and 2 deletions

View File

@ -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<Dept> 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<Long> getDeptIdsByPid(Long deptPid){
Set<Long> deptSet = new HashSet<>();
Set<Long> result = new HashSet<>();
if (!ObjectUtils.isEmpty(deptPid)) {
deptSet.add(deptPid);
deptSet.addAll(getDeptChildren(deptService.findByPid(deptPid)));
}
// 数据权限
Set<Long> 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<Long> getDeptChildren(List<Dept> deptList) {
List<Long> list = new ArrayList<>();