refactor: 优化数据权限拦截,无数据权限注解优先级设置最高

This commit is contained in:
hxr
2023-11-23 23:57:53 +08:00
parent 78ef8b91b3
commit b2374bda69

View File

@@ -28,17 +28,17 @@ public class MyDataPermissionHandler implements DataPermissionHandler {
@Override @Override
@SneakyThrows @SneakyThrows
public Expression getSqlSegment(Expression where, String mappedStatementId) { public Expression getSqlSegment(Expression where, String mappedStatementId) {
// 超级管理员不受数据权限控制
if (SecurityUtils.isRoot()) {
return where;
}
Class<?> clazz = Class.forName(mappedStatementId.substring(0, mappedStatementId.lastIndexOf(StringPool.DOT))); Class<?> clazz = Class.forName(mappedStatementId.substring(0, mappedStatementId.lastIndexOf(StringPool.DOT)));
String methodName = mappedStatementId.substring(mappedStatementId.lastIndexOf(StringPool.DOT) + 1); String methodName = mappedStatementId.substring(mappedStatementId.lastIndexOf(StringPool.DOT) + 1);
Method[] methods = clazz.getDeclaredMethods(); Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) { for (Method method : methods) {
DataPermission annotation = method.getAnnotation(DataPermission.class); DataPermission annotation = method.getAnnotation(DataPermission.class);
if (ObjectUtils.isNotEmpty(annotation) // 如果没有注解或者是超级管理员,直接返回
&& (method.getName().equals(methodName) || (method.getName() + "_COUNT").equals(methodName))) { if (annotation == null || SecurityUtils.isRoot()) {
return where;
}
if (method.getName().equals(methodName) || (method.getName() + "_COUNT").equals(methodName)) {
return dataScopeFilter(annotation.deptAlias(), annotation.deptIdColumnName(), annotation.userAlias(), annotation.userIdColumnName(), where); return dataScopeFilter(annotation.deptAlias(), annotation.deptIdColumnName(), annotation.userAlias(), annotation.userIdColumnName(), where);
} }
} }