package com.qianwen.core.datascope.interceptor; import com.baomidou.mybatisplus.core.toolkit.PluginUtils; import java.lang.reflect.Method; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.stream.Stream; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.mapping.StatementType; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.qianwen.core.datascope.annotation.DataAuth; import com.qianwen.core.datascope.handler.DataScopeHandler; import com.qianwen.core.datascope.model.DataScopeModel; import com.qianwen.core.datascope.props.DataScopeProperties; import com.qianwen.core.mp.intercept.QueryInterceptor; import com.qianwen.core.secure.BladeUser; import com.qianwen.core.secure.utils.AuthUtil; import com.qianwen.core.tool.utils.ClassUtil; import com.qianwen.core.tool.utils.SpringUtil; import com.qianwen.core.tool.utils.StringUtil; /* loaded from: blade-starter-datascope-9.3.0.1-SNAPSHOT.jar:org/springblade/core/datascope/interceptor/DataScopeInterceptor.class */ public class DataScopeInterceptor implements QueryInterceptor { private static final Logger log = LoggerFactory.getLogger(DataScopeInterceptor.class); private final ConcurrentMap dataAuthMap = new ConcurrentHashMap(8); private final DataScopeHandler dataScopeHandler; private final DataScopeProperties dataScopeProperties; public DataScopeInterceptor(final DataScopeHandler dataScopeHandler, final DataScopeProperties dataScopeProperties) { this.dataScopeHandler = dataScopeHandler; this.dataScopeProperties = dataScopeProperties; } public void intercept(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { if (!this.dataScopeProperties.getEnabled().booleanValue()) return; BladeUser bladeUser = AuthUtil.getUser(); if (bladeUser == null) return; if (SqlCommandType.SELECT != ms.getSqlCommandType() || StatementType.CALLABLE == ms.getStatementType()) return; String originalSql = boundSql.getSql(); DataAuth dataAuth = findDataAuthAnnotation(ms); String mapperId = ms.getId(); String className = mapperId.substring(0, mapperId.lastIndexOf(".")); String mapperName = ClassUtil.getShortName(className); String methodName = mapperId.substring(mapperId.lastIndexOf(".") + 1); boolean mapperSkip = (this.dataScopeProperties.getMapperKey().stream().noneMatch(methodName::contains) || this.dataScopeProperties.getMapperExclude().stream().anyMatch(mapperName::contains)); if (dataAuth == null || mapperSkip) return; DataScopeModel dataScope = new DataScopeModel(); if (dataAuth != null) { dataScope.setResourceCode(dataAuth.code()); dataScope.setScopeColumn(dataAuth.column()); dataScope.setScopeType(Integer.valueOf(dataAuth.type().getType())); dataScope.setScopeField(dataAuth.field()); dataScope.setScopeValue(dataAuth.value()); } String sqlCondition = this.dataScopeHandler.sqlCondition(mapperId, dataScope, bladeUser, originalSql); if (!StringUtil.isBlank(sqlCondition)) { PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql); mpBoundSql.sql(sqlCondition); } } private DataAuth findDataAuthAnnotation(MappedStatement mappedStatement) { String id = mappedStatement.getId(); return this.dataAuthMap.computeIfAbsent(id, key -> { Method[] declaredMethods; String className = key.substring(0, key.lastIndexOf(".")); String mapperBean = StringUtil.firstCharToLower(ClassUtil.getShortName(className)); Object mapper = SpringUtil.getBean(mapperBean); String methodName = key.substring(key.lastIndexOf(".") + 1); Class[] interfaces = ClassUtil.getAllInterfaces(mapper); for (Class mapperInterface : interfaces) { for (Method method : mapperInterface.getDeclaredMethods()) { if (methodName.equals(method.getName()) && method.isAnnotationPresent(DataAuth.class)) { return (DataAuth) method.getAnnotation(DataAuth.class); } } } return null; }); } }