package com.lightinit.hsdataportal.spring;

import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.dictionary.DicShiroTokenUserType;
import com.lightinit.hsdataportal.entity.Admin;
import com.lightinit.hsdataportal.entity.SentryRole;
import com.lightinit.hsdataportal.entity.SentryRoleAction;
import com.lightinit.hsdataportal.service.IAdminService;
import com.lightinit.hsdataportal.service.IRoleService;
import com.lightinit.hsdataportal.service.IShiroPermissionsService;
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.Permission;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.authz.permission.WildcardPermission;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author WangYao
 * @date 2018/7/1 10:03
 * @description 后台用户Realm
 */
public class AdminRealm extends AuthorizingRealm {
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IShiroPermissionsService shiroPermissionsService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals){
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //暂时不加权限
        Set<Permission> actions = new HashSet<Permission>();
        Permission wildcardPermission = new WildcardPermission(DicPlatformType.SHARE);
        actions.add(wildcardPermission);
        authorizationInfo.setObjectPermissions(actions);
        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        if (!(authenticationToken instanceof  UsernamePasswordUserTypeToken)){
            return null;
        }
        UsernamePasswordUserTypeToken token = (UsernamePasswordUserTypeToken) authenticationToken;
        if (!token.getUserType().equals(DicShiroTokenUserType.ADMIN)){
            return null;
        }
        String username = (String)token.getPrincipal();
        // TODO: 2017/4/7 验证当前用户的是否合法
        Admin adminEntity=adminService.SelectOne(username);
        if(adminEntity==null)
            return null;
//        adminEntity.setUsername("admin");
//        adminEntity.setId(1L);
//        String salt = new SecureRandomNumberGenerator().nextBytes().toHex();
//        String password_cipherText= new Md5Hash("admin",username+salt,3).toHex();
//        adminEntity.setPassword(password_cipherText);


        //交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配，如果觉得人家的不好可以自定义实现
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                adminEntity.getUsername(), //用户名
                adminEntity.getPassword(), //密码
                ByteSource.Util.bytes(username+adminEntity.getSalt()),//salt=username+salt
                getName()  //realm name
        );
        return authenticationInfo;
    }

    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }
}
