Commit 04cdf60d by yuwei

项目初始化

parent 5554970f
package cn.datax.common.base; package cn.datax.common.base;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BaseDao<T> extends BaseMapper<T> { public interface BaseDao<T> extends BaseMapper<T> {
List<T> selectListDataScope(@Param("ew") Wrapper<T> queryWrapper, @Param("dataScope") DataScope dataScope);
IPage<T> selectPageDataScope(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper, @Param("dataScope") DataScope dataScope);
} }
...@@ -11,9 +11,9 @@ public class BaseQueryParams implements Serializable { ...@@ -11,9 +11,9 @@ public class BaseQueryParams implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
// 当前页码 // 当前页码
private Integer pageNum; private Integer pageNum = 1;
// 分页条数 // 分页条数
private Integer pageSize; private Integer pageSize = 20;
// 显示字段 // 显示字段
private List<String> columnList; private List<String> columnList;
// 排序 // 排序
......
...@@ -6,5 +6,5 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -6,5 +6,5 @@ import org.springframework.beans.factory.annotation.Autowired;
public abstract class BaseServiceImpl<M extends BaseDao<T>, T> extends ServiceImpl<M, T> implements BaseService<T> { public abstract class BaseServiceImpl<M extends BaseDao<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {
@Autowired @Autowired
protected M BaseDao; protected M baseDao;
} }
package cn.datax.common.mybatis.annotation; package cn.datax.common.base;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.HashMap;
/** /**
* 数据权限查询参数 * 数据权限查询参数
*/ */
@Data @Data
@EqualsAndHashCode(callSuper = true) public class DataScope {
public class DataScope extends HashMap {
/** /**
* 部门表的别名 * 部门表的别名
*/ */
private String deptAlias = "sys_dept"; private String deptAlias = "sys_dept";
private String deptScopeName = "dept_id";
/** /**
* 用户表的别名 * 用户表的别名
*/ */
private String userAlias = "sys_user"; private String userAlias = "sys_user";
private String userScopeName = "create_by";
} }
...@@ -13,12 +13,21 @@ import java.lang.annotation.*; ...@@ -13,12 +13,21 @@ import java.lang.annotation.*;
public @interface DataScopeAop { public @interface DataScopeAop {
/** /**
* 表的别名
*/
String alias() default "sys_user";
/**
* 部门表的别名 * 部门表的别名
*/ */
String deptAlias() default "sys_dept"; String deptAlias() default "sys_dept";
String deptScopeName() default "dept_id";
/** /**
* 用户表的别名 * 用户表的别名
*/ */
String userAlias() default "sys_user"; String userAlias() default "sys_user";
String userScopeName() default "create_by";
} }
...@@ -20,7 +20,7 @@ import org.aspectj.lang.annotation.Pointcut; ...@@ -20,7 +20,7 @@ import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.MethodSignature;
/** /**
* 数据过滤处理(基于注解式) * 数据过滤处理(基于注解式,用于自定义sql
*/ */
@Slf4j @Slf4j
@Aspect @Aspect
...@@ -68,27 +68,33 @@ public class DataScopeAspect { ...@@ -68,27 +68,33 @@ public class DataScopeAspect {
break; break;
} else if (DataConstant.DataScope.CUSTOM.getKey().equals(roleDataScope)) { } else if (DataConstant.DataScope.CUSTOM.getKey().equals(roleDataScope)) {
sqlString.append(StrUtil.format( sqlString.append(StrUtil.format(
" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", " OR ( SELECT dept_id FROM sys_user_dept WHERE user_id = {}.{} ) IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) "
dataScope.deptAlias() ,dataScope.alias()
,role.getId() ,dataScope.userScopeName()
,"'" + role.getId() + "'"
)); ));
} else if (DataConstant.DataScope.DEPT.getKey().equals(roleDataScope)) { } else if (DataConstant.DataScope.DEPT.getKey().equals(roleDataScope)) {
sqlString.append(StrUtil.format( sqlString.append(StrUtil.format(
" OR {}.dept_id IN ( SELECT dept_id FROM sys_user_dept WHERE user_id = {} ) ", " OR ( SELECT dept_id FROM sys_user_dept WHERE user_id = {}.{} ) IN ( SELECT dept_id FROM sys_user_dept WHERE user_id = {} ) "
dataScope.deptAlias() ,dataScope.alias()
,user.getId() ,dataScope.userScopeName()
,"'" + user.getId() + "'"
)); ));
} else if (DataConstant.DataScope.DEPTANDCHILD.getKey().equals(roleDataScope)) { } else if (DataConstant.DataScope.DEPTANDCHILD.getKey().equals(roleDataScope)) {
sqlString.append(StrUtil.format( sqlString.append(StrUtil.format(
" OR {}.dept_id IN ( SELECT descendant FROM sys_dept_relation WHERE ancestor = {} )", " OR ( SELECT dept_id FROM sys_user_dept WHERE user_id = {}.{} ) IN ( SELECT descendant FROM sys_dept_relation WHERE ancestor = {} )"
dataScope.deptAlias() ,dataScope.alias()
,role.getId() ,dataScope.userScopeName()
,"'" + role.getId() + "'"
)); ));
} else if (DataConstant.DataScope.SELF.getKey().equals(roleDataScope)) { } else if (DataConstant.DataScope.SELF.getKey().equals(roleDataScope)) {
if (StrUtil.isNotBlank(dataScope.userAlias())) { if (StrUtil.isNotBlank(dataScope.alias())) {
sqlString.append(StrUtil.format(" OR {}.user_id = {} ", dataScope.userAlias(), user.getId())); sqlString.append(StrUtil.format(" OR {}.{} = {} "
,dataScope.alias()
,dataScope.userScopeName()
,user.getId()));
} else { } else {
// 数据权限为仅本人且没有userAlias别名不查询任何数据 // 数据权限为仅本人且没有alias别名不查询任何数据
sqlString.append(" OR 1=0 "); sqlString.append(" OR 1=0 ");
} }
} }
......
package cn.datax.common.mybatis.config; package cn.datax.common.mybatis.config;
import cn.datax.common.mybatis.aspectj.DataScopeAspect; import cn.datax.common.mybatis.aspectj.DataScopeAspect;
import cn.datax.common.mybatis.injector.DataLogicSqlInjector;
import cn.datax.common.mybatis.interceptor.DataScopeInterceptor; import cn.datax.common.mybatis.interceptor.DataScopeInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
...@@ -40,4 +41,13 @@ public class DataBatisPlusConfig { ...@@ -40,4 +41,13 @@ public class DataBatisPlusConfig {
public DataScopeAspect dataScopeAspect() { public DataScopeAspect dataScopeAspect() {
return new DataScopeAspect(); return new DataScopeAspect();
} }
/**
* 自定义 SqlInjector
* 里面包含自定义的全局方法
*/
@Bean
public DataLogicSqlInjector myLogicSqlInjector() {
return new DataLogicSqlInjector();
}
} }
package cn.datax.common.mybatis.injector;
import cn.datax.common.mybatis.injector.methods.SelectListDataScope;
import cn.datax.common.mybatis.injector.methods.SelectPageDataScope;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import java.util.List;
/**
* 自定义 SqlInjector
*/
public class DataLogicSqlInjector extends DefaultSqlInjector {
/**
* 如果只需增加方法,保留MP自带方法
* 可以super.getMethodList() 再add
* @return
*/
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
methodList.add(new SelectListDataScope());
methodList.add(new SelectPageDataScope());
return methodList;
}
}
package cn.datax.common.mybatis.injector.methods;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
public class SelectListDataScope extends AbstractMethod {
public SelectListDataScope() {
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_LIST;
String sql = String.format(sqlMethod.getSql(), this.sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), this.sqlWhereEntityWrapper(true, tableInfo), this.sqlComment());
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
return this.addSelectMappedStatementForTable(mapperClass, "selectListDataScope", sqlSource, tableInfo);
}
}
package cn.datax.common.mybatis.injector.methods;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
public class SelectPageDataScope extends AbstractMethod {
public SelectPageDataScope() {
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_PAGE;
String sql = String.format(sqlMethod.getSql(), this.sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), this.sqlWhereEntityWrapper(true, tableInfo), this.sqlComment());
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
return this.addSelectMappedStatementForTable(mapperClass, "selectPageDataScope", sqlSource, tableInfo);
}
}
package cn.datax.common.mybatis.interceptor; package cn.datax.common.mybatis.interceptor;
import cn.datax.common.base.DataScope;
import cn.datax.common.core.DataConstant; import cn.datax.common.core.DataConstant;
import cn.datax.common.core.DataRole; import cn.datax.common.core.DataRole;
import cn.datax.common.core.DataUser; import cn.datax.common.core.DataUser;
import cn.datax.common.mybatis.annotation.DataScope;
import cn.datax.common.utils.SecurityUtil; import cn.datax.common.utils.SecurityUtil;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
...@@ -58,12 +58,13 @@ public class DataScopeInterceptor extends AbstractSqlParserHandler implements In ...@@ -58,12 +58,13 @@ public class DataScopeInterceptor extends AbstractSqlParserHandler implements In
if (null != currentUser) { if (null != currentUser) {
// 如果是超级管理员,则不过滤数据 // 如果是超级管理员,则不过滤数据
if (!currentUser.isAdmin()) { if (!currentUser.isAdmin()) {
dataScopeFilter(currentUser, dataScope); String sqlString = dataScopeFilter(currentUser, dataScope);
// originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + scopeName + " in (" + join + ")"; if (StrUtil.isNotBlank(sqlString)) {
// metaObject.setValue("delegate.boundSql.sql", originalSql); originalSql = "SELECT * FROM (" + originalSql + ") temp_data_scope WHERE 1=1 AND (" + sqlString.substring(4) + ")";
metaObject.setValue("delegate.boundSql.sql", originalSql);
}
} }
} }
log.info("originalSql:{}", originalSql);
return invocation.proceed(); return invocation.proceed();
} }
} }
...@@ -74,7 +75,7 @@ public class DataScopeInterceptor extends AbstractSqlParserHandler implements In ...@@ -74,7 +75,7 @@ public class DataScopeInterceptor extends AbstractSqlParserHandler implements In
* @param user * @param user
* @param dataScope * @param dataScope
*/ */
private void dataScopeFilter(DataUser user, DataScope dataScope) { private String dataScopeFilter(DataUser user, DataScope dataScope) {
StringBuilder sqlString = new StringBuilder(); StringBuilder sqlString = new StringBuilder();
List<DataRole> roles = user.getRoles(); List<DataRole> roles = user.getRoles();
if (CollUtil.isNotEmpty(roles)){ if (CollUtil.isNotEmpty(roles)){
...@@ -85,33 +86,32 @@ public class DataScopeInterceptor extends AbstractSqlParserHandler implements In ...@@ -85,33 +86,32 @@ public class DataScopeInterceptor extends AbstractSqlParserHandler implements In
break; break;
} else if (DataConstant.DataScope.CUSTOM.getKey().equals(roleDataScope)) { } else if (DataConstant.DataScope.CUSTOM.getKey().equals(roleDataScope)) {
sqlString.append(StrUtil.format( sqlString.append(StrUtil.format(
" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", " OR ( SELECT dept_id FROM sys_user_dept WHERE user_id = {} ) IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) "
dataScope.getDeptAlias() ,dataScope.getUserScopeName()
,role.getId() ,"'" + role.getId() + "'"
)); ));
} else if (DataConstant.DataScope.DEPT.getKey().equals(roleDataScope)) { } else if (DataConstant.DataScope.DEPT.getKey().equals(roleDataScope)) {
sqlString.append(StrUtil.format( sqlString.append(StrUtil.format(
" OR {}.dept_id IN ( SELECT dept_id FROM sys_user_dept WHERE user_id = {} ) ", " OR ( SELECT dept_id FROM sys_user_dept WHERE user_id = {} ) IN ( SELECT dept_id FROM sys_user_dept WHERE user_id = {} ) "
dataScope.getDeptAlias() ,dataScope.getUserScopeName()
,user.getId() ,"'" + user.getId() + "'"
)); ));
} else if (DataConstant.DataScope.DEPTANDCHILD.getKey().equals(roleDataScope)) { } else if (DataConstant.DataScope.DEPTANDCHILD.getKey().equals(roleDataScope)) {
sqlString.append(StrUtil.format( sqlString.append(StrUtil.format(
" OR {}.dept_id IN ( SELECT descendant FROM sys_dept_relation WHERE ancestor = {} )", " OR ( SELECT dept_id FROM sys_user_dept WHERE user_id = {} ) IN ( SELECT descendant FROM sys_dept_relation WHERE ancestor = {} )"
dataScope.getDeptAlias() ,dataScope.getUserScopeName()
,role.getId() ,"'" + role.getId() + "'"
)); ));
} else if (DataConstant.DataScope.SELF.getKey().equals(roleDataScope)) { } else if (DataConstant.DataScope.SELF.getKey().equals(roleDataScope)) {
if (StrUtil.isNotBlank(dataScope.getUserAlias())) { sqlString.append(StrUtil.format(" OR {} = {} "
sqlString.append(StrUtil.format(" OR {}.user_id = {} ", dataScope.getUserAlias(), user.getId())); ,dataScope.getUserScopeName()
} else { ,"'" + user.getId() + "'"
// 数据权限为仅本人且没有userAlias别名不查询任何数据 ));
sqlString.append(" OR 1=0 ");
}
} }
} }
} }
log.info("数据范围过滤:{}", sqlString); log.info("数据范围过滤:{}", sqlString);
return sqlString.toString();
} }
/** /**
......
package cn.datax.service.system.controller;
import cn.datax.common.base.BaseController;
import cn.datax.common.base.DataScope;
import cn.datax.common.core.R;
import cn.datax.service.system.api.entity.UserEntity;
import cn.datax.service.system.api.query.UserQuery;
import cn.datax.service.system.service.UserService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController extends BaseController {
@Autowired
private UserService userService;
@GetMapping("/users/pageDataScope")
public R getUserPageDataScope(UserQuery userQuery) {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
if(CollUtil.isNotEmpty(userQuery.getColumnList())){
queryWrapper.select(ArrayUtil.toArray(userQuery.getColumnList(), String.class));
}
queryWrapper.like(StrUtil.isNotBlank(userQuery.getUsername()), "username", userQuery.getUsername());
queryWrapper.apply(StrUtil.isNotBlank(userQuery.getDeptId()), "(select dept_id from sys_user_dept where user_id = id) = {0}", userQuery.getDeptId());
if(CollUtil.isNotEmpty(userQuery.getOrderList())){
userQuery.getOrderList().stream().forEach(orderItem -> {
queryWrapper.orderBy(StrUtil.isNotBlank(orderItem.getColumn()), orderItem.isAsc(), orderItem.getColumn());
});
}
IPage<UserEntity> page = userService.pageDataScope(new Page<>(userQuery.getPageNum(), userQuery.getPageSize()), queryWrapper, new DataScope());
return R.ok().setData(page);
}
}
package cn.datax.service.system.service; package cn.datax.service.system.service;
import cn.datax.common.base.BaseService; import cn.datax.common.base.BaseService;
import cn.datax.common.base.DataScope;
import cn.datax.service.system.api.dto.UserDto; import cn.datax.service.system.api.dto.UserDto;
import cn.datax.service.system.api.dto.UserPasswordDto; import cn.datax.service.system.api.dto.UserPasswordDto;
import cn.datax.service.system.api.entity.UserEntity; import cn.datax.service.system.api.entity.UserEntity;
import cn.datax.service.system.api.vo.UserInfo; import cn.datax.service.system.api.vo.UserInfo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
/** /**
* <p> * <p>
...@@ -25,4 +30,6 @@ public interface UserService extends BaseService<UserEntity> { ...@@ -25,4 +30,6 @@ public interface UserService extends BaseService<UserEntity> {
void updateUserPassword(UserPasswordDto user); void updateUserPassword(UserPasswordDto user);
UserInfo getUserByUsername(String username); UserInfo getUserByUsername(String username);
IPage<UserEntity> pageDataScope(IPage<UserEntity> page, Wrapper<UserEntity> queryWrapper, DataScope dataScope);
} }
package cn.datax.service.system.service.impl; package cn.datax.service.system.service.impl;
import cn.datax.common.base.DataScope;
import cn.datax.common.exception.DataException; import cn.datax.common.exception.DataException;
import cn.datax.service.system.api.dto.UserDto; import cn.datax.service.system.api.dto.UserDto;
import cn.datax.service.system.api.dto.UserPasswordDto; import cn.datax.service.system.api.dto.UserPasswordDto;
...@@ -14,6 +15,8 @@ import cn.datax.common.base.BaseServiceImpl; ...@@ -14,6 +15,8 @@ import cn.datax.common.base.BaseServiceImpl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheConfig;
...@@ -177,4 +180,9 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem ...@@ -177,4 +180,9 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem
} }
return userInfo; return userInfo;
} }
@Override
public IPage<UserEntity> pageDataScope(IPage<UserEntity> page, Wrapper<UserEntity> queryWrapper, DataScope dataScope) {
return baseMapper.selectPageDataScope(page, queryWrapper, dataScope);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment