Bugfix: user can change email and password accordingly
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
@@ -48,6 +48,30 @@ public class TokenService {
|
||||
return jwtEncoder.encode(JwtEncoderParameters.from(claims)).getTokenValue();
|
||||
}
|
||||
|
||||
|
||||
// Re-Authentification after change of email
|
||||
public String generateUserJwt(Authentication auth, UserDTO userDTO, String email){
|
||||
|
||||
System.out.println(auth.getName());
|
||||
|
||||
Instant now = Instant.now();
|
||||
|
||||
List<String> roles = auth.getAuthorities().stream()
|
||||
.map(authority -> authority.getAuthority().replace("ROLE_", "")) // <- WICHTIG!
|
||||
.collect(Collectors.toList());
|
||||
|
||||
//CLAIMS define the Payload of the JWT, stores user Data etc. Payload is not encrypted, so no secret data shall be saved stored in a jwt
|
||||
JwtClaimsSet claims = JwtClaimsSet.builder()
|
||||
.issuer("self")
|
||||
.issuedAt(now)
|
||||
.subject(email)
|
||||
.claim("roles",roles)
|
||||
.claim("userId", userDTO.getId())
|
||||
.claim("privateName", userDTO.getPrivateName())
|
||||
.build();
|
||||
return jwtEncoder.encode(JwtEncoderParameters.from(claims)).getTokenValue();
|
||||
}
|
||||
|
||||
//TODO: change return type
|
||||
public void decodeJwt(String jwt){
|
||||
System.out.println("###############DECODED JWT##################");
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package SuMoC.backend.user.controller;
|
||||
|
||||
import SuMoC.backend.security.dtos.LoginResponseDTO;
|
||||
import SuMoC.backend.user.dtos.MaximalUserDTO;
|
||||
import SuMoC.backend.user.dtos.MinimalUserDTO;
|
||||
import SuMoC.backend.user.enums.VisibilityPrivacy;
|
||||
import SuMoC.backend.user.service.UserService;
|
||||
@@ -39,6 +41,18 @@ public class UserController {
|
||||
return new ResponseEntity<>(this.userService.getUser(id), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Operation(summary = "Nutzer fragt sein eigenes Profil an", description = "Ein Nutzer kann sein eigenes Profil anfragen, um dessen Informationen einzusehen.")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Nutzerprofil erfolgreich abgerufen",
|
||||
content = @Content(mediaType = "application/json", schema = @Schema(implementation = MinimalUserDTO.class))),
|
||||
@ApiResponse(responseCode = "404", description = "Nutzerprofil nicht gefunden",
|
||||
content = @Content(mediaType = "application/json"))
|
||||
})
|
||||
@GetMapping("/profile")
|
||||
public ResponseEntity<MaximalUserDTO> getUser() {
|
||||
return new ResponseEntity<>(this.userService.getSelf(), 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",
|
||||
@@ -57,10 +71,10 @@ public class UserController {
|
||||
}
|
||||
|
||||
@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 @NotNull Long id,
|
||||
@PutMapping("/setPrivateName/{id}")
|
||||
public ResponseEntity<Void> setPrivateName(@PathVariable @NotNull Long id,
|
||||
@RequestParam @NotBlank String newPrivateName) {
|
||||
this.userService.changePrivateName(id, newPrivateName);
|
||||
this.userService.setPrivateName(id, newPrivateName);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@@ -71,11 +85,10 @@ public class UserController {
|
||||
@ApiResponse(responseCode = "400", description = "Ungültige Eingabedaten",
|
||||
content = @Content(mediaType = "application/json"))
|
||||
})
|
||||
@PutMapping("/changePassword/{id}")
|
||||
public ResponseEntity<Object> changePassword(@PathVariable @NotNull Long id,
|
||||
@RequestParam @NotBlank String newPassword) {
|
||||
this.userService.changePassword(id, newPassword);
|
||||
return ResponseEntity.ok().build();
|
||||
@PutMapping("/setPassword/{id}")
|
||||
public ResponseEntity<LoginResponseDTO> setPassword(@PathVariable @NotNull Long id,
|
||||
@RequestParam @NotBlank String newPassword) {
|
||||
return new ResponseEntity<>(this.userService.setPassword(id, newPassword), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Operation(summary = "Nutzer ändert seine E-Mail-Adresse", description = "Ein Nutzer kann seine E-Mail-Adresse ändern, um sicherzustellen, dass seine Kontaktdaten aktuell sind.")
|
||||
@@ -85,11 +98,10 @@ public class UserController {
|
||||
@ApiResponse(responseCode = "400", description = "Ungültige Eingabedaten",
|
||||
content = @Content(mediaType = "application/json"))
|
||||
})
|
||||
@PutMapping("/changeEmail/{id}")
|
||||
public ResponseEntity<Object> changeEmail(@PathVariable @NotNull Long id,
|
||||
@PutMapping("/setEmail/{id}")
|
||||
public ResponseEntity<Object> setEmail(@PathVariable @NotNull Long id,
|
||||
@RequestParam @NotBlank String newEmail) {
|
||||
this.userService.changeEmail(id, newEmail);
|
||||
return ResponseEntity.ok().build();
|
||||
return new ResponseEntity<>(this.userService.setEmail(id, newEmail), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@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.")
|
||||
@@ -99,9 +111,9 @@ public class UserController {
|
||||
@ApiResponse(responseCode = "400", description = "Ungültige Eingabedaten",
|
||||
content = @Content(mediaType = "application/json"))
|
||||
})
|
||||
@PutMapping("/changeVisibilityPrivacy")
|
||||
public ResponseEntity<Void> changeVisibilityPrivacy(@RequestParam @NotNull VisibilityPrivacy visibilityPrivacy) {
|
||||
this.userService.changeVisibilityPrivacy(visibilityPrivacy);
|
||||
@PutMapping("/setVisibilityPrivacy")
|
||||
public ResponseEntity<Void> setVisibilityPrivacy(@RequestParam @NotNull VisibilityPrivacy visibilityPrivacy) {
|
||||
this.userService.setVisibilityPrivacy(visibilityPrivacy);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package SuMoC.backend.user.dtos;
|
||||
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import SuMoC.backend.user.enums.VisibilityPrivacy;
|
||||
import SuMoC.backend.user.models.Friendship;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class MaximalUserDTO {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String publicName;
|
||||
|
||||
private String privateName;
|
||||
|
||||
private String email;
|
||||
|
||||
private VisibilityPrivacy visibilityPrivacy;
|
||||
|
||||
private Set<Trip> trips;
|
||||
|
||||
private Set<Friendship> friendships;
|
||||
|
||||
public MaximalUserDTO() {
|
||||
}
|
||||
|
||||
public MaximalUserDTO(Long id, String publicName, String privateName, String email, VisibilityPrivacy visibilityPrivacy, Set<Trip> trips, Set<Friendship> friendships) {
|
||||
this.id = id;
|
||||
this.publicName = publicName;
|
||||
this.privateName = privateName;
|
||||
this.email = email;
|
||||
this.visibilityPrivacy = visibilityPrivacy;
|
||||
this.trips = trips;
|
||||
this.friendships = friendships;
|
||||
}
|
||||
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getPublicName() {
|
||||
return publicName;
|
||||
}
|
||||
|
||||
public void setPublicName(String publicName) {
|
||||
this.publicName = publicName;
|
||||
}
|
||||
|
||||
public String getPrivateName() {
|
||||
return privateName;
|
||||
}
|
||||
|
||||
public void setPrivateName(String privateName) {
|
||||
this.privateName = privateName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public VisibilityPrivacy getVisibilityPrivacy() {
|
||||
return visibilityPrivacy;
|
||||
}
|
||||
|
||||
public void setVisibilityPrivacy(VisibilityPrivacy visibilityPrivacy) {
|
||||
this.visibilityPrivacy = visibilityPrivacy;
|
||||
}
|
||||
|
||||
public Set<Trip> getTrips() {
|
||||
return trips;
|
||||
}
|
||||
|
||||
public void setTrips(Set<Trip> trips) {
|
||||
this.trips = trips;
|
||||
}
|
||||
|
||||
public Set<Friendship> getFriendships() {
|
||||
return friendships;
|
||||
}
|
||||
|
||||
public void setFriendships(Set<Friendship> friendships) {
|
||||
this.friendships = friendships;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,11 @@ import SuMoC.backend.user.dtos.FriendshipDTO;
|
||||
import SuMoC.backend.user.models.Friendship;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper(componentModel = "spring", uses = UserMapper.class)
|
||||
public interface FriendshipMapper {
|
||||
FriendshipDTO toFriendshipDTO(Friendship friendship);
|
||||
|
||||
List<FriendshipDTO> toFriendshipDTOs(List<Friendship> friendships);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package SuMoC.backend.user.mapper;
|
||||
|
||||
|
||||
import SuMoC.backend.user.dtos.MaximalUserDTO;
|
||||
import SuMoC.backend.user.dtos.UserDTO;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import org.mapstruct.Mapper;
|
||||
@@ -10,6 +11,8 @@ public interface UserMapper {
|
||||
UserDTO toUserDTO(User user);
|
||||
|
||||
User toUser(UserDTO userDTO);
|
||||
|
||||
MaximalUserDTO toMaximalUserDTO(User user);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
package SuMoC.backend.user.service;
|
||||
|
||||
import SuMoC.backend.security.dtos.LoginResponseDTO;
|
||||
import SuMoC.backend.security.services.TokenService;
|
||||
import SuMoC.backend.trip.TripMapper;
|
||||
import SuMoC.backend.user.dtos.MaximalUserDTO;
|
||||
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.mapper.FriendshipMapper;
|
||||
import SuMoC.backend.user.mapper.UserMapper;
|
||||
import SuMoC.backend.user.models.Friendship;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import SuMoC.backend.user.repository.UserRepository;
|
||||
@@ -21,10 +27,20 @@ public class UserService {
|
||||
private final UserRepository userRepository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
private final UserMapper userMapper;
|
||||
private final FriendshipMapper friendshipMapper;
|
||||
private final TripMapper tripMapper;
|
||||
|
||||
private final TokenService tokenService;;
|
||||
|
||||
@Autowired
|
||||
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
|
||||
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, UserMapper userMapper, FriendshipMapper friendshipMapper, TripMapper tripMapper, TokenService tokenService) {
|
||||
this.userRepository = userRepository;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
this.userMapper = userMapper;
|
||||
this.friendshipMapper = friendshipMapper;
|
||||
this.tripMapper = tripMapper;
|
||||
this.tokenService = tokenService;
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +105,7 @@ public class UserService {
|
||||
|
||||
|
||||
|
||||
public void changePassword(Long id, String newPassword) {
|
||||
public LoginResponseDTO setPassword(Long id, String newPassword) {
|
||||
User sender = getSender();
|
||||
if (!sender.getId().equals(id) && !sender.getRoles().contains(RoleName.ADMIN)) {
|
||||
throw new RuntimeException("Ein Nutzer kann nur sein eigenes Passwort ändern.");
|
||||
@@ -103,12 +119,21 @@ public class UserService {
|
||||
throw new RuntimeException("Das Passwort muss mindestens 8 Zeichen lang sein.");
|
||||
}
|
||||
// TODO: Add Email Notification for password change
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
sender.setPassword(passwordEncoder.encode(newPassword));
|
||||
userRepository.save(sender);
|
||||
|
||||
User updatedSender = userRepository.findById(id).get();
|
||||
|
||||
return new LoginResponseDTO(userMapper.toUserDTO(updatedSender),
|
||||
tokenService.generateUserJwt(authentication, userMapper.toUserDTO(updatedSender)));
|
||||
|
||||
}
|
||||
|
||||
public void changeEmail(Long id, String newEmail) {
|
||||
public LoginResponseDTO setEmail(Long id, String newEmail) {
|
||||
User sender = getSender();
|
||||
System.out.println(sender.getId());
|
||||
|
||||
if (!sender.getId().equals(id)) {
|
||||
throw new RuntimeException("Ein Nutzer kann nur seine eigene E-Mail-Adresse ändern.");
|
||||
}
|
||||
@@ -124,17 +149,26 @@ public class UserService {
|
||||
|
||||
|
||||
// TODO: Add Email Notification for password change
|
||||
|
||||
System.out.println("################ Changing Email ################");
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
sender.setEmail(newEmail);
|
||||
userRepository.save(sender);
|
||||
|
||||
User updatedUser = userRepository.findById(sender.getId()).orElseThrow();
|
||||
System.out.println("################# Returning LoginResponse ################");
|
||||
return new LoginResponseDTO(userMapper.toUserDTO(updatedUser),
|
||||
tokenService.generateUserJwt(authentication, userMapper.toUserDTO(updatedUser), newEmail));
|
||||
}
|
||||
|
||||
public void changeVisibilityPrivacy(VisibilityPrivacy visibilityPrivacy) {
|
||||
User user = getSender();
|
||||
user.setVisibilityPrivacy(visibilityPrivacy);
|
||||
public void setVisibilityPrivacy(VisibilityPrivacy visibilityPrivacy) {
|
||||
User sender = getSender();
|
||||
|
||||
sender.setVisibilityPrivacy(visibilityPrivacy);
|
||||
userRepository.save(sender);
|
||||
|
||||
}
|
||||
|
||||
public void changePrivateName(Long id, String newPrivateName) {
|
||||
public void setPrivateName(Long id, String newPrivateName) {
|
||||
User sender = getSender();
|
||||
if (!sender.getId().equals(id) && !sender.getRoles().contains(RoleName.ADMIN)) {
|
||||
throw new RuntimeException("Ein Nutzer kann nur seinen eigenen privaten Namen ändern.");
|
||||
@@ -145,14 +179,27 @@ public class UserService {
|
||||
}
|
||||
|
||||
|
||||
public MaximalUserDTO getSelf() {
|
||||
User sender = getSender();
|
||||
return userMapper.toMaximalUserDTO(sender);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public User getSender() {
|
||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
if (auth == null || !auth.isAuthenticated()) {
|
||||
throw new RuntimeException("Nicht authentifiziert");
|
||||
}
|
||||
return userRepository.findByEmail(auth.getName()).get();
|
||||
|
||||
return userRepository.findByEmail(auth.getName())
|
||||
.orElseThrow(() -> new RuntimeException(
|
||||
"Authentifizierter Nutzer existiert nicht mehr: " + auth.getName()
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user