package com.izouma.jmrh.config;

import com.izouma.jmrh.domain.User;
import com.izouma.jmrh.utils.SecurityUtils;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import java.util.Optional;

@Component
public class SpringSecurityAuditorAware implements AuditorAware<String> {

    @Override
    @Nonnull
    public Optional<String> getCurrentAuditor() {
        String auditor = "system";
        User user = SecurityUtils.getAuthenticatedUser();
        if (user != null) {
            auditor = user.getNickname() + "(" + user.getId() + ")";
        }
        return Optional.of(auditor);
    }
}
