Merge branch 'master' into mybatis-plus

# Conflicts:
#	eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java
pull/459/head
ZhengJie 2020-08-02 15:49:28 +08:00
commit 1cbbe38018
6 changed files with 48 additions and 16 deletions

View File

@ -20,6 +20,7 @@ import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.utils.enums.DataScopeEnum;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
@ -84,4 +85,16 @@ public class SecurityUtils {
JSONArray array = JSONUtil.parseArray(new JSONObject(userDetails).get("dataScopes")); JSONArray array = JSONUtil.parseArray(new JSONObject(userDetails).get("dataScopes"));
return JSONUtil.toList(array,Long.class); return JSONUtil.toList(array,Long.class);
} }
/**
*
* @return
*/
public static String getDataScopeType() {
List<Long> dataScopes = getCurrentUserDataScope();
if(dataScopes.size() != 0){
return "";
}
return DataScopeEnum.ALL.getValue();
}
} }

View File

@ -65,7 +65,7 @@ public class DataServiceImpl implements DataService {
deptIds.addAll(getCustomize(deptIds, role)); deptIds.addAll(getCustomize(deptIds, role));
break; break;
default: default:
break; return new ArrayList<>(deptIds);
} }
} }
return new ArrayList<>(deptIds); return new ArrayList<>(deptIds);

View File

@ -25,19 +25,16 @@ import me.zhengjie.modules.system.repository.RoleRepository;
import me.zhengjie.modules.system.repository.UserRepository; import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.dto.DeptDto; import me.zhengjie.modules.system.service.dto.DeptDto;
import me.zhengjie.modules.system.service.dto.DeptQueryCriteria; import me.zhengjie.modules.system.service.dto.DeptQueryCriteria;
import me.zhengjie.utils.FileUtil; import me.zhengjie.utils.*;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.RedisUtils;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.system.repository.DeptRepository; import me.zhengjie.modules.system.repository.DeptRepository;
import me.zhengjie.modules.system.service.DeptService; import me.zhengjie.modules.system.service.DeptService;
import me.zhengjie.modules.system.service.mapstruct.DeptMapper; import me.zhengjie.modules.system.service.mapstruct.DeptMapper;
import me.zhengjie.utils.enums.DataScopeEnum;
import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -45,9 +42,9 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* @author Zheng Jie * @author Zheng Jie
* @date 2019-03-25 * @date 2019-03-25
*/ */
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@CacheConfig(cacheNames = "dept") @CacheConfig(cacheNames = "dept")
@ -62,13 +59,13 @@ public class DeptServiceImpl implements DeptService {
@Override @Override
public List<DeptDto> queryAll(DeptQueryCriteria criteria, Boolean isQuery) throws Exception { public List<DeptDto> queryAll(DeptQueryCriteria criteria, Boolean isQuery) throws Exception {
Sort sort = new Sort(Sort.Direction.ASC, "deptSort"); Sort sort = new Sort(Sort.Direction.ASC, "deptSort");
String dataScopeType = SecurityUtils.getDataScopeType();
if (isQuery) { if (isQuery) {
criteria.setPidIsNull(true); if(dataScopeType.equals(DataScopeEnum.ALL.getValue())){
List<Field> fields = QueryHelp.getAllFields(criteria.getClass()); criteria.setPidIsNull(true);
List<String> fieldNames = new ArrayList<String>() {{ }
add("pidIsNull"); List<Field> fields = QueryHelp.getAllFields(criteria.getClass(), new ArrayList<>());
add("enabled"); List<String> fieldNames = new ArrayList<String>(){{ add("pidIsNull");add("enabled");}};
}};
for (Field field : fields) { for (Field field : fields) {
//设置对象的访问权限保证对private的属性的访问 //设置对象的访问权限保证对private的属性的访问
field.setAccessible(true); field.setAccessible(true);
@ -82,7 +79,12 @@ public class DeptServiceImpl implements DeptService {
} }
} }
} }
return deptMapper.toDto(deptRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), sort)); List<DeptDto> list = deptMapper.toDto(deptRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),sort));
// 如果为空,就代表为自定义权限或者本级权限,就需要去重,不理解可以注释掉,看查询结果
if(StringUtils.isBlank(dataScopeType)){
return deduplication(list);
}
return list;
} }
@Override @Override
@ -251,6 +253,23 @@ public class DeptServiceImpl implements DeptService {
} }
} }
private List<DeptDto> deduplication(List<DeptDto> list) {
List<DeptDto> deptDtos = new ArrayList<>();
for (DeptDto deptDto : list) {
boolean flag = true;
for (DeptDto dto : list) {
if (deptDto.getPid().equals(dto.getId())) {
flag = false;
break;
}
}
if (flag){
deptDtos.add(deptDto);
}
}
return deptDtos;
}
/** /**
* *
* *