Commit 1c1540b1 authored by tangwei's avatar tangwei

解决冲突

parents 8ae9d10b 118470c4
...@@ -26,6 +26,7 @@ import org.typroject.tyboot.core.restful.exception.instance.BadRequest; ...@@ -26,6 +26,7 @@ import org.typroject.tyboot.core.restful.exception.instance.BadRequest;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.sql.Connection; import java.sql.Connection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
...@@ -45,6 +46,8 @@ public class UserEmpowerInterceptor implements Interceptor { ...@@ -45,6 +46,8 @@ public class UserEmpowerInterceptor implements Interceptor {
@Override @Override
public Object intercept(Invocation invocation) throws Throwable { public Object intercept(Invocation invocation) throws Throwable {
try { try {
StdUserEmpower orgCode =(StdUserEmpower) redisUtils.get("Emp_"+RedisKey.buildReginKey(RequestContext.getExeUserId(), RequestContext.getToken())); StdUserEmpower orgCode =(StdUserEmpower) redisUtils.get("Emp_"+RedisKey.buildReginKey(RequestContext.getExeUserId(), RequestContext.getToken()));
...@@ -123,9 +126,78 @@ public class UserEmpowerInterceptor implements Interceptor { ...@@ -123,9 +126,78 @@ public class UserEmpowerInterceptor implements Interceptor {
//经销商 //经销商
StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
//获取方法注解
Method method = getTargetDataAuthMethodJXS(mappedStatement);
DealerRestrict dealerRestrict = getTargetDataAuthAnnotationJXS(mappedStatement);
if (dealerRestrict == null) {
return invocation.proceed();
}
//获取字段
String[] filed = dealerRestrict.field();
//获取字段条件表达式
String[] fileCondition = dealerRestrict.fieldConditions();
//获取 参数之间关系
String fileBetweenCondition = dealerRestrict.relationship();
//获取参数值,
StdUserEmpower dataAuthRule =(StdUserEmpower) redisUtils.get("Emp_"+RedisKey.buildReginKey(RequestContext.getExeUserId(), RequestContext.getToken()));
String[] data = new String[]{String.join(",",dataAuthRule.getAmosOrgCode()),dataAuthRule.getRegionalCompaniesCode(),dataAuthRule.getUserId(),dataAuthRule.getAdminRegionalCompaniesCode()};
BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
List<String> sq ;
//获取sql
String sql = boundSql.getSql();
List<String> fileds = Arrays.asList(filed);
if (!ValidationUtil.isEmpty(dataAuthRule.getAdminRegionalCompaniesCode())) {
sq = selectSqlJXS(new String[]{fileds.get(0), fileds.get(1)}, fileCondition, Arrays.asList(data));
} else if (!ValidationUtil.isEmpty(dataAuthRule.getUserId())) {
sq = selectSqlJXS(new String[]{fileds.get(0), fileds.get(2)}, fileCondition, Arrays.asList(data));
} else {
sq = selectSqlJXS(new String[]{fileds.get(0), fileds.get(1)}, fileCondition, Arrays.asList(data));
}
String sqldata = " ";
if (sq != null && sq.size() > 0) {
if (fileCondition.length == 1) {
sqldata = sqldata + sq.get(0);
} else {
if (fileBetweenCondition != null && !fileBetweenCondition.isEmpty()) {
for (int i = 0; i < sq.size(); i++) {
if (i == sq.size() - 1) {
sqldata = sqldata + sq.get(i);
} else {
sqldata = sqldata + sq.get(i) + " " + fileBetweenCondition + " ";
}
}
} else {
sqldata = " ";
}
}
}
Select select = (Select) CCJSqlParserUtil.parse(sql);
PlainSelect selectBody = (PlainSelect) select.getSelectBody();
if (!ValidationUtil.isEmpty(sqldata.trim())) {
} else {
sqldata = " 1= 2 ";
}
if (ValidationUtil.isEmpty(selectBody.getWhere())) {
selectBody.setWhere(CCJSqlParserUtil.parseCondExpression(sqldata));
} else {
AndExpression andExpr = new AndExpression(selectBody.getWhere(), CCJSqlParserUtil.parseCondExpression(sqldata));
selectBody.setWhere(andExpr);
}
System.out.println(selectBody.toString());
metaObject.setValue("delegate.boundSql.sql", selectBody.toString());
} }
...@@ -426,4 +498,185 @@ public class UserEmpowerInterceptor implements Interceptor { ...@@ -426,4 +498,185 @@ public class UserEmpowerInterceptor implements Interceptor {
return getTargetDataAuthMethod(mappedStatement).getAnnotation(UserEmpower.class); return getTargetDataAuthMethod(mappedStatement).getAnnotation(UserEmpower.class);
} }
private List<String> selectSqlJXS(String[] filed, String[] fileCondition, List<String> data) {
List<String> sql = new ArrayList<>();
if (filed != null && filed.length > 0 && fileCondition != null && fileCondition.length > 0 && data != null && data.size() > 0) {
for (int i = 0; i < filed.length; i++) {
String sq = " ";
sq = getConditionJXS(filed[i], fileCondition[i], data);
sql.add(sq);
}
}
return sql;
}
private String getConditionJXS(String filed, String type, List<String> data) {
String sql = " ";
switch (type) {
case "in":
sql = sql + getInDataJXS(filed, data);
break;
case "like":
if (data.size() == 1) {
sql = sql + getlikeDataJXS(filed, data);
} else {
}
break;
case "likeLeft":
if (data.size() == 1) {
sql = sql + getlikeLeftDataJXS(filed, data);
} else {
}
break;
case "likeRight":
if (data.size() == 1) {
sql = sql + getlikeRightDataJXS(filed, data);
} else {
}
break;
case "eq":
if (data.size() == 1) {
sql = sql + getDataJXS(filed, data);
} else {
}
break;
}
return sql;
}
private String getInDataJXS(String filed, List<String> data) {
String sql = " ( ";
for (int i = 0; i < data.size(); i++) {
if (i == data.size() - 1) {
sql = sql + "'" + data.get(i) + "' ) ";
} else {
sql = sql + "'" + data.get(i) + "',";
}
}
return filed + " in " + sql;
}
private String getDataJXS(String filed, List<String> data) {
String sql = "";
if (data.size() == 1) {
sql = sql + filed + " = " + data.get(0);
} else {
for (int i = 0; i < data.size(); i++) {
if (i == data.size() - 1) {
sql = sql + filed + " = " + data.get(i) + " ";
} else {
sql = sql + filed + " = " + data.get(i) + " and ";
;
}
}
}
return sql;
}
private String getlikeRightDataJXS(String filed, List<String> data) {
String sql = "";
if (data.size() == 1) {
sql = sql + filed + " = " + data.get(0) + "%";
} else {
for (int i = 0; i < data.size(); i++) {
if (i == data.size() - 1) {
sql = sql + filed + " = " + data.get(i) + "%" + " ";
} else {
sql = sql + filed + " = " + data.get(i) + "%" + " and ";
}
}
}
return sql;
}
private String getlikeLeftDataJXS(String filed, List<String> data) {
String sql = "";
if (data.size() == 1) {
sql = sql + filed + " = " + "%" + data.get(0);
} else {
for (int i = 0; i < data.size(); i++) {
if (i == data.size() - 1) {
sql = sql + filed + " = " + "%" + data.get(i) + " ";
} else {
sql = sql + filed + " = " + "%" + data.get(i) + " and ";
}
}
}
return sql;
}
private String getlikeDataJXS(String filed, List<String> data) {
String sql = "";
if (data.size() == 1) {
sql = sql + filed + " = " + "%" + data.get(0) + "%";
} else {
for (int i = 0; i < data.size(); i++) {
if (i == data.size() - 1) {
sql = sql + filed + " = " + "%" + data.get(i) + "%" + " ";
} else {
sql = sql + filed + " = " + "%" + data.get(i) + "%" + " and ";
}
}
}
return sql;
}
/**
* 获取当前添加数据权限DataAuth的执行语句对应mapper方法
*
* @param mappedStatement
* @return
* @throws ClassNotFoundException
*/
private Method getTargetDataAuthMethodJXS(MappedStatement mappedStatement) throws ClassNotFoundException {
String id = mappedStatement.getId();
String className = id.substring(0, id.lastIndexOf("."));
String methodName = id.substring(id.lastIndexOf(".") + 1);
final Class<?> cls = Class.forName(className);
final Method[] methods = cls.getMethods();
for (Method method : methods) {
// TODO 后续重载方法需要优化
if (method.getName().equals(methodName) && method.isAnnotationPresent(DealerRestrict.class)) {
return method;
}
}
return null;
}
/**
* 获取当前执行语句对应mapper方法的DataAuth注解
*
* @param mappedStatement
* @return
* @throws ClassNotFoundException
*/
private DealerRestrict getTargetDataAuthAnnotationJXS(MappedStatement mappedStatement) throws ClassNotFoundException {
if (ValidationUtil.isEmpty(getTargetDataAuthMethodJXS(mappedStatement))) {
return null;
}
return getTargetDataAuthMethodJXS(mappedStatement).getAnnotation(DealerRestrict.class);
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment