mirror of https://github.com/elunez/eladmin
[代码完善](v2.5): v2.5 beta 数据权限使升级,现可通过注解[@DataPermission]控制
DataPermission 类中有详细说明和使用示例 SecurityUtils 中加入获取当前用户的数据权限的方法 2.5 Beta 详情:https://www.ydyno.com/archives/1225.htmlpull/361/head^2
parent
63f00cd39c
commit
fa26d67469
|
@ -16,8 +16,10 @@
|
|||
package me.zhengjie.utils;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.annotation.DataPermission;
|
||||
import me.zhengjie.annotation.Query;
|
||||
import javax.persistence.criteria.*;
|
||||
import java.lang.reflect.Field;
|
||||
|
@ -33,10 +35,23 @@ public class QueryHelp {
|
|||
|
||||
public static <R, Q> Predicate getPredicate(Root<R> root, Q query, CriteriaBuilder cb) {
|
||||
List<Predicate> list = new ArrayList<>();
|
||||
|
||||
if(query == null){
|
||||
return cb.and(list.toArray(new Predicate[0]));
|
||||
}
|
||||
// 数据权限验证
|
||||
DataPermission permission = query.getClass().getAnnotation(DataPermission.class);
|
||||
if(permission != null){
|
||||
// 获取数据权限
|
||||
List<Long> dataScopes = SecurityUtils.getCurrentUserDataScope();
|
||||
if(CollectionUtil.isNotEmpty(dataScopes)){
|
||||
if(StringUtils.isNotBlank(permission.joinName()) && StringUtils.isNotBlank(permission.fieldName())) {
|
||||
Join join = root.join(permission.joinName(), JoinType.LEFT);
|
||||
list.add(getExpression(permission.fieldName(),join, root).in(dataScopes));
|
||||
} else if (StringUtils.isBlank(permission.joinName()) && StringUtils.isNotBlank(permission.fieldName())) {
|
||||
list.add(getExpression(permission.fieldName(),null, root).in(dataScopes));
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
List<Field> fields = getAllFields(query.getClass(), new ArrayList<>());
|
||||
for (Field field : fields) {
|
||||
|
|
|
@ -15,7 +15,9 @@
|
|||
*/
|
||||
package me.zhengjie.utils;
|
||||
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
@ -23,6 +25,7 @@ import org.springframework.security.core.Authentication;
|
|||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户
|
||||
|
@ -65,11 +68,20 @@ public class SecurityUtils {
|
|||
|
||||
/**
|
||||
* 获取系统用户ID
|
||||
*
|
||||
* @return 系统用户ID
|
||||
*/
|
||||
public static Long getCurrentUserId() {
|
||||
UserDetails userDetails = getCurrentUser();
|
||||
return new JSONObject(new JSONObject(userDetails).get("user")).get("id", Long.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户的数据权限
|
||||
* @return /
|
||||
*/
|
||||
public static List<Long> getCurrentUserDataScope(){
|
||||
UserDetails userDetails = getCurrentUser();
|
||||
JSONArray array = JSONUtil.parseArray(new JSONObject(userDetails).get("dataScopes"));
|
||||
return JSONUtil.toList(array,Long.class);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.aop.log;
|
||||
package me.zhengjie.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
|
@ -51,7 +51,7 @@ public class LogAspect {
|
|||
/**
|
||||
* 配置切入点
|
||||
*/
|
||||
@Pointcut("@annotation(me.zhengjie.aop.log.Log)")
|
||||
@Pointcut("@annotation(me.zhengjie.annotation.Log)")
|
||||
public void logPointcut() {
|
||||
// 该方法无方法体,主要为了让同类中其他方法使用此切入点
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ package me.zhengjie.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.service.LogService;
|
||||
import me.zhengjie.service.dto.LogQueryCriteria;
|
||||
import me.zhengjie.utils.SecurityUtils;
|
||||
|
|
|
@ -81,7 +81,7 @@ public class LogServiceImpl implements LogService {
|
|||
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
me.zhengjie.aop.log.Log aopLog = method.getAnnotation(me.zhengjie.aop.log.Log.class);
|
||||
me.zhengjie.annotation.Log aopLog = method.getAnnotation(me.zhengjie.annotation.Log.class);
|
||||
|
||||
// 方法路径
|
||||
String methodName = joinPoint.getTarget().getClass().getName()+"."+signature.getName()+"()";
|
||||
|
|
|
@ -1,104 +0,0 @@
|
|||
/*
|
||||
* Copyright 2019-2020 Zheng Jie
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.config;
|
||||
|
||||
import me.zhengjie.modules.system.domain.Dept;
|
||||
import me.zhengjie.modules.system.service.DeptService;
|
||||
import me.zhengjie.modules.system.service.RoleService;
|
||||
import me.zhengjie.modules.system.service.UserService;
|
||||
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 java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 数据权限配置
|
||||
* @author Zheng Jie
|
||||
* @date 2019-4-1
|
||||
*/
|
||||
@Component
|
||||
public class DataScope {
|
||||
|
||||
private final String[] scopeType = {"全部","本级","自定义"};
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
private final RoleService roleService;
|
||||
|
||||
private final DeptService deptService;
|
||||
|
||||
public DataScope(UserService userService, RoleService roleService, DeptService deptService) {
|
||||
this.userService = userService;
|
||||
this.roleService = roleService;
|
||||
this.deptService = deptService;
|
||||
}
|
||||
|
||||
public Set<Long> getDeptIds() {
|
||||
|
||||
UserDto user = userService.findByName(SecurityUtils.getCurrentUsername());
|
||||
|
||||
// 用于存储部门id
|
||||
Set<Long> deptIds = new HashSet<>();
|
||||
|
||||
// 查询用户角色
|
||||
List<RoleSmallDto> roleSet = roleService.findByUsersId(user.getId());
|
||||
|
||||
for (RoleSmallDto role : roleSet) {
|
||||
|
||||
if (scopeType[0].equals(role.getDataScope())) {
|
||||
return new HashSet<>() ;
|
||||
}
|
||||
|
||||
// 存储本级的数据权限
|
||||
if (scopeType[1].equals(role.getDataScope())) {
|
||||
deptIds.add(user.getDept().getId());
|
||||
}
|
||||
|
||||
// 存储自定义的数据权限
|
||||
if (scopeType[2].equals(role.getDataScope())) {
|
||||
Set<Dept> depts = deptService.findByRoleIds(role.getId());
|
||||
for (Dept dept : depts) {
|
||||
deptIds.add(dept.getId());
|
||||
List<Dept> deptChildren = deptService.findByPid(dept.getId());
|
||||
if (deptChildren != null && deptChildren.size() != 0) {
|
||||
deptIds.addAll(getDeptChildren(deptChildren));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return deptIds;
|
||||
}
|
||||
|
||||
|
||||
public List<Long> getDeptChildren(List<Dept> deptList) {
|
||||
List<Long> list = new ArrayList<>();
|
||||
deptList.forEach(dept -> {
|
||||
if (dept!=null && dept.getEnabled()){
|
||||
List<Dept> depts = deptService.findByPid(dept.getId());
|
||||
if(deptList.size() != 0){
|
||||
list.addAll(getDeptChildren(depts));
|
||||
}
|
||||
list.add(dept.getId());
|
||||
}
|
||||
}
|
||||
);
|
||||
return list;
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ package me.zhengjie.modules.mnt.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.modules.mnt.domain.App;
|
||||
import me.zhengjie.modules.mnt.service.AppService;
|
||||
import me.zhengjie.modules.mnt.service.dto.AppQueryCriteria;
|
||||
|
|
|
@ -18,7 +18,7 @@ package me.zhengjie.modules.mnt.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.mnt.domain.Database;
|
||||
import me.zhengjie.modules.mnt.service.DatabaseService;
|
||||
|
|
|
@ -18,7 +18,7 @@ package me.zhengjie.modules.mnt.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.modules.mnt.domain.Deploy;
|
||||
import me.zhengjie.modules.mnt.domain.DeployHistory;
|
||||
import me.zhengjie.modules.mnt.service.DeployService;
|
||||
|
|
|
@ -18,7 +18,7 @@ package me.zhengjie.modules.mnt.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.modules.mnt.service.DeployHistoryService;
|
||||
import me.zhengjie.modules.mnt.service.dto.DeployHistoryQueryCriteria;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
|
|
@ -18,7 +18,7 @@ package me.zhengjie.modules.mnt.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.modules.mnt.domain.ServerDeploy;
|
||||
import me.zhengjie.modules.mnt.service.ServerDeployService;
|
||||
import me.zhengjie.modules.mnt.service.dto.ServerDeployQueryCriteria;
|
||||
|
|
|
@ -19,7 +19,7 @@ import io.swagger.annotations.Api;
|
|||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.quartz.domain.QuartzJob;
|
||||
import me.zhengjie.modules.quartz.service.QuartzJobService;
|
||||
|
|
|
@ -24,7 +24,7 @@ import io.swagger.annotations.ApiOperation;
|
|||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.annotation.AnonymousAccess;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.security.config.SecurityProperties;
|
||||
import me.zhengjie.modules.security.security.TokenProvider;
|
||||
|
|
|
@ -18,7 +18,7 @@ package me.zhengjie.modules.security.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.modules.security.service.OnlineUserService;
|
||||
import me.zhengjie.utils.EncryptUtils;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
|
|
@ -19,6 +19,7 @@ import lombok.RequiredArgsConstructor;
|
|||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.exception.EntityNotFoundException;
|
||||
import me.zhengjie.modules.security.service.dto.JwtUserDto;
|
||||
import me.zhengjie.modules.system.service.DataService;
|
||||
import me.zhengjie.modules.system.service.RoleService;
|
||||
import me.zhengjie.modules.system.service.UserService;
|
||||
import me.zhengjie.modules.system.service.dto.UserDto;
|
||||
|
@ -39,6 +40,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|||
|
||||
private final UserService userService;
|
||||
private final RoleService roleService;
|
||||
private final DataService dataService;
|
||||
|
||||
@Override
|
||||
public JwtUserDto loadUserByUsername(String username) {
|
||||
|
@ -57,6 +59,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|||
}
|
||||
return new JwtUserDto(
|
||||
user,
|
||||
dataService.getDeptIds(user),
|
||||
roleService.mapToGrantedAuthorities(user)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -33,10 +33,12 @@ import java.util.stream.Collectors;
|
|||
@AllArgsConstructor
|
||||
public class JwtUserDto implements UserDetails {
|
||||
|
||||
private UserDto user;
|
||||
private final UserDto user;
|
||||
|
||||
private final List<Long> dataScopes;
|
||||
|
||||
@JsonIgnore
|
||||
private List<GrantedAuthority> authorities;
|
||||
private final List<GrantedAuthority> authorities;
|
||||
|
||||
public Set<String> getRoles() {
|
||||
return authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet());
|
||||
|
|
|
@ -20,6 +20,7 @@ import io.swagger.annotations.ApiModelProperty;
|
|||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import me.zhengjie.base.BaseEntity;
|
||||
import me.zhengjie.utils.enums.DataScopeEnum;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
@ -71,7 +72,7 @@ public class Role extends BaseEntity implements Serializable {
|
|||
private String name;
|
||||
|
||||
@ApiModelProperty(value = "数据权限,全部 、 本级 、 自定义")
|
||||
private String dataScope = "本级";
|
||||
private String dataScope = DataScopeEnum.THIS_LEVEL.getValue();
|
||||
|
||||
@Column(name = "level")
|
||||
@ApiModelProperty(value = "级别,数值越小,级别越大")
|
||||
|
|
|
@ -19,8 +19,7 @@ import cn.hutool.core.collection.CollectionUtil;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.config.DataScope;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.system.domain.Dept;
|
||||
import me.zhengjie.modules.system.service.DeptService;
|
||||
|
@ -49,7 +48,6 @@ import java.util.Set;
|
|||
public class DeptController {
|
||||
|
||||
private final DeptService deptService;
|
||||
private final DataScope dataScope;
|
||||
private static final String ENTITY_NAME = "dept";
|
||||
|
||||
@Log("导出部门数据")
|
||||
|
@ -65,8 +63,6 @@ public class DeptController {
|
|||
@GetMapping
|
||||
@PreAuthorize("@el.check('user:list','dept:list')")
|
||||
public ResponseEntity<Object> getDepts(DeptQueryCriteria criteria){
|
||||
// 数据权限
|
||||
criteria.setIds(dataScope.getDeptIds());
|
||||
List<DeptDto> deptDtos = deptService.queryAll(criteria);
|
||||
return new ResponseEntity<>(deptService.buildTree(deptDtos),HttpStatus.OK);
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ package me.zhengjie.modules.system.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.system.domain.Dict;
|
||||
import me.zhengjie.modules.system.service.DictService;
|
||||
|
|
|
@ -18,7 +18,7 @@ package me.zhengjie.modules.system.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.system.domain.DictDetail;
|
||||
import me.zhengjie.modules.system.service.DictDetailService;
|
||||
|
|
|
@ -18,7 +18,7 @@ package me.zhengjie.modules.system.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.system.domain.Job;
|
||||
import me.zhengjie.modules.system.service.JobService;
|
||||
|
|
|
@ -18,7 +18,7 @@ package me.zhengjie.modules.system.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.modules.system.domain.Menu;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.system.service.MenuService;
|
||||
|
|
|
@ -18,7 +18,6 @@ package me.zhengjie.modules.system.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.modules.system.service.MonitorService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
|
|
@ -19,7 +19,7 @@ import cn.hutool.core.lang.Dict;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.modules.system.domain.Role;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.system.service.RoleService;
|
||||
|
|
|
@ -15,13 +15,14 @@
|
|||
*/
|
||||
package me.zhengjie.modules.system.rest;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.RSA;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.config.DataScope;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.modules.system.service.DataService;
|
||||
import me.zhengjie.modules.system.domain.User;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.system.domain.vo.UserPassVo;
|
||||
|
@ -64,7 +65,7 @@ public class UserController {
|
|||
private String privateKey;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final UserService userService;
|
||||
private final DataScope dataScope;
|
||||
private final DataService dataService;
|
||||
private final DeptService deptService;
|
||||
private final RoleService roleService;
|
||||
private final VerifyService verificationCodeService;
|
||||
|
@ -82,33 +83,25 @@ public class UserController {
|
|||
@GetMapping
|
||||
@PreAuthorize("@el.check('user:list')")
|
||||
public ResponseEntity<Object> getUsers(UserQueryCriteria criteria, Pageable pageable){
|
||||
Set<Long> deptSet = new HashSet<>();
|
||||
Set<Long> result = new HashSet<>();
|
||||
if (!ObjectUtils.isEmpty(criteria.getDeptId())) {
|
||||
deptSet.add(criteria.getDeptId());
|
||||
deptSet.addAll(dataScope.getDeptChildren(deptService.findByPid(criteria.getDeptId())));
|
||||
criteria.getDeptIds().add(criteria.getDeptId());
|
||||
criteria.getDeptIds().addAll(dataService.getDeptChildren(deptService.findByPid(criteria.getDeptId())));
|
||||
}
|
||||
// 数据权限
|
||||
Set<Long> deptIds = dataScope.getDeptIds();
|
||||
// 查询条件不为空并且数据权限不为空则取交集
|
||||
if (!CollectionUtils.isEmpty(deptIds) && !CollectionUtils.isEmpty(deptSet)){
|
||||
List<Long> dataScopes = dataService.getDeptIds(userService.findById(SecurityUtils.getCurrentUserId()));
|
||||
// criteria.getDeptIds() 不为空并且数据权限不为空则取交集
|
||||
if (!CollectionUtils.isEmpty(criteria.getDeptIds()) && !CollectionUtils.isEmpty(dataScopes)){
|
||||
// 取交集
|
||||
result.addAll(deptSet);
|
||||
result.retainAll(deptIds);
|
||||
// 若无交集,则代表无数据权限
|
||||
criteria.setDeptIds(result);
|
||||
if(result.size() == 0){
|
||||
return new ResponseEntity<>(PageUtil.toPage(null,0),HttpStatus.OK);
|
||||
} else {
|
||||
criteria.getDeptIds().retainAll(dataScopes);
|
||||
if(!CollectionUtil.isEmpty(criteria.getDeptIds())){
|
||||
return new ResponseEntity<>(userService.queryAll(criteria,pageable),HttpStatus.OK);
|
||||
}
|
||||
// 否则取并集
|
||||
} else {
|
||||
result.addAll(deptSet);
|
||||
result.addAll(deptIds);
|
||||
criteria.setDeptIds(result);
|
||||
// 否则取并集
|
||||
criteria.getDeptIds().addAll(dataScopes);
|
||||
return new ResponseEntity<>(userService.queryAll(criteria,pageable),HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>(PageUtil.toPage(null,0),HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Log("新增用户")
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Copyright 2019-2020 Zheng Jie
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.modules.system.service;
|
||||
|
||||
import me.zhengjie.modules.system.domain.Dept;
|
||||
import me.zhengjie.modules.system.service.dto.UserDto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 数据权限服务类
|
||||
* @author Zheng Jie
|
||||
* @date 2020-05-07
|
||||
*/
|
||||
public interface DataService {
|
||||
|
||||
/**
|
||||
* 获取数据权限
|
||||
* @param user /
|
||||
* @return /
|
||||
*/
|
||||
List<Long> getDeptIds(UserDto user);
|
||||
|
||||
/**
|
||||
* 递归获取子级部门
|
||||
* @param deptList /
|
||||
* @return /
|
||||
*/
|
||||
List<Long> getDeptChildren(List<Dept> deptList);
|
||||
}
|
|
@ -16,22 +16,19 @@
|
|||
package me.zhengjie.modules.system.service.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import me.zhengjie.annotation.DataPermission;
|
||||
import me.zhengjie.annotation.Query;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2019-03-25
|
||||
*/
|
||||
@Data
|
||||
@DataPermission(fieldName = "id")
|
||||
public class DeptQueryCriteria{
|
||||
|
||||
@Query(type = Query.Type.IN, propName="id")
|
||||
private Set<Long> ids;
|
||||
|
||||
@Query(type = Query.Type.INNER_LIKE)
|
||||
private String name;
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ import lombok.Data;
|
|||
import me.zhengjie.annotation.Query;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -33,7 +34,7 @@ public class UserQueryCriteria implements Serializable {
|
|||
private Long id;
|
||||
|
||||
@Query(propName = "id", type = Query.Type.IN, joinName = "dept")
|
||||
private Set<Long> deptIds;
|
||||
private Set<Long> deptIds = new HashSet<>();
|
||||
|
||||
@Query(blurry = "email,username,nickName")
|
||||
private String blurry;
|
||||
|
|
|
@ -15,10 +15,13 @@
|
|||
*/
|
||||
package me.zhengjie.modules.system.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.modules.system.domain.Dept;
|
||||
import me.zhengjie.modules.system.domain.Menu;
|
||||
import me.zhengjie.modules.system.domain.Role;
|
||||
import me.zhengjie.exception.EntityExistException;
|
||||
import me.zhengjie.modules.system.repository.DeptRepository;
|
||||
import me.zhengjie.modules.system.repository.RoleRepository;
|
||||
import me.zhengjie.modules.system.service.RoleService;
|
||||
import me.zhengjie.modules.system.service.dto.RoleDto;
|
||||
|
@ -28,6 +31,7 @@ import me.zhengjie.modules.system.service.dto.UserDto;
|
|||
import me.zhengjie.modules.system.service.mapper.RoleMapper;
|
||||
import me.zhengjie.modules.system.service.mapper.RoleSmallMapper;
|
||||
import me.zhengjie.utils.*;
|
||||
import me.zhengjie.utils.enums.DataScopeEnum;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
|
@ -56,6 +60,7 @@ public class RoleServiceImpl implements RoleService {
|
|||
private final RoleRepository roleRepository;
|
||||
private final RoleMapper roleMapper;
|
||||
private final RoleSmallMapper roleSmallMapper;
|
||||
private final DeptRepository deptRepository;
|
||||
|
||||
@Override
|
||||
@Cacheable
|
||||
|
@ -91,6 +96,7 @@ public class RoleServiceImpl implements RoleService {
|
|||
if(roleRepository.findByName(resources.getName()) != null){
|
||||
throw new EntityExistException(Role.class,"username",resources.getName());
|
||||
}
|
||||
checkDataScope(resources);
|
||||
return roleMapper.toDto(roleRepository.save(resources));
|
||||
}
|
||||
|
||||
|
@ -106,6 +112,7 @@ public class RoleServiceImpl implements RoleService {
|
|||
if(role1 != null && !role1.getId().equals(role.getId())){
|
||||
throw new EntityExistException(Role.class,"username",resources.getName());
|
||||
}
|
||||
checkDataScope(resources);
|
||||
role.setName(resources.getName());
|
||||
role.setDescription(resources.getDescription());
|
||||
role.setDataScope(resources.getDataScope());
|
||||
|
@ -114,6 +121,19 @@ public class RoleServiceImpl implements RoleService {
|
|||
roleRepository.save(role);
|
||||
}
|
||||
|
||||
private void checkDataScope(Role resources){
|
||||
if(CollectionUtil.isNotEmpty(resources.getDepts()) && resources.getDepts().size() == 1){
|
||||
for (Dept dept : resources.getDepts()) {
|
||||
dept = deptRepository.findById(dept.getId()).orElseGet(Dept::new);
|
||||
if(dept.getPid() == 0 || dept.getPid() == null){
|
||||
resources.setDepts(null);
|
||||
resources.setDataScope(DataScopeEnum.ALL.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@CacheEvict(allEntries = true)
|
||||
public void updateMenu(Role resources, RoleDto roleDTO) {
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
*/
|
||||
package ${package}.rest;
|
||||
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import ${package}.domain.${className};
|
||||
import ${package}.service.${className}Service;
|
||||
import ${package}.service.dto.${className}QueryCriteria;
|
||||
|
|
|
@ -20,7 +20,7 @@ import io.swagger.annotations.ApiOperation;
|
|||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.annotation.AnonymousAccess;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.domain.vo.TradeVo;
|
||||
import me.zhengjie.domain.AlipayConfig;
|
||||
import me.zhengjie.utils.AliPayStatusEnum;
|
||||
|
|
|
@ -18,7 +18,7 @@ package me.zhengjie.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.domain.vo.EmailVo;
|
||||
import me.zhengjie.domain.EmailConfig;
|
||||
import me.zhengjie.service.EmailService;
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
package me.zhengjie.rest;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.domain.LocalStorage;
|
||||
import me.zhengjie.service.LocalStorageService;
|
||||
import me.zhengjie.service.dto.LocalStorageQueryCriteria;
|
||||
|
|
|
@ -18,7 +18,7 @@ package me.zhengjie.rest;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.domain.Picture;
|
||||
import me.zhengjie.service.PictureService;
|
||||
import me.zhengjie.service.dto.PictureQueryCriteria;
|
||||
|
|
|
@ -19,7 +19,7 @@ import io.swagger.annotations.Api;
|
|||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.domain.QiniuConfig;
|
||||
import me.zhengjie.domain.QiniuContent;
|
||||
import me.zhengjie.service.dto.QiniuQueryCriteria;
|
||||
|
|
Loading…
Reference in New Issue