From 0c3f0f3e387fa66e520d8a065011232b559e6acc Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期三, 09 九月 2026 13:43:43 +0800
Subject: [PATCH] fix(datascope): 修复数据权限范围判断逻辑
---
src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java | 72 +++++++++++++++++++++--------------
1 files changed, 43 insertions(+), 29 deletions(-)
diff --git a/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java b/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java
index da6ac96..065a17c 100644
--- a/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java
+++ b/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java
@@ -6,6 +6,7 @@
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
+import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
@@ -73,8 +74,7 @@
if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
{
String permission = StringUtils.defaultIfEmpty(controllerDataScope.permission(), PermissionContextHolder.getContext());
- dataScopeFilter(joinPoint, currentUser, controllerDataScope.deptAlias(),
- controllerDataScope.userAlias(), permission);
+ dataScopeFilter(joinPoint, currentUser, controllerDataScope.deptAlias(), controllerDataScope.userAlias(), permission);
}
}
}
@@ -94,7 +94,7 @@
List<String> conditions = new ArrayList<String>();
List<String> scopeCustomIds = new ArrayList<String>();
user.getRoles().forEach(role -> {
- if (DATA_SCOPE_CUSTOM.equals(role.getDataScope()) && StringUtils.containsAny(role.getPermissions(), Convert.toStrArray(permission)))
+ if (DATA_SCOPE_CUSTOM.equals(role.getDataScope()) && StringUtils.equals(role.getStatus(), UserConstants.ROLE_NORMAL) && StringUtils.containsAny(role.getPermissions(), Convert.toStrArray(permission)))
{
scopeCustomIds.add(Convert.toStr(role.getRoleId()));
}
@@ -103,11 +103,14 @@
for (SysRole role : user.getRoles())
{
String dataScope = role.getDataScope();
- if (conditions.contains(dataScope))
+ if (conditions.contains(dataScope) || StringUtils.equals(role.getStatus(), UserConstants.ROLE_DISABLE))
{
continue;
}
- if (!StringUtils.containsAny(role.getPermissions(), Convert.toStrArray(permission)))
+ // 鏃犳潈闄愬瓧绗︿笂涓嬫枃(permission 涓虹┖锛屽鍛樺伐鍒犻櫎杩欑被鏃燖PreAuthorize鐨勬帴鍙�)鏃讹紝
+ // 涓嶅仛鍩轰簬鏉冮檺瀛楃鐨勯杩囨护锛屽惁鍒�"鍏ㄩ儴鏁版嵁"瑙掕壊浼氬湪鍛戒腑 DATA_SCOPE_ALL 鍓嶈 continue锛�
+ // 鏈�缁堣惤鍏�"鏃犱换浣曡鑹插尮閰�"鐨勫厹搴曪紝瀵艰嚧鏃犳硶鍒犻櫎
+ if (StringUtils.isNotBlank(permission) && !StringUtils.containsAny(role.getPermissions(), Convert.toStrArray(permission)))
{
continue;
}
@@ -117,45 +120,56 @@
conditions.add(dataScope);
break;
}
- else if (DATA_SCOPE_CUSTOM.equals(dataScope))
- {
- if (scopeCustomIds.size() > 1)
- {
- // 澶氫釜鑷畾鏁版嵁鏉冮檺浣跨敤in鏌ヨ锛岄伩鍏嶅娆℃嫾鎺ャ��
- sqlString.append(StringUtils.format(" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id in ({}) ) ", deptAlias, String.join(",", scopeCustomIds)));
- }
- else
- {
- sqlString.append(StringUtils.format(" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias, role.getRoleId()));
- }
- }
- else if (DATA_SCOPE_DEPT.equals(dataScope))
- {
- sqlString.append(StringUtils.format(" OR {}.dept_id = {} ", deptAlias, user.getDeptId()));
- }
- else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope))
- {
- sqlString.append(StringUtils.format(" OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )", deptAlias, user.getDeptId(), user.getDeptId()));
- }
+// else if (DATA_SCOPE_CUSTOM.equals(dataScope))
+// {
+// if (scopeCustomIds.size() > 1)
+// {
+// // 澶氫釜鑷畾鏁版嵁鏉冮檺浣跨敤in鏌ヨ锛岄伩鍏嶅娆℃嫾鎺ャ��
+// sqlString.append(StringUtils.format(" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id in ({}) ) ", deptAlias, String.join(",", scopeCustomIds)));
+// }
+// else
+// {
+// sqlString.append(StringUtils.format(" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias, role.getRoleId()));
+// }
+// }
+// else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope))
+// {
+// sqlString.append(StringUtils.format(" OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )", deptAlias, user.getDeptId(), user.getDeptId()));
+// }
else if (DATA_SCOPE_SELF.equals(dataScope))
{
if (StringUtils.isNotBlank(userAlias))
{
sqlString.append(StringUtils.format(" OR {}.user_id = {} ", userAlias, user.getUserId()));
}
- else
+ else if (StringUtils.isNotBlank(deptAlias))
{
// 鏁版嵁鏉冮檺涓轰粎鏈汉涓旀病鏈塽serAlias鍒悕涓嶆煡璇换浣曟暟鎹�
- sqlString.append(StringUtils.format(" OR {}.dept_id = 0 ", deptAlias));
+ sqlString.append(StringUtils.format(" OR {}.dept_id = -1 ", deptAlias));
+ }
+ else
+ {
+ sqlString.append(" OR 1 = 0 ");
}
}
conditions.add(dataScope);
}
- // 澶氳鑹叉儏鍐典笅锛屾墍鏈夎鑹查兘涓嶅寘鍚紶閫掕繃鏉ョ殑鏉冮檺瀛楃锛岃繖涓椂鍊檚qlString涔熶細涓虹┖锛屾墍浠ヨ闄愬埗涓�涓�,涓嶆煡璇换浣曟暟鎹�
+ // 瑙掕壊閮戒笉鍖呭惈浼犻�掕繃鏉ョ殑鏉冮檺瀛楃锛岃繖涓椂鍊檚qlString涔熶細涓虹┖锛屾墍浠ヨ闄愬埗涓�涓�,涓嶆煡璇换浣曟暟鎹�
if (StringUtils.isEmpty(conditions))
{
- sqlString.append(StringUtils.format(" OR {}.dept_id = 0 ", deptAlias));
+ if (StringUtils.isNotBlank(deptAlias))
+ {
+ sqlString.append(StringUtils.format(" OR {}.dept_id = -1 ", deptAlias));
+ }
+ else if (StringUtils.isNotBlank(userAlias))
+ {
+ sqlString.append(StringUtils.format(" OR {}.user_id = -1 ", userAlias));
+ }
+ else
+ {
+ sqlString.append(" OR 1 = 0 ");
+ }
}
if (StringUtils.isNotBlank(sqlString.toString()))
--
Gitblit v1.9.3