package com.simuwang.shiro.core.bridge;

import com.simuwang.shiro.core.ShiroUser;
import com.simuwang.shiro.core.adapter.LoginAuthAdapter;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wangzaijun
 * @date 2024/9/6 17:42
 * @description 授权与认证的用户信息桥接接口
 */
@Service
public class AuthBridgeService {
    private final LoginAuthAdapter loginAuthAdapter;

    public AuthBridgeService(LoginAuthAdapter loginAuthAdapter) {
        this.loginAuthAdapter = loginAuthAdapter;
    }

    public ShiroUser findUserByUsername(String username) {
        return this.loginAuthAdapter.findUserByUsername(username);
    }

    public AuthorizationInfo getAuthorizationInfo(ShiroUser shiroUser) {
        //查询用户对应的角色标识
        List<String> roleList = this.findRoles(shiroUser.getUserId());
        //查询用户对于的资源标识
        List<String> resourcesList = this.findPermissions(shiroUser.getUserId());
        //构建鉴权信息对象
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRoles(roleList);
        simpleAuthorizationInfo.addStringPermissions(resourcesList);
        return simpleAuthorizationInfo;
    }

    public List<String> findRoles(Integer userId) {
        return this.loginAuthAdapter.findRoleByUserId(userId);
    }

    public List<String> findPermissions(Integer userId) {
        return this.loginAuthAdapter.findPermissionsByUserId(userId);
    }
}
