Enhance user privacy checks and improve error handling in user services
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user