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
package com.qianwen.core.datascope.handler;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import com.qianwen.core.datascope.enums.DataScopeEnum;
import com.qianwen.core.datascope.model.DataScopeModel;
import com.qianwen.core.secure.BladeUser;
import com.qianwen.core.tool.utils.BeanUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.core.tool.utils.PlaceholderUtil;
import com.qianwen.core.tool.utils.StringUtil;
 
/* loaded from: blade-starter-datascope-9.3.0.1-SNAPSHOT.jar:org/springblade/core/datascope/handler/BladeDataScopeHandler.class */
public class BladeDataScopeHandler implements DataScopeHandler {
    private final ScopeModelHandler scopeModelHandler;
 
    public BladeDataScopeHandler(final ScopeModelHandler scopeModelHandler) {
        this.scopeModelHandler = scopeModelHandler;
    }
 
    @Override // org.springblade.core.datascope.handler.DataScopeHandler
    public String sqlCondition(String mapperId, DataScopeModel dataScope, BladeUser bladeUser, String originalSql) {
        String code = dataScope.getResourceCode();
        DataScopeModel dataScopeDb = this.scopeModelHandler.getDataScopeByMapper(mapperId, bladeUser.getDeptId());
        if (dataScopeDb == null && StringUtil.isNotBlank(code) && StringUtil.isNotBlank(bladeUser.getDeptId())) {
            dataScopeDb = this.scopeModelHandler.getDataScopeByCodeAndDept(code, bladeUser.getDeptId());
        }
        DataScopeModel dataScope2 = dataScopeDb != null ? dataScopeDb : dataScope;
        Integer scopeRule = ((DataScopeModel) Objects.requireNonNull(dataScope2)).getScopeType();
        DataScopeEnum scopeTypeEnum = DataScopeEnum.of(scopeRule);
        List<Long> ids = new ArrayList<>();
        String whereSql = "where scope.{} in ({}) or scope.{} is null";
        if (DataScopeEnum.ALL == scopeTypeEnum || StringUtil.containsAny(bladeUser.getRoleName(), new CharSequence[]{"administrator"})) {
            return null;
        }
        if (DataScopeEnum.CUSTOM == scopeTypeEnum) {
            whereSql = PlaceholderUtil.getDefaultResolver().resolveByMap(dataScope2.getScopeValue(), BeanUtil.toMap(bladeUser));
        } else if (DataScopeEnum.OWN == scopeTypeEnum) {
            ids.add(bladeUser.getUserId());
        } else if (DataScopeEnum.OWN_DEPT == scopeTypeEnum) {
            ids.addAll(Func.toLongList(bladeUser.getDeptId()));
        } else if (DataScopeEnum.OWN_DEPT_CHILD == scopeTypeEnum) {
            List<Long> deptIds = Func.toLongList(bladeUser.getDeptId());
            ids.addAll(deptIds);
            deptIds.forEach(deptId -> {
                List<Long> deptIdList = this.scopeModelHandler.getDeptAncestors(deptId);
                ids.addAll(deptIdList);
            });
        }
        return StringUtil.format(" select {} from ({}) scope " + whereSql, new Object[]{Func.toStr(dataScope2.getScopeField(), "*"), originalSql, dataScope2.getScopeColumn(), StringUtil.join(ids), dataScope2.getScopeColumn()});
    }
}