feat: 添加数据权限拦截器

添加数据权限拦截器
This commit is contained in:
chuan 2021-12-10 18:52:18 +08:00
parent 7edbb55dc1
commit a0bfa1e919
4 changed files with 159 additions and 6 deletions

View File

@ -1,10 +1,20 @@
package com.youlai.admin.mapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.youlai.admin.pojo.entity.SysDept;
import com.youlai.common.mybatis.handler.InterceptorIgnore;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface SysDeptMapper extends BaseMapper<SysDept> {
@InterceptorIgnore
@Override
List<SysDept> selectList(@Param(Constants.WRAPPER) Wrapper<SysDept> queryWrapper);
}

View File

@ -4,12 +4,9 @@ import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.youlai.common.mybatis.handler.MyMetaObjectHandler;
import com.youlai.common.mybatis.handler.IntegerArrayJsonTypeHandler;
import com.youlai.common.mybatis.handler.LongArrayJsonTypeHandler;
import com.youlai.common.mybatis.handler.StringArrayJsonTypeHandler;
import com.youlai.common.mybatis.handler.*;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.springframework.context.annotation.Bean;
@ -26,12 +23,14 @@ public class MybatisPlusConfig {
/**
* 分页插件
* 分页插件和数据权限插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//数据权限
interceptor.addInnerInterceptor(new DataPermissionInterceptor(new DataPermissionHandlerImpl()));
return interceptor;
}

View File

@ -0,0 +1,121 @@
package com.youlai.common.mybatis.handler;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import java.lang.reflect.Method;
/**
* 部门数据权限
*
* @author <a href="mailto:2256222053@qq.com">zc</a>
* @Date 2021-12-10 13:28
*/
@Slf4j
public class DataPermissionHandlerImpl implements DataPermissionHandler {
@Override
public Expression getSqlSegment(Expression where, String mappedStatementId) {
try {
Class<?> clazz = Class.forName(mappedStatementId.substring(0, mappedStatementId.lastIndexOf(".")));
String methodName = mappedStatementId.substring(mappedStatementId.lastIndexOf(".") + 1);
clazz.getAnnotatedSuperclass();
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
InterceptorIgnore annotation = method.getAnnotation(InterceptorIgnore.class);
if (ObjectUtils.isNotEmpty(annotation) && (method.getName().equals(methodName) || (method.getName() + "_COUNT").equals(methodName))) {
// 获取当前的用户
log.info(annotation.dataPermission());
//LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
//if (ObjectUtils.isNotEmpty(loginUser) && ObjectUtils.isNotEmpty(loginUser.getUser()) && !loginUser.getUser().isAdmin()) {
// return dataScopeFilter(loginUser.getUser(), annotation.value(), where);
//}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return where;
}
/**
* 构建过滤条件
*
* @param user 当前登录用户
* @param where 当前查询条件
* @return 构建后查询条件
*/
//public static Expression dataScopeFilter(SysUser user, String tableAlias, Expression where) {
// Expression expression = null;
// for (SysRole role : user.getRoles()) {
// String dataScope = role.getDataScope();
// if (DataScopeAspect.DATA_SCOPE_ALL.equals(dataScope)) {
// return where;
// }
// if (DataScopeAspect.DATA_SCOPE_CUSTOM.equals(dataScope)) {
// InExpression inExpression = new InExpression();
// inExpression.setLeftExpression(buildColumn(tableAlias, "dept_id"));
// SubSelect subSelect = new SubSelect();
// PlainSelect select = new PlainSelect();
// select.setSelectItems(Collections.singletonList(new SelectExpressionItem(new Column("dept_id"))));
// select.setFromItem(new Table("sys_role_dept"));
// EqualsTo equalsTo = new EqualsTo();
// equalsTo.setLeftExpression(new Column("role_id"));
// equalsTo.setRightExpression(new LongValue(role.getRoleId()));
// select.setWhere(equalsTo);
// subSelect.setSelectBody(select);
// inExpression.setRightExpression(subSelect);
// expression = ObjectUtils.isNotEmpty(expression) ? new OrExpression(expression, inExpression) : inExpression;
// }
// if (DataScopeAspect.DATA_SCOPE_DEPT.equals(dataScope)) {
// EqualsTo equalsTo = new EqualsTo();
// equalsTo.setLeftExpression(buildColumn(tableAlias, "dept_id"));
// equalsTo.setRightExpression(new LongValue(user.getDeptId()));
// expression = ObjectUtils.isNotEmpty(expression) ? new OrExpression(expression, equalsTo) : equalsTo;
// }
// if (DataScopeAspect.DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope)) {
// InExpression inExpression = new InExpression();
// inExpression.setLeftExpression(buildColumn(tableAlias, "dept_id"));
// SubSelect subSelect = new SubSelect();
// PlainSelect select = new PlainSelect();
// select.setSelectItems(Collections.singletonList(new SelectExpressionItem(new Column("dept_id"))));
// select.setFromItem(new Table("sys_dept"));
// EqualsTo equalsTo = new EqualsTo();
// equalsTo.setLeftExpression(new Column("dept_id"));
// equalsTo.setRightExpression(new LongValue(user.getDeptId()));
// Function function = new Function();
// function.setName("find_in_set");
// function.setParameters(new ExpressionList(new LongValue(user.getDeptId()) , new Column("ancestors")));
// select.setWhere(new OrExpression(equalsTo, function));
// subSelect.setSelectBody(select);
// inExpression.setRightExpression(subSelect);
// expression = ObjectUtils.isNotEmpty(expression) ? new OrExpression(expression, inExpression) : inExpression;
// }
// if (DataScopeAspect.DATA_SCOPE_SELF.equals(dataScope)) {
// EqualsTo equalsTo = new EqualsTo();
// equalsTo.setLeftExpression(buildColumn(tableAlias, "create_by"));
// equalsTo.setRightExpression(new StringValue(user.getUserName()));
// expression = ObjectUtils.isNotEmpty(expression) ? new OrExpression(expression, equalsTo) : equalsTo;
// }
// }
// return ObjectUtils.isNotEmpty(where) ? new AndExpression(where, new Parenthesis(expression)) : expression;
//}
/**
* 构建Column
*
* @param tableAlias 表别名
* @param columnName 字段名称
* @return 带表别名字段
*/
//public static Column buildColumn(String tableAlias, String columnName) {
// if (StringUtils.isNotEmpty(tableAlias)) {
// columnName = tableAlias + "." + columnName;
// }
// return new Column(columnName);
//}
}

View File

@ -0,0 +1,23 @@
package com.youlai.common.mybatis.handler;
import java.lang.annotation.*;
/**
* 是否需要数据权限
*
* @author <a href="mailto:2256222053@qq.com">zc</a>
* @Date 2021-12-10 15:48
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface InterceptorIgnore {
/**
* 数据权限 {@link com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor}
* <p>
* 默认打开需要注解关闭
*/
String dataPermission() default "0";
}