package com.czg.config;

import cn.dev33.satoken.stp.StpInterface;
import com.czg.sa.StpKit;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义权限加载接口实现类
 *
 * @author Administrator
 */
@Component    // 保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        String key1 = "sa:permissionList:" + loginType + ":" + loginId;
        List<String> list = getCashInfo(key1);
        if (list != null) {
            return list;
        }

        return new ArrayList<>();
    }

    private static List<String> getCashInfo(String key) {
        Object value = StpKit.USER.getSession().get(key);
        if (value instanceof List<?> list) {
            return (List<String>) list;
        }
        return null;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        String key = "sa:roleList:" + loginType + ":" + loginId;
        List<String> list = getCashInfo(key);
        if (list != null) {
            return list;
        }
        return new ArrayList<>();
    }

}

