✨ Introducing new features. closed #1400 数据权限支持 Mybatis-plus 自带方法的过滤

This commit is contained in:
gokje committed 2020-06-17 20:39:01 +08:00
commit e7da9c0510
8 files changed
+115 -8

No files matched your search

@@ -47,4 +47,13 @@ public class DataScope extends HashMap {
*/
private Boolean isOnly = false;
/**
* 函数名称,默认 SELECT * ;
*
* <ul>
* <li>COUNT(1)</li>
* </ul>
*/
private DataScopeFuncEnum Func = DataScopeFuncEnum.ALL;
}
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.data.datascope;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 数据权限函数类型
*
* @author lengleng
* @date 2020-06-17
*/
@Getter
@AllArgsConstructor
public enum DataScopeFuncEnum {
/**
* 查询全部数据 SELECT * FROM (originSql) temp_data_scope WHERE temp_data_scope.dept_id IN
* (1)
*/
ALL("*", "全部"),
/**
* 查询函数COUNT SELECT COUNT(1) FROM (originSql) temp_data_scope WHERE
* temp_data_scope.dept_id IN (1)
*/
COUNT("COUNT(1)", "自定义");
/**
* 类型
*/
private final String type;
/**
* 描述
*/
private final String description;
}
@@ -81,13 +81,15 @@ public class DataScopeInterceptor extends AbstractSqlParserHandler implements In
}
if (deptIds.isEmpty()) {
originalSql = "select * from (" + originalSql + ") temp_data_scope where 1 = 2";
originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE 1 = 2",
dataScope.getFunc().getType(), originalSql);
}
else {
String join = CollectionUtil.join(deptIds, ",");
originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + scopeName
+ " in (" + join + ")";
originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s IN (%s)",
dataScope.getFunc().getType(), originalSql, scopeName, join);
}
metaObject.setValue("delegate.boundSql.sql", originalSql);
return invocation.proceed();
}
@@ -9,8 +9,10 @@ import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 若使用数据权限 Mapper继承此类
*
* @author lengleng
* @date 2020/4/26
* @date 2020-06-17
*/
public interface DataScopeMapper<T> extends BaseMapper<T> {
@@ -32,4 +34,12 @@ public interface DataScopeMapper<T> extends BaseMapper<T> {
<E extends IPage<T>> E selectPageByScope(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper,
DataScope scope);
/**
* 根据 Wrapper 条件,查询总记录数
* @param queryWrapper 实体对象封装操作类(可以为 null)
* @param scope 数据权限范围
* @return Integer
*/
Integer selectCountByScope(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, DataScope scope);
}
@@ -6,8 +6,10 @@ import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import java.util.List;
/**
* 支持自定义数据权限方法注入
*
* @author lengleng
* @date 2020/4/26
* @date 2020-06-17
*/
public class DataScopeSqlInjector extends DefaultSqlInjector {
@@ -16,6 +18,7 @@ public class DataScopeSqlInjector extends DefaultSqlInjector {
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
methodList.add(new SelectListByScope());
methodList.add(new SelectPageByScope());
methodList.add(new SelectCountByScope());
return methodList;
}
@@ -0,0 +1,28 @@
package com.pig4cloud.pigx.common.data.datascope;
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;
/**
* 扩展支持COUNT查询数量
*
* @author lengleng
* @date 2020/6/17
*/
public class SelectCountByScope extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_LIST;
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatementForOther(mapperClass, "selectCountByScope", sqlSource, Integer.class);
}
}
@@ -19,13 +19,13 @@
package com.pig4cloud.pigx.admin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.admin.api.dto.UserDTO;
import com.pig4cloud.pigx.admin.api.entity.SysUser;
import com.pig4cloud.pigx.admin.api.vo.UserVO;
import com.pig4cloud.pigx.common.data.datascope.DataScope;
import com.pig4cloud.pigx.common.data.datascope.DataScopeMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -40,7 +40,7 @@ import java.util.List;
* @since 2017-10-29
*/
@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {
public interface SysUserMapper extends DataScopeMapper<SysUser> {
/**
* 通过用户名查询用户信息(含有角色信息)
+1 -1
View File
@@ -33,7 +33,7 @@
</organization>
<properties>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<spring-boot.version>2.3.1.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR5</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>