提交 | 用户 | 时间
|
722af2
|
1 |
package com.dl.framework.interceptor; |
X |
2 |
|
|
3 |
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; |
|
4 |
import com.baomidou.mybatisplus.core.toolkit.PluginUtils; |
|
5 |
import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport; |
|
6 |
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; |
|
7 |
import com.dl.framework.handler.PlusDataPermissionHandler; |
|
8 |
import net.sf.jsqlparser.expression.Expression; |
|
9 |
import net.sf.jsqlparser.statement.delete.Delete; |
|
10 |
import net.sf.jsqlparser.statement.select.PlainSelect; |
|
11 |
import net.sf.jsqlparser.statement.select.Select; |
|
12 |
import net.sf.jsqlparser.statement.select.SelectBody; |
|
13 |
import net.sf.jsqlparser.statement.select.SetOperationList; |
|
14 |
import net.sf.jsqlparser.statement.update.Update; |
|
15 |
import org.apache.ibatis.executor.Executor; |
|
16 |
import org.apache.ibatis.executor.statement.StatementHandler; |
|
17 |
import org.apache.ibatis.mapping.BoundSql; |
|
18 |
import org.apache.ibatis.mapping.MappedStatement; |
|
19 |
import org.apache.ibatis.mapping.SqlCommandType; |
|
20 |
import org.apache.ibatis.session.ResultHandler; |
|
21 |
import org.apache.ibatis.session.RowBounds; |
|
22 |
|
|
23 |
import java.sql.Connection; |
|
24 |
import java.sql.SQLException; |
|
25 |
import java.util.List; |
|
26 |
|
|
27 |
/** |
|
28 |
* 数据权限拦截器 |
|
29 |
* |
|
30 |
* @author Lion Li |
|
31 |
* @version 3.5.0 |
|
32 |
*/ |
|
33 |
public class PlusDataPermissionInterceptor extends JsqlParserSupport implements InnerInterceptor { |
|
34 |
|
|
35 |
private final PlusDataPermissionHandler dataPermissionHandler = new PlusDataPermissionHandler(); |
|
36 |
|
|
37 |
@Override |
|
38 |
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { |
|
39 |
// 检查忽略注解 |
|
40 |
if (InterceptorIgnoreHelper.willIgnoreDataPermission(ms.getId())) { |
|
41 |
return; |
|
42 |
} |
|
43 |
// 检查是否无效 无数据权限注解 |
|
44 |
if (dataPermissionHandler.isInvalid(ms.getId())) { |
|
45 |
return; |
|
46 |
} |
|
47 |
// 解析 sql 分配对应方法 |
|
48 |
PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); |
|
49 |
mpBs.sql(parserSingle(mpBs.sql(), ms.getId())); |
|
50 |
} |
|
51 |
|
|
52 |
@Override |
|
53 |
public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) { |
|
54 |
PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh); |
|
55 |
MappedStatement ms = mpSh.mappedStatement(); |
|
56 |
SqlCommandType sct = ms.getSqlCommandType(); |
|
57 |
if (sct == SqlCommandType.UPDATE || sct == SqlCommandType.DELETE) { |
|
58 |
if (InterceptorIgnoreHelper.willIgnoreDataPermission(ms.getId())) { |
|
59 |
return; |
|
60 |
} |
|
61 |
PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql(); |
|
62 |
mpBs.sql(parserMulti(mpBs.sql(), ms.getId())); |
|
63 |
} |
|
64 |
} |
|
65 |
|
|
66 |
@Override |
|
67 |
protected void processSelect(Select select, int index, String sql, Object obj) { |
|
68 |
SelectBody selectBody = select.getSelectBody(); |
|
69 |
if (selectBody instanceof PlainSelect) { |
|
70 |
this.setWhere((PlainSelect) selectBody, (String) obj); |
|
71 |
} else if (selectBody instanceof SetOperationList) { |
|
72 |
SetOperationList setOperationList = (SetOperationList) selectBody; |
|
73 |
List<SelectBody> selectBodyList = setOperationList.getSelects(); |
|
74 |
selectBodyList.forEach(s -> this.setWhere((PlainSelect) s, (String) obj)); |
|
75 |
} |
|
76 |
} |
|
77 |
|
|
78 |
@Override |
|
79 |
protected void processUpdate(Update update, int index, String sql, Object obj) { |
|
80 |
Expression sqlSegment = dataPermissionHandler.getSqlSegment(update.getWhere(), (String) obj, false); |
|
81 |
if (null != sqlSegment) { |
|
82 |
update.setWhere(sqlSegment); |
|
83 |
} |
|
84 |
} |
|
85 |
|
|
86 |
@Override |
|
87 |
protected void processDelete(Delete delete, int index, String sql, Object obj) { |
|
88 |
Expression sqlSegment = dataPermissionHandler.getSqlSegment(delete.getWhere(), (String) obj, false); |
|
89 |
if (null != sqlSegment) { |
|
90 |
delete.setWhere(sqlSegment); |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
/** |
|
95 |
* 设置 where 条件 |
|
96 |
* |
|
97 |
* @param plainSelect 查询对象 |
|
98 |
* @param mappedStatementId 执行方法id |
|
99 |
*/ |
|
100 |
protected void setWhere(PlainSelect plainSelect, String mappedStatementId) { |
|
101 |
Expression sqlSegment = dataPermissionHandler.getSqlSegment(plainSelect.getWhere(), mappedStatementId, true); |
|
102 |
if (null != sqlSegment) { |
|
103 |
plainSelect.setWhere(sqlSegment); |
|
104 |
} |
|
105 |
} |
|
106 |
|
|
107 |
} |
|
108 |
|