feat: 租户数据权限增加指定值

This commit is contained in:
qiufeng
2024-04-28 23:21:40 +08:00
parent 1694c045fa
commit 6bb386a32a
4 changed files with 6 additions and 10 deletions
@@ -1,7 +1,6 @@
package cn.com.mfish.common.ds.config;
import cn.com.mfish.common.core.exception.MyRuntimeException;
import cn.com.mfish.common.core.utils.StringUtils;
import cn.com.mfish.common.ds.annotation.DataScope;
import cn.com.mfish.common.ds.common.DataScopeUtils;
import lombok.extern.slf4j.Slf4j;
@@ -57,11 +56,7 @@ public class SelectInterceptor implements Interceptor {
if (!oriSql.toLowerCase().contains(tableName.toLowerCase())) {
continue;
}
if (!StringUtils.isEmpty(scope.fieldName())) {
oriSql = scope.type().getHandle().sqlChange(oriSql, tableName, scope.fieldName());
} else {
oriSql = scope.type().getHandle().sqlChange(oriSql, tableName, "");
}
oriSql = scope.type().getHandle().sqlChange(oriSql, tableName, scope.fieldName(), scope.value());
}
Field field = boundSql.getClass().getDeclaredField("sql");
field.setAccessible(true);
@@ -7,5 +7,5 @@ package cn.com.mfish.common.ds.scope;
*/
public interface DataScopeHandle {
String sqlChange(String sql, String table, String fieldName);
String sqlChange(String sql, String table, String fieldName, String value);
}
@@ -8,7 +8,7 @@ package cn.com.mfish.common.ds.scope;
public class RoleDataScopeHandle implements DataScopeHandle {
@Override
public String sqlChange(String sql, String table, String fieldName) {
public String sqlChange(String sql, String table, String fieldName,String value) {
//todo 补充逻辑
return sql;
}
@@ -14,13 +14,14 @@ import java.util.stream.Collectors;
*/
public class TenantDataScopeHandle implements DataScopeHandle {
@Override
public String sqlChange(String sql, String table, String fieldName) {
public String sqlChange(String sql, String table, String fieldName,String value) {
fieldName = StringUtils.isEmpty(fieldName) ? "tenant_id" : fieldName;
value = StringUtils.isEmpty(value) ? AuthInfoUtils.getCurrentTenantId() : value;
List<DataScopeUtils.SqlSplit> list = DataScopeUtils.splitSql(sql, table);
for (DataScopeUtils.SqlSplit split : list) {
if (split.isHaveTable()) {
split.setStatement(split.getStatement().replaceAll(table
, "(select * from " + table + " where " + fieldName + "='" + AuthInfoUtils.getCurrentTenantId() + "')"));
, "(select * from " + table + " where " + fieldName + "='" + value + "')"));
if (split.isHaveAlias()) {
continue;
}