forked from fanruan/design
Henry.Wang
3 years ago
1 changed files with 57 additions and 41 deletions
@ -1,81 +1,97 @@ |
|||||||
package com.fr.design.mainframe.authority; |
package com.fr.design.mainframe.authority; |
||||||
|
|
||||||
import com.fr.base.Formula; |
import com.fr.base.Formula; |
||||||
import com.fr.stable.StringUtils; |
import com.fr.general.ComparatorUtils; |
||||||
|
import com.fr.parser.FunctionCall; |
||||||
|
import com.fr.parser.StringLiteral; |
||||||
|
import com.fr.script.Calculator; |
||||||
|
import com.fr.stable.script.Node; |
||||||
import org.jetbrains.annotations.Nullable; |
import org.jetbrains.annotations.Nullable; |
||||||
|
|
||||||
import java.util.Arrays; |
|
||||||
import java.util.HashSet; |
import java.util.HashSet; |
||||||
import java.util.Set; |
import java.util.Set; |
||||||
import java.util.regex.Matcher; |
|
||||||
import java.util.regex.Pattern; |
|
||||||
|
|
||||||
public class FormulaAuthorityChecker extends ElementAuthorityChecker<Formula> { |
public class FormulaAuthorityChecker extends ElementAuthorityChecker<Formula> { |
||||||
private static final Set<FormulaPattern> CONNECTION_NAME_FORMULA_PATTERN = new HashSet<>(); |
private static final Set<FormulaParser> CONNECTION_NAME_FORMULA_PARSER = new HashSet<>(); |
||||||
private static final Set<FormulaPattern> DATASET_NAME_FORMULA_PATTERN = new HashSet<>(); |
private static final Set<FormulaParser> DATASET_NAME_FORMULA_PARSER = new HashSet<>(); |
||||||
|
|
||||||
static { |
static { |
||||||
CONNECTION_NAME_FORMULA_PATTERN.add(new FormulaPattern("^=SQL\\(\"(.+?)\",", 1)); |
CONNECTION_NAME_FORMULA_PARSER.add(new FormulaParser("SQL", 0)); |
||||||
DATASET_NAME_FORMULA_PATTERN.add(new FormulaPattern("^=VALUE\\(\"(.+?)\",", 1)); |
DATASET_NAME_FORMULA_PARSER.add(new FormulaParser("VALUE", 0)); |
||||||
} |
} |
||||||
|
|
||||||
@Override |
@Override |
||||||
@Nullable |
@Nullable |
||||||
public Set<String> getNoAuthConnectionNames(Formula formula, Set<String> authConnectionNames) { |
public Set<String> getNoAuthConnectionNames(Formula formula, Set<String> authConnectionNames) { |
||||||
return getNoAuthNames(formula, CONNECTION_NAME_FORMULA_PATTERN, authConnectionNames); |
return getNoAuthNames(formula, CONNECTION_NAME_FORMULA_PARSER, authConnectionNames); |
||||||
} |
} |
||||||
|
|
||||||
|
|
||||||
@Override |
@Override |
||||||
@Nullable |
@Nullable |
||||||
Set<String> getNoAuthDatasetNames(Formula formula, Set<String> authDatasetNames) { |
Set<String> getNoAuthDatasetNames(Formula formula, Set<String> authDatasetNames) { |
||||||
return getNoAuthNames(formula, DATASET_NAME_FORMULA_PATTERN, authDatasetNames); |
return getNoAuthNames(formula, DATASET_NAME_FORMULA_PARSER, authDatasetNames); |
||||||
} |
} |
||||||
|
|
||||||
private Set<String> getNoAuthNames(Formula formula, Set<FormulaPattern> formulaPatterns, Set<String> authNames) { |
private Set<String> getNoAuthNames(Formula formula, Set<FormulaParser> formulaParsers, Set<String> authNames) { |
||||||
Set<String> noAuthNames = new HashSet<>(); |
Set<String> noAuthNames = new HashSet<>(); |
||||||
for (FormulaPattern formulaPattern : formulaPatterns) { |
try { |
||||||
String noAuthName = getNoAuthName(formula, formulaPattern, authNames); |
FunctionCall functionCall = (FunctionCall) Calculator.createCalculator().parse(formula.getContent()).getConditionalExpression(); |
||||||
|
handleNoAuthNames(functionCall, formulaParsers, authNames, noAuthNames); |
||||||
|
} catch (Exception ignore) { |
||||||
|
|
||||||
|
} finally { |
||||||
|
return noAuthNames; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void handleNoAuthNames(FunctionCall functionCall, Set<FormulaParser> formulaParsers, Set<String> authNames, Set<String> noAuthNames) { |
||||||
|
for (FormulaParser formulaPattern : formulaParsers) { |
||||||
|
String noAuthName = formulaPattern.getNoAuthName(functionCall, authNames); |
||||||
if (noAuthName != null) { |
if (noAuthName != null) { |
||||||
noAuthNames.add(noAuthName); |
noAuthNames.add(noAuthName); |
||||||
} |
} |
||||||
} |
} |
||||||
return noAuthNames; |
Node[] nodes = functionCall.getArguments(); |
||||||
} |
if (nodes != null) { |
||||||
|
for (int i = 0; i < nodes.length; i++) { |
||||||
private String getNoAuthName(Formula formula, FormulaPattern formulaPattern, Set<String> authNames) { |
Node node = nodes[i]; |
||||||
String parameter = getFormulaParameter(formula, formulaPattern); |
if (node instanceof FunctionCall) { |
||||||
if (!authNames.contains(parameter)) { |
handleNoAuthNames((FunctionCall) node, formulaParsers, authNames, noAuthNames); |
||||||
return parameter; |
} |
||||||
|
} |
||||||
} |
} |
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
private String getFormulaParameter(Formula formula, FormulaPattern formulaPattern) { |
|
||||||
String content = transformFormulaContent(formula.getContent()); |
|
||||||
Matcher matcher = formulaPattern.pattern.matcher(content); |
|
||||||
if (matcher.find()) { |
|
||||||
return matcher.group(formulaPattern.index); |
|
||||||
} |
|
||||||
return null; |
|
||||||
} |
} |
||||||
|
|
||||||
private String transformFormulaContent(String content) { |
|
||||||
int index = content.indexOf('('); |
|
||||||
if (index > 0) { |
|
||||||
return content.substring(0, index).toUpperCase() + content.substring(index); |
|
||||||
} else { |
|
||||||
return StringUtils.EMPTY; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
static class FormulaPattern { |
static class FormulaParser { |
||||||
public Pattern pattern; |
//函数的名称
|
||||||
|
public String name; |
||||||
|
//要检测的位置
|
||||||
public int index; |
public int index; |
||||||
|
|
||||||
FormulaPattern(String regex, int index) { |
|
||||||
this.pattern = Pattern.compile(regex); |
FormulaParser(String name, int index) { |
||||||
|
this.name = name; |
||||||
this.index = index; |
this.index = index; |
||||||
} |
} |
||||||
|
|
||||||
|
String getNoAuthName(FunctionCall functionCall, Set<String> authNames) { |
||||||
|
if (functionCall.getName() != null && ComparatorUtils.equals(functionCall.getName().toUpperCase(), name)) { |
||||||
|
Node node = functionCall.getArguments()[index]; |
||||||
|
if (node instanceof StringLiteral) { |
||||||
|
String stringLiteral = node.toString(); |
||||||
|
if (stringLiteral.length() > 2) { |
||||||
|
String value = stringLiteral.substring(1, stringLiteral.length() - 1); |
||||||
|
if (!authNames.contains(value)) { |
||||||
|
return value; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue