All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.houbb.current.user.utils.CurrentUserUtil Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package com.github.houbb.current.user.utils;

import com.github.houbb.current.user.dto.CurrentUserInfo;
import com.github.houbb.heaven.annotation.CommonEager;
import com.github.houbb.heaven.util.lang.StringUtil;

import java.util.Collections;
import java.util.List;

/**
 * 当前用户工具类
 * @author binbin.hou
 * @since 0.0.19
 */
@CommonEager
public final class CurrentUserUtil {

    private CurrentUserUtil(){}

    private static final ThreadLocal THREAD_LOCAL = new ThreadLocal<>();

    /**
     * 设置当前用户信息
     * @param currentUserInfo 当前用户信息
     * @since 0.0.19
     */
    public static void set(CurrentUserInfo currentUserInfo) {
        THREAD_LOCAL.set(currentUserInfo);
    }

    public static CurrentUserInfo get() {
        return THREAD_LOCAL.get();
    }

    public static void remove() {
        THREAD_LOCAL.remove();;
    }

    /**
     * 获取当前用户名称
     * @return 用户
     * @since 0.0.19
     */
    public static String getCurrentUserName() {
        CurrentUserInfo userInfo = get();
        if(userInfo == null) {
            return StringUtil.BLANK;
        }

        return userInfo.getUserName();
    }

    /**
     * 获取当前用户标识
     * @return 用户
     * @since 1.2.0
     */
    public static String getCurrentUserId() {
        CurrentUserInfo userInfo = get();
        if(userInfo == null) {
            return StringUtil.BLANK;
        }

        return userInfo.getUserId();
    }

    /**
     * 获取当前用户 IP
     * @return 用户
     * @since 1.2.0
     */
    public static String getCurrentIp() {
        CurrentUserInfo userInfo = get();
        if(userInfo == null) {
            return StringUtil.BLANK;
        }

        return userInfo.getIp();
    }

    /**
     * 获取当前用户头像
     * @return 用户
     * @since 1.2.0
     */
    public static String getCurrentAvatar() {
        CurrentUserInfo userInfo = get();
        if(userInfo == null) {
            return StringUtil.BLANK;
        }

        return userInfo.getAvatar();
    }

    /**
     * 获取当前用户权限
     * @return 用户
     * @since 1.2.0
     */
    public static List getCurrentPrivileges() {
        CurrentUserInfo userInfo = get();
        if(userInfo == null) {
            return Collections.emptyList();
        }

        return userInfo.getPrivileges();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy