package com.simuwang.base.components;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.simuwang.base.config.DaqProperties;
import com.simuwang.shiro.core.ShiroUser;
import com.simuwang.shiro.core.adapter.LoginAuthAdapter;

import java.util.List;

/**
 * 自定义的登录认证适配器实现，从数据库获取数据并且bean注入要调整为当前实现
 */
public class ShiroLoginAuthAdapter implements LoginAuthAdapter {
    private final DaqProperties properties;

    public ShiroLoginAuthAdapter(DaqProperties properties) {
        this.properties = properties;
    }

    @Override
    public ShiroUser findUserByUsername(String username) {
        ShiroUser sysUser = new ShiroUser();
        sysUser.setUserId("0");
        sysUser.setUsername("system");
        String encryptBase64Pwd = new RSA(null, this.properties.getSecurityRsa().getPublicKey()).encryptBase64("123456", KeyType.PublicKey);
        sysUser.setPassword(encryptBase64Pwd);
        return sysUser;
    }

    @Override
    public List<String> findRoleByUserId(String userId) {
        return ListUtil.toList("SYSTEM");
    }

    @Override
    public List<String> findPermissionsByUserId(String userId) {
        return ListUtil.toList("RS:ALL", "RS:TEST");
    }
}
