All checks were successful
CI - Build & Test (develop) / build-and-test (pull_request) Successful in 33s
33 lines
1.2 KiB
Java
Executable File
33 lines
1.2 KiB
Java
Executable File
package io.gmss.fiscad.configuration;
|
|
|
|
import io.gmss.fiscad.persistence.repositories.user.UserRepository;
|
|
import io.gmss.fiscad.security.UserPrincipal;
|
|
import org.springframework.data.domain.AuditorAware;
|
|
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
import java.util.Optional;
|
|
|
|
public class AuditorAwareImpl implements AuditorAware<Long> {
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
public AuditorAwareImpl(UserRepository userRepository) {
|
|
this.userRepository = userRepository;
|
|
}
|
|
|
|
@Override
|
|
public Optional<Long> getCurrentAuditor() {
|
|
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
|
if (authentication == null || !authentication.isAuthenticated() || authentication instanceof AnonymousAuthenticationToken) {
|
|
return Optional.empty();
|
|
}
|
|
UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
|
|
//return userRepository.findByUsername(userPrincipal.getUsername()).get().getId();
|
|
return Optional.ofNullable(userPrincipal.getUser().getId());
|
|
}
|
|
}
|