From e6b45d4cdbab85f26514911090420f1f39474b92 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期四, 16 十二月 2021 10:54:44 +0800 Subject: [PATCH] update 数据权限注解支持类标注 --- ruoyi-framework/src/main/java/com/ruoyi/framework/handler/PlusDataPermissionHandler.java | 63 +++++++++++++++++++------------ 1 files changed, 38 insertions(+), 25 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/handler/PlusDataPermissionHandler.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/handler/PlusDataPermissionHandler.java index 89897c3..5bcb868 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/handler/PlusDataPermissionHandler.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/handler/PlusDataPermissionHandler.java @@ -17,6 +17,7 @@ import lombok.extern.slf4j.Slf4j; import net.sf.jsqlparser.JSQLParserException; import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.expression.Parenthesis; import net.sf.jsqlparser.expression.operators.conditional.AndExpression; import net.sf.jsqlparser.parser.CCJSqlParserUtil; import org.springframework.context.expression.BeanFactoryResolver; @@ -60,10 +61,12 @@ } try { Expression expression = CCJSqlParserUtil.parseExpression(dataFilterSql); + // 鏁版嵁鏉冮檺浣跨敤鍗曠嫭鐨勬嫭鍙� 闃叉涓庡叾浠栨潯浠跺啿绐� + Parenthesis parenthesis = new Parenthesis(expression); if (ObjectUtil.isNotNull(where)) { - return new AndExpression(where, expression); + return new AndExpression(where, parenthesis); } else { - return expression; + return parenthesis; } } catch (JSQLParserException e) { throw new ServiceException("鏁版嵁鏉冮檺瑙f瀽寮傚父 => " + e.getMessage()); @@ -75,40 +78,44 @@ */ private String buildDataFilter(SysUser user, DataColumn[] dataColumns, boolean isSelect) { StringBuilder sqlString = new StringBuilder(); - + // 鏇存柊鎴栧垹闄ら渶婊¤冻鎵�鏈夋潯浠� + String joinStr = isSelect ? " OR " : " AND "; StandardEvaluationContext context = new StandardEvaluationContext(); context.setBeanResolver(beanResolver); context.setVariable("user", user); - - for (DataColumn dataColumn : dataColumns) { - // 璁剧疆娉ㄨВ鍙橀噺 key 涓鸿〃杈惧紡鍙橀噺 value 涓哄彉閲忓�� - context.setVariable(dataColumn.key(), dataColumn.value()); - for (SysRole role : user.getRoles()) { - user.setRoleId(role.getRoleId()); - - // 鑾峰彇瑙掕壊鏉冮檺娉涘瀷 - DataScopeType type = DataScopeType.findCode(role.getDataScope()); - if (ObjectUtil.isNull(type)) { - throw new ServiceException("瑙掕壊鏁版嵁鑼冨洿寮傚父 => " + role.getDataScope()); - } - // 鍏ㄩ儴鏁版嵁鏉冮檺鐩存帴杩斿洖 - if (type == DataScopeType.ALL) { - return ""; - } + for (SysRole role : user.getRoles()) { + user.setRoleId(role.getRoleId()); + // 鑾峰彇瑙掕壊鏉冮檺娉涘瀷 + DataScopeType type = DataScopeType.findCode(role.getDataScope()); + if (ObjectUtil.isNull(type)) { + throw new ServiceException("瑙掕壊鏁版嵁鑼冨洿寮傚父 => " + role.getDataScope()); + } + // 鍏ㄩ儴鏁版嵁鏉冮檺鐩存帴杩斿洖 + if (type == DataScopeType.ALL) { + return ""; + } + boolean isSuccess = false; + for (DataColumn dataColumn : dataColumns) { // 涓嶅寘鍚� key 鍙橀噺 鍒欎笉澶勭悊 - if (!StringUtils.contains(type.getSql(), "#" + dataColumn.key())) { + if (!StringUtils.contains(type.getSqlTemplate(), "#" + dataColumn.key())) { continue; } - // 鏇存柊鎴栧垹闄ら渶婊¤冻鎵�鏈夋潯浠� - sqlString.append(isSelect ? " OR " : " AND "); + // 璁剧疆娉ㄨВ鍙橀噺 key 涓鸿〃杈惧紡鍙橀噺 value 涓哄彉閲忓�� + context.setVariable(dataColumn.key(), dataColumn.value()); + // 瑙f瀽sql妯℃澘骞跺~鍏� - String sql = parser.parseExpression(type.getSql(), parserContext).getValue(context, String.class); - sqlString.append(sql); + String sql = parser.parseExpression(type.getSqlTemplate(), parserContext).getValue(context, String.class); + sqlString.append(joinStr).append(sql); + isSuccess = true; + } + // 鏈鐞嗘垚鍔熷垯濉厖鍏滃簳鏂规 + if (!isSuccess) { + sqlString.append(joinStr).append(type.getElseSql()); } } if (StringUtils.isNotBlank(sqlString.toString())) { - return sqlString.substring(isSelect ? 4 : 5); + return sqlString.substring(joinStr.length()); } return ""; } @@ -122,12 +129,18 @@ List<Method> methods = Arrays.stream(ClassUtil.getDeclaredMethods(clazz)) .filter(method -> method.getName().equals(methodName)).collect(Collectors.toList()); DataPermission dataPermission; + // 鑾峰彇鏂规硶娉ㄨВ for (Method method : methods) { if (AnnotationUtil.hasAnnotation(method, DataPermission.class)) { dataPermission = AnnotationUtil.getAnnotation(method, DataPermission.class); return dataPermission.value(); } } + // 鑾峰彇绫绘敞瑙� + if (AnnotationUtil.hasAnnotation(clazz, DataPermission.class)) { + dataPermission = AnnotationUtil.getAnnotation(clazz, DataPermission.class); + return dataPermission.value(); + } return null; } } -- Gitblit v1.9.3