yangys
2024-03-27 e48aa2ac8dea1be5db11c63edf0b912c4ad5ce65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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<String, DataAuth> 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;
        });
    }
}