Commit 5554970f by yuwei

项目初始化

parent 301052e8
...@@ -18,6 +18,8 @@ public class BaseQueryParams implements Serializable { ...@@ -18,6 +18,8 @@ public class BaseQueryParams implements Serializable {
private List<String> columnList; private List<String> columnList;
// 排序 // 排序
private List<OrderItem> orderList; private List<OrderItem> orderList;
// 数据权限
private String dataScope;
@Data @Data
public class OrderItem{ public class OrderItem{
......
...@@ -10,7 +10,7 @@ import java.lang.annotation.*; ...@@ -10,7 +10,7 @@ import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
@Inherited @Inherited
public @interface DataScope2 { public @interface DataScopeAop {
/** /**
* 部门表的别名 * 部门表的别名
......
package cn.datax.common.mybatis.aspectj;
import java.lang.reflect.Method;
import java.util.List;
import cn.datax.common.base.BaseQueryParams;
import cn.datax.common.core.DataConstant;
import cn.datax.common.core.DataRole;
import cn.datax.common.core.DataUser;
import cn.datax.common.mybatis.annotation.DataScopeAop;
import cn.datax.common.utils.SecurityUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
/**
* 数据过滤处理(基于注解式)
*/
@Slf4j
@Aspect
public class DataScopeAspect {
// 配置织入点
@Pointcut("@annotation(cn.datax.common.mybatis.annotation.DataScopeAop)")
public void dataScopePointCut() {
}
@Before("dataScopePointCut()")
public void doBefore(JoinPoint point) {
handleDataScope(point);
}
protected void handleDataScope(final JoinPoint joinPoint) {
// 获得注解
DataScopeAop dataScope = getAnnotationLog(joinPoint);
if (dataScope == null) {
return;
}
DataUser currentUser = SecurityUtil.getDataUser();
if (null != currentUser) {
// 如果是超级管理员,则不过滤数据
if (!currentUser.isAdmin()) {
dataScopeFilter(joinPoint, currentUser, dataScope);
}
}
}
/**
* 数据范围过滤
*
* @param user
* @param dataScope
*/
private void dataScopeFilter(JoinPoint joinPoint, DataUser user, DataScopeAop dataScope) {
StringBuilder sqlString = new StringBuilder();
List<DataRole> roles = user.getRoles();
if (CollUtil.isNotEmpty(roles)){
for (DataRole role : roles){
Integer roleDataScope = role.getDataScope();
if (DataConstant.DataScope.ALL.getKey().equals(roleDataScope)) {
sqlString = new StringBuilder();
break;
} else if (DataConstant.DataScope.CUSTOM.getKey().equals(roleDataScope)) {
sqlString.append(StrUtil.format(
" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ",
dataScope.deptAlias()
,role.getId()
));
} else if (DataConstant.DataScope.DEPT.getKey().equals(roleDataScope)) {
sqlString.append(StrUtil.format(
" OR {}.dept_id IN ( SELECT dept_id FROM sys_user_dept WHERE user_id = {} ) ",
dataScope.deptAlias()
,user.getId()
));
} else if (DataConstant.DataScope.DEPTANDCHILD.getKey().equals(roleDataScope)) {
sqlString.append(StrUtil.format(
" OR {}.dept_id IN ( SELECT descendant FROM sys_dept_relation WHERE ancestor = {} )",
dataScope.deptAlias()
,role.getId()
));
} else if (DataConstant.DataScope.SELF.getKey().equals(roleDataScope)) {
if (StrUtil.isNotBlank(dataScope.userAlias())) {
sqlString.append(StrUtil.format(" OR {}.user_id = {} ", dataScope.userAlias(), user.getId()));
} else {
// 数据权限为仅本人且没有userAlias别名不查询任何数据
sqlString.append(" OR 1=0 ");
}
}
}
}
if (StrUtil.isNotBlank(sqlString.toString())) {
BaseQueryParams baseQueryParams = (BaseQueryParams) joinPoint.getArgs()[0];
baseQueryParams.setDataScope(" AND (" + sqlString.substring(4) + ")");
}
log.info("数据范围过滤:{}", sqlString);
}
/**
* 是否存在注解,如果存在就获取
*/
private DataScopeAop getAnnotationLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null) {
return method.getAnnotation(DataScopeAop.class);
}
return null;
}
}
package cn.datax.common.mybatis.config; package cn.datax.common.mybatis.config;
import cn.datax.common.mybatis.aspectj.DataScopeAspect;
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;
...@@ -29,4 +30,14 @@ public class DataBatisPlusConfig { ...@@ -29,4 +30,14 @@ public class DataBatisPlusConfig {
public DataScopeInterceptor dataScopeInterceptor() { public DataScopeInterceptor dataScopeInterceptor() {
return new DataScopeInterceptor(); return new DataScopeInterceptor();
} }
/**
* 数据过滤处理(基于注解式)
*
* @return dataScopeAspect
*/
@Bean
public DataScopeAspect dataScopeAspect() {
return new DataScopeAspect();
}
} }
...@@ -104,6 +104,9 @@ public class DataScopeInterceptor extends AbstractSqlParserHandler implements In ...@@ -104,6 +104,9 @@ public class DataScopeInterceptor extends AbstractSqlParserHandler implements In
} else if (DataConstant.DataScope.SELF.getKey().equals(roleDataScope)) { } else if (DataConstant.DataScope.SELF.getKey().equals(roleDataScope)) {
if (StrUtil.isNotBlank(dataScope.getUserAlias())) { if (StrUtil.isNotBlank(dataScope.getUserAlias())) {
sqlString.append(StrUtil.format(" OR {}.user_id = {} ", dataScope.getUserAlias(), user.getId())); sqlString.append(StrUtil.format(" OR {}.user_id = {} ", dataScope.getUserAlias(), user.getId()));
} else {
// 数据权限为仅本人且没有userAlias别名不查询任何数据
sqlString.append(" OR 1=0 ");
} }
} }
} }
......
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