潘志宝
2024-08-15 81c220fd9e0ea6c8ee84c9b766885b0322b4038c
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
package com.iailab.framework.datapermission.config;
 
import cn.hutool.extra.spring.SpringUtil;
import com.iailab.framework.datapermission.core.rule.dept.DeptDataPermissionRule;
import com.iailab.framework.datapermission.core.rule.dept.DeptDataPermissionRuleCustomizer;
import com.iailab.framework.security.core.LoginUser;
import com.iailab.module.system.api.permission.PermissionApi;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
 
import java.util.List;
 
/**
 * 基于部门的数据权限 AutoConfiguration
 *
 * @author iailab
 */
@AutoConfiguration
@ConditionalOnClass(LoginUser.class)
@ConditionalOnBean(value = DeptDataPermissionRuleCustomizer.class)
public class IailabDeptDataPermissionAutoConfiguration {
 
    @Bean
    public DeptDataPermissionRule deptDataPermissionRule(PermissionApi permissionApi,
                                                         List<DeptDataPermissionRuleCustomizer> customizers) {
        // Cloud 专属逻辑:优先使用本地的 PermissionApi 实现类,而不是 Feign 调用
        // 原因:在创建租户时,租户还没创建好,导致 Feign 调用获取数据权限时,报“租户不存在”的错误
        try {
            PermissionApi permissionApiImpl = SpringUtil.getBean("permissionApiImpl", PermissionApi.class);
            if (permissionApiImpl != null) {
                permissionApi = permissionApiImpl;
            }
        } catch (Exception ignored) {}
 
        // 创建 DeptDataPermissionRule 对象
        DeptDataPermissionRule rule = new DeptDataPermissionRule(permissionApi);
        // 补全表配置
        customizers.forEach(customizer -> customizer.customize(rule));
        return rule;
    }
 
}