mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2026-05-12 06:42:08 +08:00
update 优化 项目中的一些存在null的问题 与一些性能问题 小优化
This commit is contained in:
@@ -33,7 +33,7 @@ public enum DataScopeType {
|
||||
/**
|
||||
* 自定数据权限
|
||||
*/
|
||||
CUSTOM("2", " #{#deptName} IN ( #{@sdss.getRoleCustom( #user.roleId )} ) ", " 1 = 0 "),
|
||||
CUSTOM("2", " #{#deptName} IN ( #{@sdss.getRoleCustom( #roleId )} ) ", " 1 = 0 "),
|
||||
|
||||
/**
|
||||
* 部门数据权限
|
||||
|
||||
@@ -17,6 +17,7 @@ import java.util.Date;
|
||||
* MP注入处理器
|
||||
*
|
||||
* @author Lion Li
|
||||
* @date 2021/4/25
|
||||
*/
|
||||
@Slf4j
|
||||
public class InjectionMetaObjectHandler implements MetaObjectHandler {
|
||||
@@ -62,7 +63,7 @@ public class InjectionMetaObjectHandler implements MetaObjectHandler {
|
||||
this.strictInsertFill(metaObject, "updateTime", Date.class, date);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
|
||||
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_INTERNAL_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,17 +81,14 @@ public class InjectionMetaObjectHandler implements MetaObjectHandler {
|
||||
baseEntity.setUpdateTime(current);
|
||||
|
||||
// 获取当前登录用户的ID,并填充更新人信息
|
||||
Long userId = LoginHelper.getUserId();
|
||||
if (ObjectUtil.isNotNull(userId)) {
|
||||
baseEntity.setUpdateBy(userId);
|
||||
} else {
|
||||
baseEntity.setUpdateBy(DEFAULT_USER_ID);
|
||||
}
|
||||
LoginUser loginUser = getLoginUser();
|
||||
Long userId = ObjectUtil.isNotNull(loginUser) ? loginUser.getUserId() : DEFAULT_USER_ID;
|
||||
baseEntity.setUpdateBy(userId);
|
||||
} else {
|
||||
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
|
||||
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_INTERNAL_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ public class PlusDataPermissionHandler {
|
||||
public Expression getSqlSegment(Expression where, boolean isSelect) {
|
||||
try {
|
||||
LoginUser currentUser = currentUser();
|
||||
// 如果是超级管理员,则不过滤数据
|
||||
// 如果是超级管理员或租户管理员,则不过滤数据
|
||||
if (LoginHelper.isSuperAdmin()) {
|
||||
return where;
|
||||
}
|
||||
@@ -134,7 +134,7 @@ public class PlusDataPermissionHandler {
|
||||
}
|
||||
|
||||
for (RoleDTO role : scopeRoles) {
|
||||
user.setRoleId(role.getRoleId());
|
||||
context.setVariable("roleId", role.getRoleId());
|
||||
// 获取角色权限泛型
|
||||
DataScopeType type = DataScopeType.findCode(role.getDataScope());
|
||||
if (ObjectUtil.isNull(type)) {
|
||||
@@ -175,7 +175,13 @@ public class PlusDataPermissionHandler {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户信息
|
||||
*
|
||||
* @return 当前登录用户的LoginUser对象,可能为null(如未登录场景)
|
||||
*/
|
||||
private LoginUser currentUser() {
|
||||
// 从数据权限助手缓存中获取当前登录用户
|
||||
LoginUser currentUser = DataPermissionHelper.getVariable("user");
|
||||
if (ObjectUtil.isNull(currentUser)) {
|
||||
currentUser = LoginHelper.getLoginUser();
|
||||
@@ -194,32 +200,6 @@ public class PlusDataPermissionHandler {
|
||||
return resolvedAccess;
|
||||
}
|
||||
|
||||
private DataPermissionAccess resolveAccess() {
|
||||
HttpServletRequest request = ServletUtils.getRequest();
|
||||
if (request == null) {
|
||||
return DataPermissionAccess.EMPTY;
|
||||
}
|
||||
Object handler = request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE);
|
||||
if (!(handler instanceof HandlerMethod handlerMethod)) {
|
||||
return DataPermissionAccess.EMPTY;
|
||||
}
|
||||
Set<String> perms = new LinkedHashSet<>();
|
||||
Set<String> roleKeys = new LinkedHashSet<>();
|
||||
SaCheckPermission saCheckPermission = findAnnotation(handlerMethod, SaCheckPermission.class);
|
||||
if (saCheckPermission != null) {
|
||||
perms.addAll(toSet(saCheckPermission.value()));
|
||||
roleKeys.addAll(toSet(saCheckPermission.orRole()));
|
||||
}
|
||||
SaCheckRole saCheckRole = findAnnotation(handlerMethod, SaCheckRole.class);
|
||||
if (saCheckRole != null) {
|
||||
roleKeys.addAll(toSet(saCheckRole.value()));
|
||||
}
|
||||
if (perms.isEmpty() && roleKeys.isEmpty()) {
|
||||
return DataPermissionAccess.EMPTY;
|
||||
}
|
||||
return new DataPermissionAccess(Set.copyOf(perms), Set.copyOf(roleKeys));
|
||||
}
|
||||
|
||||
private List<RoleDTO> scopeRoles(LoginUser user, DataPermissionAccess access) {
|
||||
List<RoleDTO> roles = user.getRoles();
|
||||
if (!access.constrained()) {
|
||||
@@ -253,6 +233,32 @@ public class PlusDataPermissionHandler {
|
||||
return new ArrayList<>(roleMap.values());
|
||||
}
|
||||
|
||||
private DataPermissionAccess resolveAccess() {
|
||||
HttpServletRequest request = ServletUtils.getRequest();
|
||||
if (request == null) {
|
||||
return DataPermissionAccess.EMPTY;
|
||||
}
|
||||
Object handler = request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE);
|
||||
if (!(handler instanceof HandlerMethod handlerMethod)) {
|
||||
return DataPermissionAccess.EMPTY;
|
||||
}
|
||||
Set<String> perms = new LinkedHashSet<>();
|
||||
Set<String> roleKeys = new LinkedHashSet<>();
|
||||
SaCheckPermission saCheckPermission = findAnnotation(handlerMethod, SaCheckPermission.class);
|
||||
if (saCheckPermission != null) {
|
||||
perms.addAll(toSet(saCheckPermission.value()));
|
||||
roleKeys.addAll(toSet(saCheckPermission.orRole()));
|
||||
}
|
||||
SaCheckRole saCheckRole = findAnnotation(handlerMethod, SaCheckRole.class);
|
||||
if (saCheckRole != null) {
|
||||
roleKeys.addAll(toSet(saCheckRole.value()));
|
||||
}
|
||||
if (perms.isEmpty() && roleKeys.isEmpty()) {
|
||||
return DataPermissionAccess.EMPTY;
|
||||
}
|
||||
return new DataPermissionAccess(Set.copyOf(perms), Set.copyOf(roleKeys));
|
||||
}
|
||||
|
||||
private <A extends Annotation> A findAnnotation(HandlerMethod handlerMethod, Class<A> annotationType) {
|
||||
A annotation = AnnotationUtil.getAnnotation(handlerMethod.getMethod(), annotationType);
|
||||
if (annotation != null) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.dromara.common.mybatis.helper;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
||||
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.dromara.common.core.exception.ServiceException;
|
||||
@@ -14,6 +15,8 @@ import java.sql.DatabaseMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 数据库助手
|
||||
@@ -24,6 +27,7 @@ import java.util.List;
|
||||
public class DataBaseHelper {
|
||||
|
||||
private static final DynamicRoutingDataSource DS = SpringUtils.getBean(DynamicRoutingDataSource.class);
|
||||
private static final Map<String, DataBaseType> DB_TYPE_CACHE = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 获取当前数据源对应的数据库类型
|
||||
@@ -37,13 +41,17 @@ public class DataBaseHelper {
|
||||
*/
|
||||
public static DataBaseType getDataBaseType() {
|
||||
DataSource dataSource = DS.determineDataSource();
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
DatabaseMetaData metaData = conn.getMetaData();
|
||||
String databaseProductName = metaData.getDatabaseProductName();
|
||||
return DataBaseType.find(databaseProductName);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("获取数据库类型失败", e);
|
||||
}
|
||||
String dsKey = DynamicDataSourceContextHolder.peek();
|
||||
final String key = dsKey != null ? dsKey : "primary";
|
||||
return DB_TYPE_CACHE.computeIfAbsent(key, k -> {
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
DatabaseMetaData metaData = conn.getMetaData();
|
||||
String databaseProductName = metaData.getDatabaseProductName();
|
||||
return DataBaseType.find(databaseProductName);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("获取数据库类型失败", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,7 +82,9 @@ public class DataBaseHelper {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前加载的数据库名
|
||||
* 获取当前注册的数据源名称列表。
|
||||
*
|
||||
* @return 数据源名称列表
|
||||
*/
|
||||
public static List<String> getDataSourceNameList() {
|
||||
return new ArrayList<>(DS.getDataSources().keySet());
|
||||
|
||||
@@ -8,13 +8,14 @@ import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
|
||||
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.dromara.common.mybatis.core.domain.DataPermissionAccess;
|
||||
import org.dromara.common.core.utils.reflect.ReflectUtils;
|
||||
import org.dromara.common.mybatis.annotation.DataPermission;
|
||||
import org.dromara.common.mybatis.core.domain.DataPermissionAccess;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
@@ -27,10 +28,10 @@ import java.util.function.Supplier;
|
||||
@SuppressWarnings("unchecked")
|
||||
public class DataPermissionHelper {
|
||||
|
||||
public static final String DATA_PERMISSION_KEY = "data:permission";
|
||||
private static final String DATA_PERMISSION_KEY = "data:permission";
|
||||
private static final String ACCESS_KEY = "data:permission:access";
|
||||
|
||||
private static final ThreadLocal<Stack<Integer>> REENTRANT_IGNORE = ThreadLocal.withInitial(Stack::new);
|
||||
private static final ThreadLocal<Deque<Integer>> REENTRANT_IGNORE = ThreadLocal.withInitial(ArrayDeque::new);
|
||||
|
||||
private static final ThreadLocal<DataPermission> PERMISSION_CACHE = new ThreadLocal<>();
|
||||
|
||||
@@ -46,7 +47,7 @@ public class DataPermissionHelper {
|
||||
/**
|
||||
* 设置当前执行mapper权限注解
|
||||
*
|
||||
* @param dataPermission 数据权限注解
|
||||
* @param dataPermission 数据权限注解
|
||||
*/
|
||||
public static void setPermission(DataPermission dataPermission) {
|
||||
PERMISSION_CACHE.set(dataPermission);
|
||||
@@ -82,10 +83,20 @@ public class DataPermissionHelper {
|
||||
context.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前数据权限访问控制对象。
|
||||
*
|
||||
* @return 访问控制对象
|
||||
*/
|
||||
public static DataPermissionAccess getAccess() {
|
||||
return getVariable(ACCESS_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前数据权限访问控制对象。
|
||||
*
|
||||
* @param access 访问控制对象
|
||||
*/
|
||||
public static void setAccess(DataPermissionAccess access) {
|
||||
setVariable(ACCESS_KEY, access);
|
||||
}
|
||||
@@ -97,21 +108,23 @@ public class DataPermissionHelper {
|
||||
* @throws NullPointerException 如果数据权限上下文类型异常,则抛出NullPointerException
|
||||
*/
|
||||
public static Map<String, Object> getContext() {
|
||||
Object attribute = new HashMap<>();
|
||||
if (SaHolder.getContext().isValid()) {
|
||||
SaStorage saStorage = SaHolder.getStorage();
|
||||
SaStorage saStorage = SaHolder.getStorage();
|
||||
Object attribute = saStorage.get(DATA_PERMISSION_KEY);
|
||||
if (ObjectUtil.isNull(attribute)) {
|
||||
saStorage.set(DATA_PERMISSION_KEY, new HashMap<>());
|
||||
attribute = saStorage.get(DATA_PERMISSION_KEY);
|
||||
if (ObjectUtil.isNull(attribute)) {
|
||||
saStorage.set(DATA_PERMISSION_KEY, new HashMap<>());
|
||||
attribute = saStorage.get(DATA_PERMISSION_KEY);
|
||||
}
|
||||
}
|
||||
if (attribute instanceof Map map) {
|
||||
return map;
|
||||
}
|
||||
throw new NullPointerException("data permission context type exception");
|
||||
throw new IllegalStateException("data permission context type exception");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前忽略策略。
|
||||
*
|
||||
* @return 忽略策略
|
||||
*/
|
||||
private static IgnoreStrategy getIgnoreStrategy() {
|
||||
Object ignoreStrategyLocal = ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL"));
|
||||
if (ignoreStrategyLocal instanceof ThreadLocal<?> IGNORE_STRATEGY_LOCAL) {
|
||||
@@ -132,7 +145,7 @@ public class DataPermissionHelper {
|
||||
} else {
|
||||
ignoreStrategy.setDataPermission(true);
|
||||
}
|
||||
Stack<Integer> reentrantStack = REENTRANT_IGNORE.get();
|
||||
Deque<Integer> reentrantStack = REENTRANT_IGNORE.get();
|
||||
reentrantStack.push(reentrantStack.size() + 1);
|
||||
}
|
||||
|
||||
@@ -147,7 +160,7 @@ public class DataPermissionHelper {
|
||||
&& !Boolean.TRUE.equals(ignoreStrategy.getIllegalSql())
|
||||
&& !Boolean.TRUE.equals(ignoreStrategy.getTenantLine())
|
||||
&& CollectionUtil.isEmpty(ignoreStrategy.getOthers());
|
||||
Stack<Integer> reentrantStack = REENTRANT_IGNORE.get();
|
||||
Deque<Integer> reentrantStack = REENTRANT_IGNORE.get();
|
||||
boolean empty = reentrantStack.isEmpty() || reentrantStack.pop() == 1;
|
||||
if (noOtherIgnoreStrategy && empty) {
|
||||
InterceptorIgnoreHelper.clearIgnoreStrategy();
|
||||
@@ -176,6 +189,7 @@ public class DataPermissionHelper {
|
||||
* 在忽略数据权限中执行
|
||||
*
|
||||
* @param handle 处理执行方法
|
||||
* @return 执行结果
|
||||
*/
|
||||
public static <T> T ignore(Supplier<T> handle) {
|
||||
enableIgnore();
|
||||
|
||||
Reference in New Issue
Block a user