package com.jeelowcode.tool.framework.datapermission.config;

import com.jeelowcode.tool.framework.datapermission.core.rule.dept.DeptDataPermissionRule;
import com.jeelowcode.tool.framework.datapermission.core.rule.dept.DeptDataPermissionRuleCustomizer;
import com.jeelowcode.tool.framework.security.core.LoginUser;
import com.jeelowcode.service.system.api.IApiPermissionApi;
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 芋道源码
 */
@AutoConfiguration
@ConditionalOnClass(LoginUser.class)
@ConditionalOnBean(value = {IApiPermissionApi.class, DeptDataPermissionRuleCustomizer.class})
public class DeptDataPermissionAutoConfiguration {

    @Bean
    public DeptDataPermissionRule deptDataPermissionRule(IApiPermissionApi apiPermissionApi,
                                                         List<DeptDataPermissionRuleCustomizer> customizers) {
        // 创建 DeptDataPermissionRule 对象
        DeptDataPermissionRule rule = new DeptDataPermissionRule(apiPermissionApi);
        // 补全表配置
        customizers.forEach(customizer -> customizer.customize(rule));
        return rule;
    }

}
