Volle Mindflux-App ins Deploy-Repo übernehmen
Ersetzt das Hello-World-Gerüst durch die vollständige Anwendung (com.example.mindflux: 64 Klassen, Thymeleaf-Templates, Static-Assets, Flyway V1–V6). Deploy-Infrastruktur (.gitea, Dockerfile) bleibt erhalten. - pom: volle Abhängigkeiten (Security, Thymeleaf, JPA, Flyway), Spring Boot 3.4.4 / Java 17 - docker-compose: SPRING_DATASOURCE_* unverändert; App lauscht auf 8080 (nginx -> mindflux:8080); Uploads-Volume; Admin-Passwörter via Gitea-Secrets - Workflow: Deploy-Step injiziert MINDFLUX_ADMIN*_PASSWORD aus Secrets - .dockerignore ergänzt; Hello-World (com.mindflux) und Test-Datei 'main' entfernt Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
package com.example.mindflux.controller;
|
||||
|
||||
import com.example.mindflux.model.User;
|
||||
import com.example.mindflux.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.Principal;
|
||||
import java.util.UUID;
|
||||
|
||||
@Controller
|
||||
public class ProfileController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private BCryptPasswordEncoder passwordEncoder;
|
||||
|
||||
private static final String UPLOAD_DIR = System.getProperty("user.dir") + "/uploads/profiles/";
|
||||
|
||||
@GetMapping("/profile")
|
||||
public String profile(Model model, Principal principal, @RequestParam(value = "success", required = false) Boolean success) {
|
||||
String username = principal.getName();
|
||||
User user = userService.findByUsername(username);
|
||||
|
||||
model.addAttribute("user", user);
|
||||
|
||||
if (Boolean.TRUE.equals(success)) {
|
||||
model.addAttribute("message", "Profil erfolgreich aktualisiert!");
|
||||
}
|
||||
|
||||
return "profile";
|
||||
}
|
||||
|
||||
@PostMapping("/profile/delete-picture")
|
||||
public String deleteProfilePicture(Principal principal) {
|
||||
String username = principal.getName();
|
||||
User existingUser = userService.findByUsername(username);
|
||||
|
||||
if (existingUser != null) {
|
||||
String profilePicture = existingUser.getProfilePicture();
|
||||
if (profilePicture != null && !profilePicture.trim().isEmpty()) {
|
||||
try {
|
||||
Path picturePath = Paths.get(System.getProperty("user.dir"), "uploads", "profiles", profilePicture);
|
||||
System.out.println("Versuche zu löschen: " + picturePath.toAbsolutePath());
|
||||
|
||||
if (Files.exists(picturePath)) {
|
||||
Files.delete(picturePath);
|
||||
System.out.println("Datei erfolgreich gelöscht: " + picturePath.getFileName());
|
||||
} else {
|
||||
System.out.println("Datei existiert nicht: " + picturePath.toAbsolutePath());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("Fehler beim Löschen der Datei: " + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
System.out.println("Kein Profilbild gesetzt, nichts zu löschen.");
|
||||
}
|
||||
|
||||
existingUser.setProfilePicture(null); // Wichtig: auch wenn Datei nicht existiert
|
||||
userService.save(existingUser);
|
||||
}
|
||||
|
||||
return "redirect:/profile?success=true";
|
||||
}
|
||||
|
||||
@GetMapping("/profile/knowledge")
|
||||
public String knowledgeCenter(Model model, Principal principal) {
|
||||
String username = principal.getName();
|
||||
User user = userService.findByUsername(username);
|
||||
|
||||
model.addAttribute("user", user);
|
||||
return "knowledge";
|
||||
}
|
||||
|
||||
@PostMapping("/profile/update")
|
||||
public String updateProfile(@RequestParam("email") String email,
|
||||
@RequestParam(value = "profilePicture", required = false) MultipartFile file,
|
||||
Principal principal) {
|
||||
String username = principal.getName();
|
||||
User existingUser = userService.findByUsername(username);
|
||||
|
||||
if (existingUser != null) {
|
||||
existingUser.setEmail(email); // Email aktualisieren
|
||||
|
||||
if (file != null && !file.isEmpty()) {
|
||||
try {
|
||||
// Altes Profilbild löschen (falls vorhanden)
|
||||
if (existingUser.getProfilePicture() != null && !existingUser.getProfilePicture().isEmpty()) {
|
||||
Path oldFilePath = Paths.get(System.getProperty("user.dir"), "uploads", "profiles", existingUser.getProfilePicture());
|
||||
if (Files.exists(oldFilePath)) {
|
||||
Files.delete(oldFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
// Neues Profilbild speichern
|
||||
String fileExtension = getFileExtension(file.getOriginalFilename());
|
||||
String fileName = UUID.randomUUID() + fileExtension;
|
||||
|
||||
Path uploadPath = Paths.get(UPLOAD_DIR);
|
||||
Files.createDirectories(uploadPath);
|
||||
|
||||
Path filePath = uploadPath.resolve(fileName);
|
||||
file.transferTo(filePath.toFile());
|
||||
|
||||
existingUser.setProfilePicture(fileName);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
userService.save(existingUser);
|
||||
}
|
||||
|
||||
return "redirect:/profile?success=true";
|
||||
}
|
||||
|
||||
@PostMapping("/profile/change-password")
|
||||
public String changePassword(@RequestParam("oldPassword") String oldPassword,
|
||||
@RequestParam("newPassword") String newPassword,
|
||||
Principal principal,
|
||||
Model model) {
|
||||
String username = principal.getName();
|
||||
User user = userService.findByUsername(username);
|
||||
|
||||
if (user == null) {
|
||||
model.addAttribute("error", "Benutzer nicht gefunden.");
|
||||
return "profile";
|
||||
}
|
||||
|
||||
if (!passwordEncoder.matches(oldPassword, user.getPassword())) {
|
||||
model.addAttribute("error", "Das aktuelle Passwort ist falsch.");
|
||||
return "profile";
|
||||
}
|
||||
|
||||
user.setPassword(passwordEncoder.encode(newPassword));
|
||||
userService.save(user);
|
||||
|
||||
model.addAttribute("message", "Passwort erfolgreich geändert.");
|
||||
return "profile";
|
||||
}
|
||||
|
||||
@PostMapping("/profile/delete")
|
||||
public String deleteProfile(Principal principal, Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
|
||||
String username = principal.getName();
|
||||
userService.deleteByUsername(username);
|
||||
new SecurityContextLogoutHandler().logout(request, response, authentication);
|
||||
return "redirect:/?logout";
|
||||
}
|
||||
|
||||
// Hilfsmethode: Dateiendung extrahieren
|
||||
private String getFileExtension(String filename) {
|
||||
if (filename == null || filename.lastIndexOf(".") == -1) {
|
||||
return "";
|
||||
}
|
||||
return filename.substring(filename.lastIndexOf("."));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user