|
|
@@ -1,7 +1,9 @@
|
|
|
package com.jzg.commons.config;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
|
|
import com.jzg.commons.core.base.BaseController;
|
|
|
+import com.jzg.commons.entity.properties.DataScopeProperties;
|
|
|
import org.apache.ibatis.executor.Executor;
|
|
|
import org.apache.ibatis.executor.statement.StatementHandler;
|
|
|
import org.apache.ibatis.mapping.MappedStatement;
|
|
|
@@ -10,13 +12,18 @@ import org.apache.ibatis.mapping.BoundSql;
|
|
|
import org.apache.ibatis.reflection.MetaObject;
|
|
|
import org.apache.ibatis.session.ResultHandler;
|
|
|
import org.apache.ibatis.session.RowBounds;
|
|
|
+import org.apache.ibatis.session.SqlSession;
|
|
|
+import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.sql.Connection;
|
|
|
import java.sql.SQLException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import java.util.Properties;
|
|
|
|
|
|
/**
|
|
|
@@ -27,40 +34,80 @@ import java.util.Properties;
|
|
|
// @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
|
|
|
})
|
|
|
public class DataPermissionInterceptor implements InnerInterceptor {
|
|
|
+
|
|
|
+ private List<String> ignoredTables;
|
|
|
+
|
|
|
+ private DataScopeProperties dataScopeProperties;
|
|
|
+
|
|
|
private final RedissonClient redissonClient;
|
|
|
|
|
|
private final BaseController baseController;
|
|
|
|
|
|
- public DataPermissionInterceptor(RedissonClient redissonClient,BaseController baseController){
|
|
|
+ private SqlSessionFactory sqlSessionFactory;
|
|
|
+
|
|
|
+ public DataPermissionInterceptor(RedissonClient redissonClient, BaseController baseController,
|
|
|
+ DataScopeProperties dataScopeProperties){
|
|
|
this.redissonClient = redissonClient;
|
|
|
this.baseController = baseController;
|
|
|
+ this.dataScopeProperties = dataScopeProperties;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private boolean shouldIgnoreTable(String sql) {
|
|
|
+ for (String table : dataScopeProperties.getTables()) {
|
|
|
+ if (sql.toLowerCase().contains(table.toLowerCase())) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
|
|
|
-// try {
|
|
|
-// String currentUserDeptId = baseController.getUserDeptId();
|
|
|
-// // 获取原始 SQL
|
|
|
-// String sql = boundSql.getSql();
|
|
|
-//
|
|
|
-// if (sql.toLowerCase().contains("select")) {
|
|
|
-// sql += " AND dept_id = '" + currentUserDeptId + "'";
|
|
|
-// Field field = null;
|
|
|
-// try {
|
|
|
-// field = BoundSql.class.getDeclaredField("sql");
|
|
|
-// } catch (NoSuchFieldException e) {
|
|
|
-// throw new RuntimeException(e);
|
|
|
-// }
|
|
|
-// field.setAccessible(true);
|
|
|
-// try {
|
|
|
-// field.set(boundSql, sql);
|
|
|
-// } catch (IllegalAccessException e) {
|
|
|
-// throw new RuntimeException(e);
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }catch (Exception e){
|
|
|
-//
|
|
|
-// }
|
|
|
|
|
|
+ try {
|
|
|
+ if(shouldIgnoreTable(boundSql.getSql())) {
|
|
|
+ String currentUserDeptId = baseController.getUserDeptId();
|
|
|
+ String userDataScope = baseController.getUserDataScope();
|
|
|
+ JSONObject dataScope = JSONObject.parseObject(userDataScope);
|
|
|
+ List<String> deptIds = new ArrayList<>();
|
|
|
+ StringBuilder additionalSql = new StringBuilder();
|
|
|
+ if (dataScope.getString("dataMark").equals("2")) {
|
|
|
+ additionalSql.append(" AND dept_id in (");
|
|
|
+ additionalSql.append("select id from sys_dept where route like '"+currentUserDeptId+"%'");
|
|
|
+ additionalSql.append(")");
|
|
|
+ }else {
|
|
|
+ deptIds = dataScope.getObject("deptIds", List.class);
|
|
|
+ additionalSql.append(" AND dept_id in (");
|
|
|
+ deptIds.forEach(deptId -> {
|
|
|
+ additionalSql.append("'" + deptId + "',");
|
|
|
+ });
|
|
|
+ additionalSql.deleteCharAt(additionalSql.length() - 1);
|
|
|
+ additionalSql.append(")");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取原始 SQL
|
|
|
+ String sql = boundSql.getSql();
|
|
|
+
|
|
|
+ if (sql.toLowerCase().contains("select")) {
|
|
|
+ sql += additionalSql.toString();
|
|
|
+ Field field = null;
|
|
|
+ try {
|
|
|
+ field = BoundSql.class.getDeclaredField("sql");
|
|
|
+ } catch (NoSuchFieldException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ field.setAccessible(true);
|
|
|
+ try {
|
|
|
+ field.set(boundSql, sql);
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ System.out.printf(e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
}
|