diff --git a/src/main/java/stirling/software/SPDF/config/CleanUrlInterceptor.java b/src/main/java/stirling/software/SPDF/config/CleanUrlInterceptor.java index dec37567..4df14253 100644 --- a/src/main/java/stirling/software/SPDF/config/CleanUrlInterceptor.java +++ b/src/main/java/stirling/software/SPDF/config/CleanUrlInterceptor.java @@ -13,7 +13,7 @@ import jakarta.servlet.http.HttpServletResponse; public class CleanUrlInterceptor implements HandlerInterceptor { - private static final List ALLOWED_PARAMS = Arrays.asList("lang", "endpoint", "endpoints", "logout", "error", "file"); + private static final List ALLOWED_PARAMS = Arrays.asList("lang", "endpoint", "endpoints", "logout", "error", "file", "messageType"); @Override @@ -32,7 +32,6 @@ public class CleanUrlInterceptor implements HandlerInterceptor { if (keyValue.length != 2) { continue; } - if (ALLOWED_PARAMS.contains(keyValue[0])) { parameters.put(keyValue[0], keyValue[1]); } diff --git a/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java b/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java index 49f0c2f7..43d0ccfe 100644 --- a/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java +++ b/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java @@ -39,5 +39,4 @@ public class ConfigInitializer implements ApplicationContextInitializer userOpt = userService.findByUsername(principal.getName()); + Optional userOpt = userService.findByUsername(principal.getName()); - if (userOpt == null || userOpt.isEmpty()) { - redirectAttributes.addFlashAttribute("userNotFound", true); - return new RedirectView("/change-creds"); - } - User user = userOpt.get(); + if (userOpt == null || userOpt.isEmpty()) { + return new RedirectView("/change-creds?messageType=userNotFound"); + } - if (!userService.isPasswordCorrect(user, currentPassword)) { - redirectAttributes.addFlashAttribute("incorrectPassword", true); - return new RedirectView("/change-creds"); - } + User user = userOpt.get(); + + if (!userService.isPasswordCorrect(user, currentPassword)) { + return new RedirectView("/change-creds?messageType=incorrectPassword"); + } + + if (!user.getUsername().equals(newUsername) && userService.usernameExists(newUsername)) { + return new RedirectView("/change-creds?messageType=usernameExists"); + } - if (!user.getUsername().equals(newUsername) && userService.usernameExists(newUsername)) { - redirectAttributes.addFlashAttribute("usernameExists", true); - return new RedirectView("/change-creds"); - } userService.changePassword(user, newPassword); if(!user.getUsername().equals(newUsername)) { @@ -81,8 +79,7 @@ public class UserController { // Logout using Spring's utility new SecurityContextLogoutHandler().logout(request, response, null); - redirectAttributes.addFlashAttribute("credsUpdated", true); - return new RedirectView("/login"); + return new RedirectView("/login?messageType=credsUpdated"); } @@ -94,36 +91,33 @@ public class UserController { HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) { - if (principal == null) { - redirectAttributes.addFlashAttribute("notAuthenticated", true); - return new RedirectView("/account"); - } + if (principal == null) { + return new RedirectView("/account?messageType=notAuthenticated"); + } - Optional userOpt = userService.findByUsername(principal.getName()); + Optional userOpt = userService.findByUsername(principal.getName()); - if (userOpt == null || userOpt.isEmpty()) { - redirectAttributes.addFlashAttribute("userNotFound", true); - return new RedirectView("/account"); - } - User user = userOpt.get(); + if (userOpt == null || userOpt.isEmpty()) { + return new RedirectView("/account?messageType=userNotFound"); + } - if (!userService.isPasswordCorrect(user, currentPassword)) { - redirectAttributes.addFlashAttribute("incorrectPassword", true); - return new RedirectView("/account"); - } + User user = userOpt.get(); + + if (!userService.isPasswordCorrect(user, currentPassword)) { + return new RedirectView("/account?messageType=incorrectPassword"); + } + + if (!user.getUsername().equals(newUsername) && userService.usernameExists(newUsername)) { + return new RedirectView("/account?messageType=usernameExists"); + } - if (userService.usernameExists(newUsername)) { - redirectAttributes.addFlashAttribute("usernameExists", true); - return new RedirectView("/account"); - } userService.changeUsername(user, newUsername); // Logout using Spring's utility new SecurityContextLogoutHandler().logout(request, response, null); - redirectAttributes.addFlashAttribute("message", "Username updated successfully."); - return new RedirectView("/login"); + return new RedirectView("/login?messageType=credsUpdated"); } @PostMapping("/change-password") @@ -133,31 +127,28 @@ public class UserController { HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) { - if (principal == null) { - redirectAttributes.addFlashAttribute("notAuthenticated", true); - return new RedirectView("/account"); - } + if (principal == null) { + return new RedirectView("/account?messageType=notAuthenticated"); + } - Optional userOpt = userService.findByUsername(principal.getName()); + Optional userOpt = userService.findByUsername(principal.getName()); - if (userOpt == null || userOpt.isEmpty()) { - redirectAttributes.addFlashAttribute("userNotFound", true); - return new RedirectView("/account"); - } - User user = userOpt.get(); + if (userOpt == null || userOpt.isEmpty()) { + return new RedirectView("/account?messageType=userNotFound"); + } - if (!userService.isPasswordCorrect(user, currentPassword)) { - redirectAttributes.addFlashAttribute("incorrectPassword", true); - return new RedirectView("/account"); - } + User user = userOpt.get(); + + if (!userService.isPasswordCorrect(user, currentPassword)) { + return new RedirectView("/account?messageType=incorrectPassword"); + } userService.changePassword(user, newPassword); // Logout using Spring's utility new SecurityContextLogoutHandler().logout(request, response, null); - redirectAttributes.addFlashAttribute("message", "Password updated successfully."); - return new RedirectView("/login"); + return new RedirectView("/login?messageType=credsUpdated"); } diff --git a/src/main/resources/messages_en_GB.properties b/src/main/resources/messages_en_GB.properties index 88284dec..06681b6f 100644 --- a/src/main/resources/messages_en_GB.properties +++ b/src/main/resources/messages_en_GB.properties @@ -42,8 +42,8 @@ red=Red green=Green blue=Blue custom=Custom... -changedCredsMessage=Credentials changed! +changedCredsMessage=Credentials changed! notAuthenticatedMessage=User not authenticated. userNotFoundMessage=User not found. incorrectPasswordMessage=Current password is incorrect. @@ -75,6 +75,19 @@ settings.zipThreshold=Zip files when the number of downloaded files exceeds settings.signOut=Sign Out settings.accountSettings=Account Settings + + +changeCreds.title=Change Credentials +changeCreds.header=Update Your Account Details +changeCreds.changeUserAndPassword=You are using default login credentials. Please enter a new password (and username if wanted) +changeCreds.newUsername=New Username +changeCreds.oldPassword=Current Password +changeCreds.newPassword=New Password +changeCreds.confirmNewPassword=Confirm New Password +changeCreds.submit=Submit Changes + + + account.title=Account Settings account.accountSettings=Account Settings account.adminSettings=Admin Settings - View and Add Users diff --git a/src/main/resources/templates/account.html b/src/main/resources/templates/account.html index ac855d1b..4a7a3174 100644 --- a/src/main/resources/templates/account.html +++ b/src/main/resources/templates/account.html @@ -16,19 +16,21 @@

User Settings


-