package com.hys.app.framework.context.user;

import com.hys.app.framework.security.model.Admin;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 用户上下文
 * Created by kingapex on 2018/3/12.
 *
 * @author kingapex
 * @version 1.0
 * @since 7.0.0
 * 2018/3/12
 */
public class AdminUserContext {

    /**
     * 获取当前管理员
     *
     * @return
     */
    public static Admin getAdmin() {


        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return null;
        }
        Object someOne = authentication.getDetails();
        if (someOne != null && someOne instanceof Admin) {
            return (Admin) someOne;
        }
        return null;


    }

    /**
     * 获取当前管理员id
     *
     * @return
     */
    public static Long getAdminUserId() {
        Admin admin = getAdmin();
        if(admin == null){
            return null;
        }
        return admin.getUid();
    }

    /**
     * 获取当前管理员账号
     *
     * @return
     */
    public static String getAdminUserName() {
        Admin admin = getAdmin();
        if(admin == null){
            return null;
        }
        return admin.getUsername();
    }


}
