优化数据权限

This commit is contained in:
阿沐 2024-02-27 18:44:52 +08:00
parent 20bcefc5f4
commit 9da2bd8524
2 changed files with 109 additions and 2 deletions

View File

@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.PlainSelect;
@ -73,7 +72,7 @@ public class DataScopeInnerInterceptor implements InnerInterceptor {
plainSelect.setWhere(andExpression);
}
return select.toString().replaceAll("'", "");
return select.toString();
} catch (JSQLParserException e) {
return buildSql;
}

View File

@ -0,0 +1,108 @@
package net.maku.framework.mybatis.interceptor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class StringValue extends ASTNodeAccessImpl implements Expression {
private String value = "";
private String prefix = null;
public static final List<String> ALLOWED_PREFIXES = Arrays.asList("N", "U", "E", "R", "B", "RB", "_utf8");
public StringValue() {
// empty constructor
}
public StringValue(String escapedValue) {
// removing "'" at the start and at the end
if (escapedValue.startsWith("'") && escapedValue.endsWith("'")) {
value = escapedValue.substring(1, escapedValue.length() - 1);
return;
}
if (escapedValue.length() > 2) {
for (String p : ALLOWED_PREFIXES) {
if (escapedValue.length() > p.length() && escapedValue.substring(0, p.length()).equalsIgnoreCase(p)
&& escapedValue.charAt(p.length()) == '\'') {
this.prefix = p;
value = escapedValue.substring(p.length() + 1, escapedValue.length() - 1);
return;
}
}
}
value = escapedValue;
}
public String getValue() {
return value;
}
public String getPrefix() {
return prefix;
}
public String getNotExcapedValue() {
StringBuilder buffer = new StringBuilder(value);
int index = 0;
int deletesNum = 0;
while ((index = value.indexOf("''", index)) != -1) {
buffer.deleteCharAt(index - deletesNum);
index += 2;
deletesNum++;
}
return buffer.toString();
}
public void setValue(String string) {
value = string;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
@Override
public void accept(ExpressionVisitor expressionVisitor) {
// expressionVisitor.visit(this);
}
@Override
public String toString() {
return (prefix != null ? prefix : "") + value;
}
public StringValue withPrefix(String prefix) {
this.setPrefix(prefix);
return this;
}
public StringValue withValue(String value) {
this.setValue(value);
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
StringValue that = (StringValue) o;
return Objects.equals(value, that.value) && Objects.equals(prefix, that.prefix);
}
@Override
public int hashCode() {
return Objects.hash(value, prefix);
}
}