From c2746c23923b7f28219f670a8cd13ac70cd17668 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期四, 26 十二月 2024 13:26:18 +0800
Subject: [PATCH] update 优化 数据权限处理器 增加默认值处理 针对于表达式变量与注解不对应或者表达式变量为null的情况
---
ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/handler/PlusDataPermissionHandler.java | 69 ++++++++++++++++++++++++++++++++--
1 files changed, 65 insertions(+), 4 deletions(-)
diff --git a/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/handler/PlusDataPermissionHandler.java b/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/handler/PlusDataPermissionHandler.java
index c529c53..783d352 100644
--- a/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/handler/PlusDataPermissionHandler.java
+++ b/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/handler/PlusDataPermissionHandler.java
@@ -3,6 +3,7 @@
import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
+import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
@@ -28,9 +29,7 @@
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
-import org.springframework.expression.BeanResolver;
-import org.springframework.expression.ExpressionParser;
-import org.springframework.expression.ParserContext;
+import org.springframework.expression.*;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
@@ -130,7 +129,9 @@
joinStr = " " + dataPermission.joinStr() + " ";
}
LoginUser user = DataPermissionHelper.getVariable("user");
- StandardEvaluationContext context = new StandardEvaluationContext();
+ Object defaultValue = "-1";
+ NullSafeStandardEvaluationContext context = new NullSafeStandardEvaluationContext(defaultValue);
+ context.addPropertyAccessor(new NullSafePropertyAccessor(context.getPropertyAccessors().get(0), defaultValue));
context.setBeanResolver(beanResolver);
DataPermissionHelper.getContext().forEach(context::setVariable);
Set<String> conditions = new HashSet<>();
@@ -257,4 +258,64 @@
return getDataPermission(mapperId) == null;
}
+ /**
+ * 瀵规墍鏈塶ull鍙橀噺鎵句笉鍒扮殑鍙橀噺杩斿洖榛樿鍊�
+ */
+ @AllArgsConstructor
+ private static class NullSafeStandardEvaluationContext extends StandardEvaluationContext {
+
+ private final Object defaultValue;
+
+ @Override
+ public Object lookupVariable(String name) {
+ Object obj = super.lookupVariable(name);
+ // 濡傛灉璇诲彇鍒扮殑鍊兼槸 null锛屽垯杩斿洖榛樿鍊�
+ if (obj == null) {
+ return defaultValue;
+ }
+ return obj;
+ }
+
+ }
+
+ /**
+ * 瀵规墍鏈塶ull鍙橀噺鎵句笉鍒扮殑鍙橀噺杩斿洖榛樿鍊� 濮旀墭妯″紡 灏嗕笉闇�瑕佸鐞嗙殑鏂规硶濮旀墭缁欏師澶勭悊鍣�
+ */
+ @AllArgsConstructor
+ private static class NullSafePropertyAccessor implements PropertyAccessor {
+
+ private final PropertyAccessor delegate;
+ private final Object defaultValue;
+
+ @Override
+ public Class<?>[] getSpecificTargetClasses() {
+ return delegate.getSpecificTargetClasses();
+ }
+
+ @Override
+ public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
+ return delegate.canRead(context, target, name);
+ }
+
+ @Override
+ public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
+ TypedValue value = delegate.read(context, target, name);
+ // 濡傛灉璇诲彇鍒扮殑鍊兼槸 null锛屽垯杩斿洖榛樿鍊�
+ if (value.getValue() == null) {
+ return new TypedValue(defaultValue);
+ }
+ return value;
+ }
+
+ @Override
+ public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
+ return delegate.canWrite(context, target, name);
+ }
+
+ @Override
+ public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
+ delegate.write(context, target, name, newValue);
+ }
+ }
+
}
--
Gitblit v1.9.3