package com.izouma.immall.config;

import com.izouma.immall.domain.BackUser;
import com.izouma.immall.domain.User;
import com.izouma.immall.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";
        BackUser user = SecurityUtils.getAuthenticatedUser();
        if (user != null) {
            auditor = user.getNickname() + "(" + user.getId() + ")";
        }
        return Optional.of(auditor);
    }
}
