package cn.com.qmth.examcloud.core.questions.starter.config;

import java.util.Optional;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.web.enums.HttpServletRequestAttribute;
import cn.com.qmth.examcloud.web.support.ServletUtil;

@Configuration
public class UserIDAuditorBean implements AuditorAware<Long> {
    @Override
    public Optional<Long> getCurrentAuditor() {
    	ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    	if(requestAttributes==null) {
    		return Optional.empty();
    	}
    	User accessUser = (User) ServletUtil.getRequest()
                .getAttribute(HttpServletRequestAttribute.$_ACCESS_USER.name());
        if (null == accessUser) {
        	return Optional.empty();
        }
        return Optional.of(accessUser.getUserId());
    }
}