fuweng11 commented on code in PR #8304: URL: https://github.com/apache/inlong/pull/8304#discussion_r1241799516
########## inlong-manager/manager-dao/src/main/java/org/apache/inlong/manager/dao/interceptor/MultitenantInterceptor.java: ########## @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.inlong.manager.dao.interceptor; + +import org.apache.inlong.manager.common.tenant.MultitenantQueryFilter; +import org.apache.inlong.manager.pojo.user.LoginUserUtils; +import org.apache.inlong.manager.pojo.user.UserInfo; + +import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.parser.CCJSqlParserManager; +import net.sf.jsqlparser.parser.CCJSqlParserUtil; +import net.sf.jsqlparser.statement.select.PlainSelect; +import net.sf.jsqlparser.statement.select.Select; +import org.apache.ibatis.executor.statement.StatementHandler; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.plugin.Interceptor; +import org.apache.ibatis.plugin.Intercepts; +import org.apache.ibatis.plugin.Invocation; +import org.apache.ibatis.plugin.Plugin; +import org.apache.ibatis.plugin.Signature; +import org.apache.ibatis.reflection.DefaultReflectorFactory; +import org.apache.ibatis.reflection.MetaObject; +import org.apache.ibatis.reflection.SystemMetaObject; + +import java.io.StringReader; +import java.sql.Connection; +import java.util.Properties; + +@Intercepts({ + @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class + }) +}) +public class MultitenantInterceptor implements Interceptor { + + private static final String TENANT_CONDITION = "tenant="; + + @Override + public Object intercept(Invocation invocation) throws Throwable { + StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); + MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, + SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory()); + MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement"); + + String fullMethodName = mappedStatement.getId(); + if (!MultitenantQueryFilter.isMultitenantQuery(fullMethodName.split("_")[0])) { + return invocation.proceed(); + } + + BoundSql boundSql = statementHandler.getBoundSql(); + String sql = boundSql.getSql(); + + CCJSqlParserManager parserManager = new CCJSqlParserManager(); + Select select = (Select) parserManager.parse(new StringReader(sql)); + PlainSelect plain = (PlainSelect) select.getSelectBody(); + + StringBuilder whereSql = new StringBuilder(); + whereSql.append(TENANT_CONDITION).append(getTenant()); + + Expression where = plain.getWhere(); + if (where == null) { + Expression expression = CCJSqlParserUtil.parseCondExpression(whereSql.toString()); + plain.setWhere(expression); + } else { + if (where.toString().contains(TENANT_CONDITION)) { + return invocation.proceed(); + } + whereSql.append(" and ( ").append(where).append(" )"); + Expression expression = CCJSqlParserUtil.parseCondExpression(whereSql.toString()); + plain.setWhere(expression); + } + metaObject.setValue("delegate.boundSql.sql", select.toString()); + return invocation.proceed(); + } + + private String getTenant() { + UserInfo userInfo = LoginUserUtils.getLoginUser(); + if (userInfo == null) { + throw new IllegalStateException("get no login user, please login first"); + } + String tenant = userInfo.getTenant(); + if (tenant == null) { Review Comment: String.isNotBlank ########## inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tenant/MultitenantQueryFilter.java: ########## @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.inlong.manager.common.tenant; + +import lombok.experimental.UtilityClass; +import lombok.extern.slf4j.Slf4j; +import org.reflections.Reflections; +import org.reflections.scanners.Scanners; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * Utils to check if SQLs from some method should and tenant condition or not. + */ +@Slf4j +@UtilityClass +public class MultitenantQueryFilter { + + private static final String METHOD_FILTER_PATH = "org.apache.inlong.manager.dao.mapper"; + + private static final Set<String> METHOD_SET = new HashSet<>(); + + public static boolean isMultitenantQuery(String methodName) { + return METHOD_SET.contains(methodName); + } + + public static void init() { + + Reflections methodReflections = new Reflections(METHOD_FILTER_PATH, Scanners.MethodsAnnotated); + // process methods + Set<Method> methodSet = methodReflections.getMethodsAnnotatedWith(MutitenancyQuery.class); + markMethods(methodSet); + + // process classes + Reflections reflections = new Reflections(METHOD_FILTER_PATH, Scanners.TypesAnnotated); + Set<Class<?>> clazzSet = reflections.getTypesAnnotatedWith(MutitenancyQuery.class); + clazzSet.stream() + .filter(Class::isInterface) + .forEach(clazz -> { + // Get the JsonTypeDefine annotation + MutitenancyQuery annotation = clazz.getAnnotation(MutitenancyQuery.class); + if (annotation == null || !annotation.with()) { + return; + } + List<Method> methods = Arrays.asList(clazz.getMethods()); + markMethods(methods); + }); + + log.info("These method are annotated with QueryWithoutTenant, methods={}", METHOD_SET); Review Comment: Whether to add log for `METHOD_SET`? ########## inlong-manager/manager-dao/src/main/java/org/apache/inlong/manager/dao/interceptor/MultitenantInterceptor.java: ########## @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.inlong.manager.dao.interceptor; + +import org.apache.inlong.manager.common.tenant.MultitenantQueryFilter; +import org.apache.inlong.manager.pojo.user.LoginUserUtils; +import org.apache.inlong.manager.pojo.user.UserInfo; + +import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.parser.CCJSqlParserManager; +import net.sf.jsqlparser.parser.CCJSqlParserUtil; +import net.sf.jsqlparser.statement.select.PlainSelect; +import net.sf.jsqlparser.statement.select.Select; +import org.apache.ibatis.executor.statement.StatementHandler; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.plugin.Interceptor; +import org.apache.ibatis.plugin.Intercepts; +import org.apache.ibatis.plugin.Invocation; +import org.apache.ibatis.plugin.Plugin; +import org.apache.ibatis.plugin.Signature; +import org.apache.ibatis.reflection.DefaultReflectorFactory; +import org.apache.ibatis.reflection.MetaObject; +import org.apache.ibatis.reflection.SystemMetaObject; + +import java.io.StringReader; +import java.sql.Connection; +import java.util.Properties; + +@Intercepts({ + @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class + }) +}) +public class MultitenantInterceptor implements Interceptor { + + private static final String TENANT_CONDITION = "tenant="; + + @Override + public Object intercept(Invocation invocation) throws Throwable { + StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); + MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, + SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory()); + MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement"); + + String fullMethodName = mappedStatement.getId(); + if (!MultitenantQueryFilter.isMultitenantQuery(fullMethodName.split("_")[0])) { + return invocation.proceed(); + } + + BoundSql boundSql = statementHandler.getBoundSql(); + String sql = boundSql.getSql(); + + CCJSqlParserManager parserManager = new CCJSqlParserManager(); + Select select = (Select) parserManager.parse(new StringReader(sql)); + PlainSelect plain = (PlainSelect) select.getSelectBody(); + + StringBuilder whereSql = new StringBuilder(); + whereSql.append(TENANT_CONDITION).append(getTenant()); + + Expression where = plain.getWhere(); + if (where == null) { + Expression expression = CCJSqlParserUtil.parseCondExpression(whereSql.toString()); + plain.setWhere(expression); + } else { + if (where.toString().contains(TENANT_CONDITION)) { + return invocation.proceed(); + } + whereSql.append(" and ( ").append(where).append(" )"); + Expression expression = CCJSqlParserUtil.parseCondExpression(whereSql.toString()); + plain.setWhere(expression); + } + metaObject.setValue("delegate.boundSql.sql", select.toString()); + return invocation.proceed(); + } + + private String getTenant() { + UserInfo userInfo = LoginUserUtils.getLoginUser(); + if (userInfo == null) { + throw new IllegalStateException("get no login user, please login first"); Review Comment: Current login user is null -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
