-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserController.java
More file actions
59 lines (52 loc) · 2.72 KB
/
Copy pathUserController.java
File metadata and controls
59 lines (52 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* REST Controller für Benutzer
* @author FA
* Code von anderen Teammitgliedern oder Quellen wird durch einzelne Kommentare deklariert
* @version 1.1 - sendpasswortresetlink entfernt - PD
*/
package ch.fhnw.timerecordingbackend.controller;
import ch.fhnw.timerecordingbackend.dto.authentication.ChangePasswordRequest;
import ch.fhnw.timerecordingbackend.dto.authentication.ResetPasswordRequest; // NEUER IMPORT
import ch.fhnw.timerecordingbackend.model.SystemLog; // NEUER IMPORT
import ch.fhnw.timerecordingbackend.model.User; // NEUER IMPORT
import ch.fhnw.timerecordingbackend.repository.SystemLogRepository; // NEUER IMPORT
import ch.fhnw.timerecordingbackend.service.UserService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime; // NEUER IMPORT
import java.util.Map;
import java.util.Optional; // NEUER IMPORT
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@Autowired // Injiziere das SystemLogRepository
private SystemLogRepository systemLogRepository;
@PutMapping( "/change-password")
public ResponseEntity<?> changePassword(@Valid @RequestBody ChangePasswordRequest request) {
userService.changePassword(request);
return ResponseEntity.ok().body(
java.util.Map.of("message", "Passwort geändert")
);
}
@PostMapping("/request-password-reset")
public ResponseEntity<?> requestPasswordReset(@Valid @RequestBody ResetPasswordRequest request) {
Optional<User> userOptional = userService.findByEmail(request.getEmail());
if (userOptional.isEmpty()) {
return ResponseEntity.ok().body(Map.of("message", "Wenn ein Konto mit dieser E-Mail-Adresse existiert, wurde Ihre Anfrage verarbeitet."));
}
User user = userOptional.get();
SystemLog log = new SystemLog();
log.setAction("Passwort Reset angefordert"); // <-- GENAU DIESER STRING
log.setTimestamp(LocalDateTime.now());
log.setUserEmail(user.getEmail());
log.setUserId(user.getId());
log.setDetails("Passwort-Reset-Anfrage von Login-Seite für Benutzer: " + user.getEmail());
log.setProcessedStatus("PENDING"); // <-- GENAU DIESER STRING
systemLogRepository.save(log);
return ResponseEntity.ok().body(Map.of("message", "Ihre Anfrage zum Zurücksetzen des Passworts wurde an den Administrator weitergeleitet."));
}
}