
package com.palmnest.application.core.util;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.palmnest.model.User;

/**
 * SpringSecurity的工具类.
 */
public class SpringSecurityUtils {

	public static String getCurrentUserName() {
		User currentUser = getCurrentUser();
		if (currentUser == null || !StringUtil.isValidString(currentUser.getUsername())) {
			return "";
		}
		return currentUser.getUsername();
	}

	public static User getCurrentUser() {
		User currentUser = null;
		SecurityContext ctx = SecurityContextHolder.getContext();
		if (ctx != null) {
			if (ctx.getAuthentication() != null && ctx.getAuthentication().getPrincipal() != null && ctx.getAuthentication().getPrincipal() instanceof UserDetails) {
				currentUser = (User) ctx.getAuthentication().getPrincipal();
			} else if (ctx.getAuthentication() != null && ctx.getAuthentication().getDetails() != null && ctx.getAuthentication().getDetails() instanceof UserDetails) {
				currentUser = (User) ctx.getAuthentication().getDetails();
			} else if (ctx.getAuthentication() != null && ctx.getAuthentication() instanceof UsernamePasswordAuthenticationToken) {
				currentUser = new User(ctx.getAuthentication().getPrincipal().toString());
			}
		}
		return currentUser;
	}

}