潘志宝
2024-08-15 81c220fd9e0ea6c8ee84c9b766885b0322b4038c
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.datapermission.config;
H 2
3 import cn.hutool.extra.spring.SpringUtil;
4 import com.iailab.framework.datapermission.core.rule.dept.DeptDataPermissionRule;
5 import com.iailab.framework.datapermission.core.rule.dept.DeptDataPermissionRuleCustomizer;
6 import com.iailab.framework.security.core.LoginUser;
7 import com.iailab.module.system.api.permission.PermissionApi;
8 import org.springframework.boot.autoconfigure.AutoConfiguration;
9 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
10 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
11 import org.springframework.context.annotation.Bean;
12
13 import java.util.List;
14
15 /**
16  * 基于部门的数据权限 AutoConfiguration
17  *
18  * @author iailab
19  */
20 @AutoConfiguration
21 @ConditionalOnClass(LoginUser.class)
22 @ConditionalOnBean(value = DeptDataPermissionRuleCustomizer.class)
23 public class IailabDeptDataPermissionAutoConfiguration {
24
25     @Bean
26     public DeptDataPermissionRule deptDataPermissionRule(PermissionApi permissionApi,
27                                                          List<DeptDataPermissionRuleCustomizer> customizers) {
28         // Cloud 专属逻辑:优先使用本地的 PermissionApi 实现类,而不是 Feign 调用
29         // 原因:在创建租户时,租户还没创建好,导致 Feign 调用获取数据权限时,报“租户不存在”的错误
30         try {
31             PermissionApi permissionApiImpl = SpringUtil.getBean("permissionApiImpl", PermissionApi.class);
32             if (permissionApiImpl != null) {
33                 permissionApi = permissionApiImpl;
34             }
35         } catch (Exception ignored) {}
36
37         // 创建 DeptDataPermissionRule 对象
38         DeptDataPermissionRule rule = new DeptDataPermissionRule(permissionApi);
39         // 补全表配置
40         customizers.forEach(customizer -> customizer.customize(rule));
41         return rule;
42     }
43
44 }