作者 RuoYi

优化数据权限代码

@@ -365,6 +365,10 @@ public class Convert @@ -365,6 +365,10 @@ public class Convert
365 */ 365 */
366 public static String[] toStrArray(String str) 366 public static String[] toStrArray(String str)
367 { 367 {
  368 + if (StringUtils.isEmpty(str))
  369 + {
  370 + return new String[] {};
  371 + }
368 return toStrArray(",", str); 372 return toStrArray(",", str);
369 } 373 }
370 374
@@ -92,16 +92,22 @@ public class DataScopeAspect @@ -92,16 +92,22 @@ public class DataScopeAspect
92 { 92 {
93 StringBuilder sqlString = new StringBuilder(); 93 StringBuilder sqlString = new StringBuilder();
94 List<String> conditions = new ArrayList<String>(); 94 List<String> conditions = new ArrayList<String>();
  95 + List<String> scopeCustomIds = new ArrayList<String>();
  96 + user.getRoles().forEach(role -> {
  97 + if (DATA_SCOPE_CUSTOM.equals(role.getDataScope()) && StringUtils.containsAny(role.getPermissions(), Convert.toStrArray(permission)))
  98 + {
  99 + scopeCustomIds.add(Convert.toStr(role.getRoleId()));
  100 + }
  101 + });
95 102
96 for (SysRole role : user.getRoles()) 103 for (SysRole role : user.getRoles())
97 { 104 {
98 String dataScope = role.getDataScope(); 105 String dataScope = role.getDataScope();
99 - if (!DATA_SCOPE_CUSTOM.equals(dataScope) && conditions.contains(dataScope)) 106 + if (conditions.contains(dataScope))
100 { 107 {
101 continue; 108 continue;
102 } 109 }
103 - if (StringUtils.isNotEmpty(permission) && StringUtils.isNotEmpty(role.getPermissions())  
104 - && !StringUtils.containsAny(role.getPermissions(), Convert.toStrArray(permission))) 110 + if (!StringUtils.containsAny(role.getPermissions(), Convert.toStrArray(permission)))
105 { 111 {
106 continue; 112 continue;
107 } 113 }
@@ -113,9 +119,15 @@ public class DataScopeAspect @@ -113,9 +119,15 @@ public class DataScopeAspect
113 } 119 }
114 else if (DATA_SCOPE_CUSTOM.equals(dataScope)) 120 else if (DATA_SCOPE_CUSTOM.equals(dataScope))
115 { 121 {
116 - sqlString.append(StringUtils.format(  
117 - " OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias,  
118 - role.getRoleId())); 122 + if (scopeCustomIds.size() > 1)
  123 + {
  124 + // 多个自定数据权限使用in查询,避免多次拼接。
  125 + sqlString.append(StringUtils.format(" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id in ({}) ) ", deptAlias, String.join(",", scopeCustomIds)));
  126 + }
  127 + else
  128 + {
  129 + sqlString.append(StringUtils.format(" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias, role.getRoleId()));
  130 + }
119 } 131 }
120 else if (DATA_SCOPE_DEPT.equals(dataScope)) 132 else if (DATA_SCOPE_DEPT.equals(dataScope))
121 { 133 {
@@ -123,9 +135,7 @@ public class DataScopeAspect @@ -123,9 +135,7 @@ public class DataScopeAspect
123 } 135 }
124 else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope)) 136 else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope))
125 { 137 {
126 - sqlString.append(StringUtils.format(  
127 - " OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )",  
128 - deptAlias, user.getDeptId(), user.getDeptId())); 138 + 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()));
129 } 139 }
130 else if (DATA_SCOPE_SELF.equals(dataScope)) 140 else if (DATA_SCOPE_SELF.equals(dataScope))
131 { 141 {
@@ -13,9 +13,11 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -13,9 +13,11 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
13 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; 13 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
14 import com.ruoyi.common.constant.HttpStatus; 14 import com.ruoyi.common.constant.HttpStatus;
15 import com.ruoyi.common.core.domain.AjaxResult; 15 import com.ruoyi.common.core.domain.AjaxResult;
  16 +import com.ruoyi.common.core.text.Convert;
16 import com.ruoyi.common.exception.DemoModeException; 17 import com.ruoyi.common.exception.DemoModeException;
17 import com.ruoyi.common.exception.ServiceException; 18 import com.ruoyi.common.exception.ServiceException;
18 import com.ruoyi.common.utils.StringUtils; 19 import com.ruoyi.common.utils.StringUtils;
  20 +import com.ruoyi.common.utils.html.EscapeUtil;
19 21
20 /** 22 /**
21 * 全局异常处理器 23 * 全局异常处理器
@@ -79,8 +81,13 @@ public class GlobalExceptionHandler @@ -79,8 +81,13 @@ public class GlobalExceptionHandler
79 public AjaxResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) 81 public AjaxResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request)
80 { 82 {
81 String requestURI = request.getRequestURI(); 83 String requestURI = request.getRequestURI();
  84 + String value = Convert.toStr(e.getValue());
  85 + if (StringUtils.isNotEmpty(value))
  86 + {
  87 + value = EscapeUtil.clean(value);
  88 + }
82 log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI, e); 89 log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI, e);
83 - return AjaxResult.error(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), e.getValue())); 90 + return AjaxResult.error(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), value));
84 } 91 }
85 92
86 /** 93 /**