Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/example-app/example-app-cmd-domain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-app</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-app/example-app-cmd-meta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-app</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-app/example-app-query/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-app</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-example</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>
<relativePath>../pom.xml</relativePath>
</parent>
<packaging>pom</packaging>
Expand Down
2 changes: 1 addition & 1 deletion example/example-domain/example-domain-leave/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-domain</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-domain/example-domain-user/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-domain</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-domain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-example</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion example/example-infra/example-infra-flow/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-infra</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-infra/example-infra-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-infra</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-infra/example-infra-security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-infra</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-infra/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-example</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>
<relativePath>../pom.xml</relativePath>
</parent>
<packaging>pom</packaging>
Expand Down
2 changes: 1 addition & 1 deletion example/example-interface/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-example</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>
</parent>

<artifactId>example-interface</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion example/example-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-example</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.4.36</version>
<version>3.4.37</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</parent>

<artifactId>springboot-example</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>

<name>springboot-example</name>
<description>springboot-example project for Spring Boot</description>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>

<url>https://github.com/codingapi/springboot-framewrok</url>
<name>springboot-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion springboot-starter-data-authorization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>3.4.36</version>
<version>3.4.37</version>
</parent>

<name>springboot-starter-data-authorization</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.codingapi.springboot.authorization.condition;

public interface IConditionSQL {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.codingapi.springboot.authorization.condition;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.schema.Table;
import org.springframework.util.StringUtils;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class JoinConditionSQL implements IConditionSQL {

private Type type;
private String tableName;
private String tableAlias;
private String onCondition;



public static enum Type{
INNER,
LEFT,
RIGHT
}


public Table toJoinTable(){
if(StringUtils.hasText(tableAlias)){
Table table = new Table(this.tableName);
table.setAlias(new Alias(tableAlias));
return table;
}else {
return new Table(this.tableName);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.codingapi.springboot.authorization.condition;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class WhereConditionSQL implements IConditionSQL {

private String condition;

public WhereConditionSQL(String condition,Object... args){
this.condition = String.format(condition, args);
}

}
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
package com.codingapi.springboot.authorization.enhancer;


import com.codingapi.springboot.authorization.condition.IConditionSQL;
import com.codingapi.springboot.authorization.enhancer.handler.ConditionSQLHandlerContext;
import com.codingapi.springboot.authorization.handler.Condition;
import com.codingapi.springboot.authorization.handler.RowHandler;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
import net.sf.jsqlparser.expression.SignedExpression;
import net.sf.jsqlparser.expression.operators.arithmetic.Subtraction;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.expression.operators.relational.LikeExpression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.util.TablesNamesFinder;

import java.sql.SQLException;
import java.util.List;
import java.util.Set;

/**
* 数据权限 SQL 增强器
Expand Down Expand Up @@ -162,12 +156,9 @@ private void injectDataPermissionCondition(PlainSelect plainSelect, Table table,
String aliaName = table.getAlias() != null ? table.getAlias().getName() : tableName;
Condition condition = rowHandler.handler(plainSelect.toString(), tableName, aliaName);
if (condition != null) {
// 添加自定义条件
Expression customExpression = CCJSqlParserUtil.parseCondExpression(condition.getCondition());
if (where != null) {
plainSelect.setWhere(new AndExpression(customExpression, where));
} else {
plainSelect.setWhere(customExpression);
List<IConditionSQL> conditionList = condition.getConditionList();
for (IConditionSQL conditionSQL:conditionList) {
ConditionSQLHandlerContext.getInstance().handler(conditionSQL,plainSelect,table,where);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.codingapi.springboot.authorization.enhancer.handler;

import com.codingapi.springboot.authorization.condition.IConditionSQL;
import lombok.Getter;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.PlainSelect;

import java.util.ArrayList;
import java.util.List;

public class ConditionSQLHandlerContext {

private final List<IConditionSQLHandler> handlers;

@Getter
private final static ConditionSQLHandlerContext instance = new ConditionSQLHandlerContext();

private ConditionSQLHandlerContext() {
this.handlers = new ArrayList<>();
this.registerHandlers();
}


private void registerHandlers(){
this.handlers.add(new WhereConditionSQLHandler());
this.handlers.add(new JoinConditionSQLHandler());
}


public void handler(IConditionSQL dynamicSQL, PlainSelect plainSelect, Table table, Expression where) {
for (IConditionSQLHandler handler:handlers){
if (handler.support(dynamicSQL)){
handler.handler(dynamicSQL,plainSelect, table, where);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.codingapi.springboot.authorization.enhancer.handler;

import com.codingapi.springboot.authorization.condition.IConditionSQL;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.PlainSelect;

public interface IConditionSQLHandler {

boolean support(IConditionSQL conditionSQL);

void handler(IConditionSQL conditionSQL, PlainSelect plainSelect, Table table, Expression where);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.codingapi.springboot.authorization.enhancer.handler;

import com.codingapi.springboot.authorization.condition.IConditionSQL;
import com.codingapi.springboot.authorization.condition.JoinConditionSQL;
import lombok.SneakyThrows;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.PlainSelect;

import java.util.ArrayList;
import java.util.Collection;

public class JoinConditionSQLHandler implements IConditionSQLHandler {

@Override
public boolean support(IConditionSQL conditionSQL) {
return conditionSQL instanceof JoinConditionSQL;
}

@SneakyThrows
@Override
public void handler(IConditionSQL conditionSQL, PlainSelect plainSelect, Table table, Expression where) {
JoinConditionSQL joinConditionSQL = (JoinConditionSQL) conditionSQL;

if(plainSelect.getJoins()==null){
plainSelect.setJoins(new ArrayList<>());
}

Join join = new Join();
join.setFromItem(joinConditionSQL.toJoinTable());

if(joinConditionSQL.getType()== JoinConditionSQL.Type.INNER){
join.setInner(true);
}

if(joinConditionSQL.getType()== JoinConditionSQL.Type.RIGHT){
join.setRight(true);
}

if(joinConditionSQL.getType()== JoinConditionSQL.Type.LEFT){
join.setLeft(true);
}

Collection<Expression> expressions = new ArrayList<>();
Expression expression = CCJSqlParserUtil.parseCondExpression(joinConditionSQL.getOnCondition());
expressions.add(expression);
join.setOnExpressions(expressions);
plainSelect.addJoins(join);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.codingapi.springboot.authorization.enhancer.handler;

import com.codingapi.springboot.authorization.condition.IConditionSQL;
import com.codingapi.springboot.authorization.condition.WhereConditionSQL;
import lombok.SneakyThrows;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.PlainSelect;

public class WhereConditionSQLHandler implements IConditionSQLHandler {

@Override
public boolean support(IConditionSQL conditionSQL) {
return conditionSQL instanceof WhereConditionSQL;
}

@SneakyThrows
@Override
public void handler(IConditionSQL conditionSQL, PlainSelect plainSelect, Table table, Expression where) {
String whereSQL = ((WhereConditionSQL) conditionSQL).getCondition();
// 添加自定义条件
Expression customExpression = CCJSqlParserUtil.parseCondExpression(whereSQL);
if (where != null) {
plainSelect.setWhere(new AndExpression(customExpression, where));
} else {
plainSelect.setWhere(customExpression);
}
}
}
Loading