package com.lideeyunji.tool.framework.permission.config;

import com.lideeyunji.tool.framework.permission.core.rule.dept.DeptDataPermissionRule;
import com.lideeyunji.tool.framework.permission.core.rule.dept.DeptDataPermissionRuleCustomizer;
import com.lideeyunji.tool.framework.security.core.LoginUser;
import com.lideeyunji.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;
    }

}
