package com.huimv.employment.security;

/**
 * 请求级登录用户上下文。
 */
public final class LoginUserHolder {

    private static final ThreadLocal<LoginUser> CONTEXT = new ThreadLocal<>();

    private LoginUserHolder() {
    }

    public static void set(LoginUser loginUser) {
        CONTEXT.set(loginUser);
    }

    public static LoginUser get() {
        return CONTEXT.get();
    }

    public static LoginUser require() {
        LoginUser loginUser = CONTEXT.get();
        if (loginUser == null) {
            throw new IllegalStateException("LoginUser not found in context");
        }
        return loginUser;
    }

    public static void clear() {
        CONTEXT.remove();
    }
}
