user controller: allow updating a users private name
This commit is contained in:
@@ -3,14 +3,11 @@ package SuMoC.backend.trip;
|
||||
|
||||
import SuMoC.backend.trip.dtos.NewTripDTO;
|
||||
import SuMoC.backend.trip.dtos.TripDTO;
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import jakarta.validation.Valid;
|
||||
import org.apache.commons.lang3.NotImplementedException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -19,7 +16,7 @@ import java.util.List;
|
||||
@RestController
|
||||
@RequestMapping("/trip")
|
||||
public class TripController {
|
||||
private TripService tripService;
|
||||
private final TripService tripService;
|
||||
|
||||
@Autowired
|
||||
public TripController(TripService tripService) {
|
||||
|
||||
@@ -16,15 +16,13 @@ import org.springframework.web.bind.annotation.*;
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
private UserService userService;
|
||||
private final UserService userService;
|
||||
|
||||
@Autowired
|
||||
public UserController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "Nutzer fragt ein fremdes Nutzerprofil an", description = "Ein Nutzer kann das Profil eines anderen Nutzers anfragen, um dessen öffentliche Informationen einzusehen.")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Nutzerprofil erfolgreich abgerufen",
|
||||
@@ -37,7 +35,6 @@ public class UserController {
|
||||
return new ResponseEntity<>(this.userService.getUser(id), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "Nutzer löscht sein eigenes Profil", description = "Ein Nutzer kann sein eigenes Profil löschen. Dies entfernt alle seine Daten aus dem System.")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Nutzerprofil erfolgreich gelöscht",
|
||||
@@ -51,11 +48,16 @@ public class UserController {
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
|
||||
public void updateUser(Long id){
|
||||
public void updateUser(Long id) {
|
||||
|
||||
}
|
||||
|
||||
@Operation(summary = "Update a users private name.", description = "A user can change it's own private name that is shown to friends.")
|
||||
@PutMapping("/changePrivateName/{id}")
|
||||
public ResponseEntity<Void> changePrivateName(@PathVariable Long id, @RequestParam String newPrivateName) {
|
||||
this.userService.changePrivateName(id, newPrivateName);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@Operation(summary = "Nutzer ändert sein Passwort", description = "Ein Nutzer kann sein Passwort ändern, um die Sicherheit seines Kontos zu gewährleisten.")
|
||||
@ApiResponses(value = {
|
||||
@@ -65,12 +67,11 @@ public class UserController {
|
||||
content = @Content(mediaType = "application/json"))
|
||||
})
|
||||
@PutMapping("/changePassword/{id}")
|
||||
public ResponseEntity<Object> changePassword(@PathVariable Long id, @RequestParam String newPassword){
|
||||
public ResponseEntity<Object> changePassword(@PathVariable Long id, @RequestParam String newPassword) {
|
||||
this.userService.changePassword(id, newPassword);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "Nutzer ändert seine E-Mail-Adresse", description = "Ein Nutzer kann seine E-Mail-Adresse ändern, um sicherzustellen, dass seine Kontaktdaten aktuell sind.")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "E-Mail-Adresse erfolgreich geändert",
|
||||
@@ -79,12 +80,11 @@ public class UserController {
|
||||
content = @Content(mediaType = "application/json"))
|
||||
})
|
||||
@PutMapping("/changeEmail/{id}")
|
||||
public ResponseEntity<Object> changeEmail(@PathVariable Long id, @RequestParam String newEmail){
|
||||
public ResponseEntity<Object> changeEmail(@PathVariable Long id, @RequestParam String newEmail) {
|
||||
this.userService.changeEmail(id, newEmail);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "Nutzer ändert seine Sichtbarkeits- und Datenschutzeinstellungen", description = "Ein Nutzer kann seine Sichtbarkeits- und Datenschutzeinstellungen ändern, um zu steuern, wer seine Informationen sehen kann.")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Sichtbarkeits- und Datenschutzeinstellungen erfolgreich geändert",
|
||||
@@ -93,7 +93,7 @@ public class UserController {
|
||||
content = @Content(mediaType = "application/json"))
|
||||
})
|
||||
@PutMapping("/changeVisibilityPrivacy")
|
||||
public void changeVisibilityPrivacy(@RequestParam VisibilityPrivacy visibilityPrivacy){
|
||||
public void changeVisibilityPrivacy(@RequestParam VisibilityPrivacy visibilityPrivacy) {
|
||||
this.userService.changeVisibilityPrivacy(visibilityPrivacy);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package SuMoC.backend.user.service;
|
||||
|
||||
import SuMoC.backend.user.models.Friendship;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import SuMoC.backend.user.UserRepository;
|
||||
import SuMoC.backend.user.dtos.MinimalUserDTO;
|
||||
import SuMoC.backend.user.enums.FriendShipStatus;
|
||||
import SuMoC.backend.user.enums.RoleName;
|
||||
import SuMoC.backend.user.enums.VisibilityPrivacy;
|
||||
import SuMoC.backend.user.UserRepository;
|
||||
import SuMoC.backend.user.models.Friendship;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
@@ -16,8 +16,7 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
@Transactional
|
||||
public class UserService {
|
||||
|
||||
private UserRepository userRepository;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
public UserService(UserRepository userRepository) {
|
||||
@@ -87,7 +86,6 @@ public class UserService {
|
||||
}
|
||||
|
||||
public void changeEmail(Long id, String newEmail) {
|
||||
|
||||
User sender = getSender();
|
||||
if (!sender.getId().equals(id)) {
|
||||
throw new RuntimeException("Ein Nutzer kann nur seine eigene E-Mail-Adresse ändern.");
|
||||
@@ -101,4 +99,14 @@ public class UserService {
|
||||
User user = getSender();
|
||||
user.setVisibilityPrivacy(visibilityPrivacy);
|
||||
}
|
||||
|
||||
public void changePrivateName(Long id, String newPrivateName) {
|
||||
User sender = getSender();
|
||||
if (!sender.getId().equals(id) && !sender.getRoles().contains(RoleName.ADMIN)) {
|
||||
throw new RuntimeException("Not allowed to change private name of other users unless admin.");
|
||||
}
|
||||
User userToChange = userRepository.findById(id).orElseThrow();
|
||||
userToChange.setPrivateName(newPrivateName);
|
||||
userRepository.save(userToChange);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user