package com.icloud.server.web.shiro.config;


import com.icloud.server.scala.shiro.RolePermission;
import com.icloud.server.scala.shiro.User;
import com.icloud.server.scala.shiro.UserRepository;
import com.icloud.server.scala.shiro.UserRole;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 自定义realm
 */
@Service
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private UserRepository userrepository;

    /**
     * 进行权限校验的时候回调用
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("授权 doGetAuthorizationInfo");
         Object pp = principals.getPrimaryPrincipal();

        User pp1 = (User)pp;
        pp1.getId();
        User newUser = (User)principals.getPrimaryPrincipal();
        User user = userrepository.findByUsernameAndDfe(newUser.getUsername(),0);

        List<String> stringRoleList = new ArrayList<>();
        List<String> stringPermissionList = new ArrayList<>();


        Set<UserRole> roleList = user.getUserrole();

        for(UserRole role : roleList){
            stringRoleList.add(role.getRole().getName());

            Set<RolePermission> permissionList = role.getRole().rolepermission();

            for(RolePermission p: permissionList){
                if(p!=null){
                    stringPermissionList.add(p.permission().getName());
                }
            }

        }

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRoles(stringRoleList);
        simpleAuthorizationInfo.addStringPermissions(stringPermissionList);

        return simpleAuthorizationInfo;
    }









    /**
     * 用户登录的时候会调用
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        System.out.println("认证 doGetAuthenticationInfo");

        //从token获取用户信息，token代表用户输入
        String username = (String)token.getPrincipal();

        User user =  userrepository.findByUsernameAndDfe(username,0);

        //取密码
        String pwd = user.getPassword();
        if(pwd == null || "".equals(pwd)){
            return null;
        }

        return new SimpleAuthenticationInfo(user, user.getPassword(), this.getClass().getName());
    }
}
