|
@@ -15,10 +15,13 @@
|
|
|
*/
|
|
|
package com.baomidou.mybatisplus.extension.plugins.inner;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
|
|
import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport;
|
|
|
+import net.sf.jsqlparser.expression.BinaryExpression;
|
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
|
import net.sf.jsqlparser.expression.Parenthesis;
|
|
|
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
|
@@ -57,22 +60,30 @@ public class BlockAttackInnerInterceptor extends JsqlParserSupport implements In
|
|
|
|
|
|
@Override
|
|
|
protected void processDelete(Delete delete, int index, String sql, Object obj) {
|
|
|
- this.checkWhere(delete.getWhere(), "Prohibition of full table deletion");
|
|
|
+ this.checkWhere(delete.getTable().getName(), delete.getWhere(), "Prohibition of full table deletion");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void processUpdate(Update update, int index, String sql, Object obj) {
|
|
|
- this.checkWhere(update.getWhere(), "Prohibition of table update operation");
|
|
|
+ this.checkWhere(update.getTable().getName(), update.getWhere(), "Prohibition of table update operation");
|
|
|
}
|
|
|
|
|
|
- protected void checkWhere(Expression where, String ex) {
|
|
|
- Assert.isFalse(this.fullMatch(where), ex);
|
|
|
+ protected void checkWhere(String tableName, Expression where, String ex) {
|
|
|
+ Assert.isFalse(this.fullMatch(where, this.getTableLogicField(tableName)), ex);
|
|
|
}
|
|
|
|
|
|
- private boolean fullMatch(Expression where) {
|
|
|
+ private boolean fullMatch(Expression where, String logicField) {
|
|
|
if (where == null) {
|
|
|
return true;
|
|
|
}
|
|
|
+ if (StringUtils.isNotBlank(logicField) && (where instanceof BinaryExpression)) {
|
|
|
+
|
|
|
+ BinaryExpression binaryExpression = (BinaryExpression) where;
|
|
|
+ if (StringUtils.equals(binaryExpression.getLeftExpression().toString(), logicField) || StringUtils.equals(binaryExpression.getRightExpression().toString(), logicField)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if (where instanceof EqualsTo) {
|
|
|
// example: 1=1
|
|
|
EqualsTo equalsTo = (EqualsTo) where;
|
|
@@ -84,17 +95,35 @@ public class BlockAttackInnerInterceptor extends JsqlParserSupport implements In
|
|
|
} else if (where instanceof OrExpression) {
|
|
|
|
|
|
OrExpression orExpression = (OrExpression) where;
|
|
|
- return fullMatch(orExpression.getLeftExpression()) || fullMatch(orExpression.getRightExpression());
|
|
|
+ return fullMatch(orExpression.getLeftExpression(), logicField) || fullMatch(orExpression.getRightExpression(), logicField);
|
|
|
} else if (where instanceof AndExpression) {
|
|
|
|
|
|
AndExpression andExpression = (AndExpression) where;
|
|
|
- return fullMatch(andExpression.getLeftExpression()) && fullMatch(andExpression.getRightExpression());
|
|
|
+ return fullMatch(andExpression.getLeftExpression(), logicField) && fullMatch(andExpression.getRightExpression(), logicField);
|
|
|
} else if (where instanceof Parenthesis) {
|
|
|
// example: (1 = 1)
|
|
|
Parenthesis parenthesis = (Parenthesis) where;
|
|
|
- return fullMatch(parenthesis.getExpression());
|
|
|
+ return fullMatch(parenthesis.getExpression(), logicField);
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取表名中的逻辑删除字段
|
|
|
+ *
|
|
|
+ * @param tableName 表名
|
|
|
+ * @return 逻辑删除字段
|
|
|
+ */
|
|
|
+ private String getTableLogicField(String tableName) {
|
|
|
+ if (StringUtils.isBlank(tableName)) {
|
|
|
+ return StringUtils.EMPTY;
|
|
|
+ }
|
|
|
+
|
|
|
+ TableInfo tableInfo = TableInfoHelper.getTableInfo(tableName);
|
|
|
+ if (tableInfo == null || !tableInfo.isWithLogicDelete() || tableInfo.getLogicDeleteFieldInfo() == null) {
|
|
|
+ return StringUtils.EMPTY;
|
|
|
+ }
|
|
|
+ return tableInfo.getLogicDeleteFieldInfo().getColumn();
|
|
|
+ }
|
|
|
}
|