package com.izouma.nineth.utils;

import com.izouma.nineth.domain.User;
import com.izouma.nineth.enums.AuthorityName;
import com.izouma.nineth.security.Authority;
import com.izouma.nineth.security.JwtUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils {
    public static User getAuthenticatedUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = null;
        if (authentication != null && authentication.getPrincipal() instanceof JwtUser) {
            user = ((JwtUser) authentication.getPrincipal()).getUser();
        }
        return user;
    }

    public static boolean hasRole(AuthorityName name){
        User user = getAuthenticatedUser();
        if (null==user) return false;
        return user.getAuthorities().contains(Authority.get(name));
    }
}
