Enhance user privacy checks and improve error handling in user services
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

This commit is contained in:
2026-01-16 17:15:25 +01:00
parent 8bace01238
commit f8c661ccb6
4 changed files with 115 additions and 62 deletions
@@ -2,9 +2,11 @@ package SuMoC.backend.user.service;
import SuMoC.backend.user.models.User;
import SuMoC.backend.user.repository.UserRepository;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ResponseStatusException;
@Component
public class CurrentUserService {
@@ -19,10 +21,14 @@ public class CurrentUserService {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth == null || !auth.isAuthenticated()) {
throw new RuntimeException("Nicht authentifiziert");
throw new ResponseStatusException(
HttpStatus.UNAUTHORIZED,
"Nicht authentifiziert");
}
return userRepository.findByEmail(auth.getName())
.orElseThrow(() -> new RuntimeException("User existiert nicht mehr"));
.orElseThrow(() -> new ResponseStatusException(
HttpStatus.NOT_FOUND,
"Der Nutzer existiert nicht"));
}
}