| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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<>(); |
| | | // 优先设置变量 |
| | | List<String> keys = new ArrayList<>(); |
| | | Map<DataColumn, Boolean> ignoreMap = new HashMap<>(); |
| | | for (DataColumn dataColumn : dataPermission.value()) { |
| | | if (dataColumn.key().length != dataColumn.value().length) { |
| | | throw new ServiceException("角色数据范围异常 => key与value长度不匹配"); |
| | | } |
| | | // 包含权限标识符 这直接跳过 |
| | | if (StringUtils.isNotBlank(dataColumn.permission()) && |
| | | CollUtil.contains(user.getMenuPermission(), dataColumn.permission()) |
| | | ) { |
| | | ignoreMap.put(dataColumn, Boolean.TRUE); |
| | | continue; |
| | | } |
| | | // 设置注解变量 key 为表达式变量 value 为变量值 |
| | | for (int i = 0; i < dataColumn.key().length; i++) { |
| | | context.setVariable(dataColumn.key()[i], dataColumn.value()[i]); |
| | | } |
| | | keys.addAll(Arrays.stream(dataColumn.key()).map(key -> "#" + key).toList()); |
| | | } |
| | | |
| | | for (RoleDTO role : user.getRoles()) { |
| | | user.setRoleId(role.getRoleId()); |
| | | // 获取角色权限泛型 |
| | |
| | | } |
| | | // 全部数据权限直接返回 |
| | | if (type == DataScopeType.ALL) { |
| | | return ""; |
| | | return StringUtils.EMPTY; |
| | | } |
| | | boolean isSuccess = false; |
| | | List<String> keys = new ArrayList<>(); |
| | | for (DataColumn dataColumn : dataPermission.value()) { |
| | | if (dataColumn.key().length != dataColumn.value().length) { |
| | | throw new ServiceException("角色数据范围异常 => key与value长度不匹配"); |
| | | } |
| | | // 设置注解变量 key 为表达式变量 value 为变量值 |
| | | for (int i = 0; i < dataColumn.key().length; i++) { |
| | | context.setVariable(dataColumn.key()[i], dataColumn.value()[i]); |
| | | } |
| | | keys.addAll(Arrays.stream(dataColumn.key()).map(key -> "#" + key).toList()); |
| | | } |
| | | for (DataColumn dataColumn : dataPermission.value()) { |
| | | // 不包含 key 变量 则不处理 |
| | | if (!StringUtils.containsAny(type.getSqlTemplate(), keys.toArray(String[]::new))) { |
| | | continue; |
| | | } |
| | | // 包含权限标识符 这直接跳过 |
| | | if (StringUtils.isNotBlank(dataColumn.permission()) && |
| | | CollUtil.contains(user.getMenuPermission(), dataColumn.permission()) |
| | | ) { |
| | | if (ignoreMap.containsKey(dataColumn)) { |
| | | // 修复多角色与权限标识符共用问题 https://gitee.com/dromara/RuoYi-Vue-Plus/issues/IB4CS4 |
| | | conditions.add(joinStr + " 1 = 1 "); |
| | | isSuccess = true; |
| | | continue; |
| | | } |
| | | |
| | | // 不包含 key 变量 则不处理 |
| | | if (!StringUtils.containsAny(type.getSqlTemplate(), keys.toArray(String[]::new))) { |
| | | continue; |
| | | } |
| | | // 当前注解不满足模板 不处理 |
| | | if (!StringUtils.containsAny(type.getSqlTemplate(), dataColumn.key())) { |
| | | continue; |
| | | } |
| | | // 忽略数据权限 防止spel表达式内有其他sql查询导致死循环调用 |
| | | String sql = DataPermissionHelper.ignore(() -> |
| | | parser.parseExpression(type.getSqlTemplate(), parserContext).getValue(context, String.class) |
| | |
| | | String sql = StreamUtils.join(conditions, Function.identity(), ""); |
| | | return sql.substring(joinStr.length()); |
| | | } |
| | | return ""; |
| | | return StringUtils.EMPTY; |
| | | } |
| | | |
| | | /** |
| | |
| | | return getDataPermission(mapperId) == null; |
| | | } |
| | | |
| | | /** |
| | | * 对所有null变量找不到的变量返回默认值 |
| | | */ |
| | | @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; |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 对所有null变量找不到的变量返回默认值 委托模式 将不需要处理的方法委托给原处理器 |
| | | */ |
| | | @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); |
| | | } |
| | | } |
| | | |
| | | } |