zhongrijian
2024-05-31 b81715414705f0f4489f6ddb583cc7fe432c556a
socpe 配置修改
2 files added
98 ■■■■■ changed files
src/main/java/org/springblade/common/config/BladeScopeDataConfig.java 20 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/common/handle/BladeScopeModelHandlerMaster.java 78 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/common/config/BladeScopeDataConfig.java
New file
@@ -0,0 +1,20 @@
package org.springblade.common.config;
import org.springblade.common.handle.BladeScopeModelHandlerMaster;
import org.springblade.core.datascope.handler.ScopeModelHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
@Configuration(proxyBeanMethods = false)
public class BladeScopeDataConfig {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Bean
    public ScopeModelHandler scopeModelHandler(){
        return new BladeScopeModelHandlerMaster(jdbcTemplate);
    }
}
src/main/java/org/springblade/common/handle/BladeScopeModelHandlerMaster.java
New file
@@ -0,0 +1,78 @@
package org.springblade.common.handle;
import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.RequiredArgsConstructor;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.datascope.constant.DataScopeConstant;
import org.springblade.core.datascope.handler.ScopeModelHandler;
import org.springblade.core.datascope.model.DataScopeModel;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@DS("master")
@RequiredArgsConstructor
public class BladeScopeModelHandlerMaster implements ScopeModelHandler {
    private static final String SCOPE_CACHE_CODE = "dataScope:code:";
    private static final String SCOPE_CACHE_CLASS = "dataScope:class:";
    private static final String DEPT_CACHE_ANCESTORS = "dept:ancestors:";
    private static final DataScopeModel SEARCHED_DATA_SCOPE_MODEL;
    private final JdbcTemplate jdbcTemplate;
    public DataScopeModel getDataScopeByMapper(String mapperId, String roleId) {
        List<Object> args = new ArrayList(Collections.singletonList(mapperId));
        List<Long> roleIds = Func.toLongList(roleId);
        args.addAll(roleIds);
        DataScopeModel dataScope = (DataScopeModel) CacheUtil.get("blade:sys", "dataScope:class:", mapperId + ":" + roleId, DataScopeModel.class, Boolean.FALSE);
        if (dataScope == null || !dataScope.getSearched()) {
            List<DataScopeModel> list = this.jdbcTemplate.query(DataScopeConstant.dataByMapper(roleIds.size()), args.toArray(), new BeanPropertyRowMapper(DataScopeModel.class));
            if (CollectionUtil.isNotEmpty(list)) {
                dataScope = (DataScopeModel)list.iterator().next();
                dataScope.setSearched(Boolean.TRUE);
            } else {
                dataScope = SEARCHED_DATA_SCOPE_MODEL;
            }
            CacheUtil.put("blade:sys", "dataScope:class:", mapperId + ":" + roleId, dataScope, Boolean.FALSE);
        }
        return StringUtil.isNotBlank(dataScope.getResourceCode()) ? dataScope : null;
    }
    public DataScopeModel getDataScopeByCode(String code) {
        DataScopeModel dataScope = (DataScopeModel)CacheUtil.get("blade:sys", "dataScope:code:", code, DataScopeModel.class, Boolean.FALSE);
        if (dataScope == null || !dataScope.getSearched()) {
            List<DataScopeModel> list = this.jdbcTemplate.query("select resource_code, scope_column, scope_field, scope_type, scope_value from blade_scope_data where resource_code = ?", new Object[]{code}, new BeanPropertyRowMapper(DataScopeModel.class));
            if (CollectionUtil.isNotEmpty(list)) {
                dataScope = (DataScopeModel)list.iterator().next();
                dataScope.setSearched(Boolean.TRUE);
            } else {
                dataScope = SEARCHED_DATA_SCOPE_MODEL;
            }
            CacheUtil.put("blade:sys", "dataScope:code:", code, dataScope, Boolean.FALSE);
        }
        return StringUtil.isNotBlank(dataScope.getResourceCode()) ? dataScope : null;
    }
    public List<Long> getDeptAncestors(Long deptId) {
        List ancestors = (List)CacheUtil.get("blade:sys", "dept:ancestors:", deptId, List.class);
        if (CollectionUtil.isEmpty(ancestors)) {
            ancestors = this.jdbcTemplate.queryForList("select id from blade_dept where ancestors like concat(concat('%', ?),'%') and is_deleted = 0", new Object[]{deptId}, Long.class);
            CacheUtil.put("blade:sys", "dept:ancestors:", deptId, ancestors);
        }
        return ancestors;
    }
    static {
        SEARCHED_DATA_SCOPE_MODEL = new DataScopeModel(Boolean.TRUE);
    }
}