package fastweb.controller;

import fastweb.domain.usertype.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class BaseController {

    /**
     * 获取登录用户
     *
     * @return 登录用户
     */
    public User getLoginUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            Object details = authentication.getDetails();
            if (details instanceof User) {
                return (User) details;
            }
        }
        return null;
    }

    /**
     * 获取登录用户ID
     *
     * @return 登录用户ID
     */
    public String getLoginUserId() {
        User sysUser = getLoginUser();
        if (sysUser != null) {
            return sysUser.getId();
        } else {
            return null;
        }
    }
}
