package com.eactive.httpmockserver.common.util;

import com.eactive.httpmockserver.user.entity.AnonymousUser;
import com.eactive.httpmockserver.user.entity.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtil {

    private SecurityUtil() {
        // Utility class
        throw new IllegalStateException("Utility class");
    }

    public static final String ANONYMOUS_USER = "anonymousUser";

    public static User getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && !authentication.getPrincipal().equals(ANONYMOUS_USER)) {
            return ((User) authentication.getDetails());
        }
        return new AnonymousUser();
    }

    public static String getCurrentUserEsntlId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && !authentication.getPrincipal().equals(ANONYMOUS_USER)) {
            return ((User) authentication.getDetails()).getEsntlId();
        }
        return "ANONYMOUS";
    }

}
