dipslay stuf

This commit is contained in:
Anthony Stirling 2023-09-04 00:12:27 +01:00
parent 0bb2df135b
commit fd08513212
7 changed files with 85 additions and 87 deletions

View file

@ -13,7 +13,7 @@ import jakarta.servlet.http.HttpServletResponse;
public class CleanUrlInterceptor implements HandlerInterceptor { public class CleanUrlInterceptor implements HandlerInterceptor {
private static final List<String> ALLOWED_PARAMS = Arrays.asList("lang", "endpoint", "endpoints", "logout", "error", "file"); private static final List<String> ALLOWED_PARAMS = Arrays.asList("lang", "endpoint", "endpoints", "logout", "error", "file", "messageType");
@Override @Override
@ -32,7 +32,6 @@ public class CleanUrlInterceptor implements HandlerInterceptor {
if (keyValue.length != 2) { if (keyValue.length != 2) {
continue; continue;
} }
if (ALLOWED_PARAMS.contains(keyValue[0])) { if (ALLOWED_PARAMS.contains(keyValue[0])) {
parameters.put(keyValue[0], keyValue[1]); parameters.put(keyValue[0], keyValue[1]);
} }

View file

@ -39,5 +39,4 @@ public class ConfigInitializer implements ApplicationContextInitializer<Configur
} }
} }
} }
} }

View file

@ -50,28 +50,26 @@ public class UserController {
HttpServletResponse response, HttpServletResponse response,
RedirectAttributes redirectAttributes) { RedirectAttributes redirectAttributes) {
if (principal == null) { if (principal == null) {
redirectAttributes.addFlashAttribute("notAuthenticated", true); return new RedirectView("/change-creds?messageType=notAuthenticated");
return new RedirectView("/change-creds");
} }
Optional<User> userOpt = userService.findByUsername(principal.getName()); Optional<User> userOpt = userService.findByUsername(principal.getName());
if (userOpt == null || userOpt.isEmpty()) { if (userOpt == null || userOpt.isEmpty()) {
redirectAttributes.addFlashAttribute("userNotFound", true); return new RedirectView("/change-creds?messageType=userNotFound");
return new RedirectView("/change-creds");
} }
User user = userOpt.get(); User user = userOpt.get();
if (!userService.isPasswordCorrect(user, currentPassword)) { if (!userService.isPasswordCorrect(user, currentPassword)) {
redirectAttributes.addFlashAttribute("incorrectPassword", true); return new RedirectView("/change-creds?messageType=incorrectPassword");
return new RedirectView("/change-creds");
} }
if (!user.getUsername().equals(newUsername) && userService.usernameExists(newUsername)) { if (!user.getUsername().equals(newUsername) && userService.usernameExists(newUsername)) {
redirectAttributes.addFlashAttribute("usernameExists", true); return new RedirectView("/change-creds?messageType=usernameExists");
return new RedirectView("/change-creds");
} }
userService.changePassword(user, newPassword); userService.changePassword(user, newPassword);
if(!user.getUsername().equals(newUsername)) { if(!user.getUsername().equals(newUsername)) {
userService.changeUsername(user, newUsername); userService.changeUsername(user, newUsername);
@ -81,8 +79,7 @@ public class UserController {
// Logout using Spring's utility // Logout using Spring's utility
new SecurityContextLogoutHandler().logout(request, response, null); new SecurityContextLogoutHandler().logout(request, response, null);
redirectAttributes.addFlashAttribute("credsUpdated", true); return new RedirectView("/login?messageType=credsUpdated");
return new RedirectView("/login");
} }
@ -95,35 +92,32 @@ public class UserController {
HttpServletResponse response, HttpServletResponse response,
RedirectAttributes redirectAttributes) { RedirectAttributes redirectAttributes) {
if (principal == null) { if (principal == null) {
redirectAttributes.addFlashAttribute("notAuthenticated", true); return new RedirectView("/account?messageType=notAuthenticated");
return new RedirectView("/account");
} }
Optional<User> userOpt = userService.findByUsername(principal.getName()); Optional<User> userOpt = userService.findByUsername(principal.getName());
if (userOpt == null || userOpt.isEmpty()) { if (userOpt == null || userOpt.isEmpty()) {
redirectAttributes.addFlashAttribute("userNotFound", true); return new RedirectView("/account?messageType=userNotFound");
return new RedirectView("/account");
} }
User user = userOpt.get(); User user = userOpt.get();
if (!userService.isPasswordCorrect(user, currentPassword)) { if (!userService.isPasswordCorrect(user, currentPassword)) {
redirectAttributes.addFlashAttribute("incorrectPassword", true); return new RedirectView("/account?messageType=incorrectPassword");
return new RedirectView("/account");
} }
if (userService.usernameExists(newUsername)) { if (!user.getUsername().equals(newUsername) && userService.usernameExists(newUsername)) {
redirectAttributes.addFlashAttribute("usernameExists", true); return new RedirectView("/account?messageType=usernameExists");
return new RedirectView("/account");
} }
userService.changeUsername(user, newUsername); userService.changeUsername(user, newUsername);
// Logout using Spring's utility // Logout using Spring's utility
new SecurityContextLogoutHandler().logout(request, response, null); new SecurityContextLogoutHandler().logout(request, response, null);
redirectAttributes.addFlashAttribute("message", "Username updated successfully."); return new RedirectView("/login?messageType=credsUpdated");
return new RedirectView("/login");
} }
@PostMapping("/change-password") @PostMapping("/change-password")
@ -134,21 +128,19 @@ public class UserController {
HttpServletResponse response, HttpServletResponse response,
RedirectAttributes redirectAttributes) { RedirectAttributes redirectAttributes) {
if (principal == null) { if (principal == null) {
redirectAttributes.addFlashAttribute("notAuthenticated", true); return new RedirectView("/account?messageType=notAuthenticated");
return new RedirectView("/account");
} }
Optional<User> userOpt = userService.findByUsername(principal.getName()); Optional<User> userOpt = userService.findByUsername(principal.getName());
if (userOpt == null || userOpt.isEmpty()) { if (userOpt == null || userOpt.isEmpty()) {
redirectAttributes.addFlashAttribute("userNotFound", true); return new RedirectView("/account?messageType=userNotFound");
return new RedirectView("/account");
} }
User user = userOpt.get(); User user = userOpt.get();
if (!userService.isPasswordCorrect(user, currentPassword)) { if (!userService.isPasswordCorrect(user, currentPassword)) {
redirectAttributes.addFlashAttribute("incorrectPassword", true); return new RedirectView("/account?messageType=incorrectPassword");
return new RedirectView("/account");
} }
userService.changePassword(user, newPassword); userService.changePassword(user, newPassword);
@ -156,8 +148,7 @@ public class UserController {
// Logout using Spring's utility // Logout using Spring's utility
new SecurityContextLogoutHandler().logout(request, response, null); new SecurityContextLogoutHandler().logout(request, response, null);
redirectAttributes.addFlashAttribute("message", "Password updated successfully."); return new RedirectView("/login?messageType=credsUpdated");
return new RedirectView("/login");
} }

View file

@ -42,8 +42,8 @@ red=Red
green=Green green=Green
blue=Blue blue=Blue
custom=Custom... custom=Custom...
changedCredsMessage=Credentials changed!
changedCredsMessage=Credentials changed!
notAuthenticatedMessage=User not authenticated. notAuthenticatedMessage=User not authenticated.
userNotFoundMessage=User not found. userNotFoundMessage=User not found.
incorrectPasswordMessage=Current password is incorrect. 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.signOut=Sign Out
settings.accountSettings=Account Settings 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.title=Account Settings
account.accountSettings=Account Settings account.accountSettings=Account Settings
account.adminSettings=Admin Settings - View and Add Users account.adminSettings=Admin Settings - View and Add Users

View file

@ -16,22 +16,24 @@
<!-- User Settings Title --> <!-- User Settings Title -->
<h2 class="text-center" th:text="#{account.accountSettings}">User Settings</h2> <h2 class="text-center" th:text="#{account.accountSettings}">User Settings</h2>
<hr> <hr>
<div th:if="${notAuthenticated}" class="alert alert-danger" role="alert"> <div th:if="${param.messageType != null and param.messageType.size() > 0 and param.messageType[0] == 'notAuthenticated'}" class="alert alert-danger">
User not authenticated. <span th:text="#{notAuthenticatedMessage}">Default message if not found</span>
</div> </div>
<div th:if="${userNotFound}" class="alert alert-danger" role="alert"> <div th:if="${param.messageType != null and param.messageType.size() > 0 and param.messageType[0] == 'userNotFound'}" class="alert alert-danger">
User not found. <span th:text="#{userNotFoundMessage}">Default message if not found</span>
</div> </div>
<div th:if="${incorrectPassword}" class="alert alert-danger" role="alert"> <div th:if="${param.messageType != null and param.messageType.size() > 0 and param.messageType[0] == 'incorrectPassword'}" class="alert alert-danger">
Current password is incorrect. <span th:text="#{incorrectPasswordMessage}">Default message if not found</span>
</div> </div>
<div th:if="${usernameExists}" class="alert alert-danger" role="alert"> <div th:if="${param.messageType != null and param.messageType.size() > 0 and param.messageType[0] == 'usernameExists'}" class="alert alert-danger">
New username already exists. <span th:text="#{usernameExistsMessage}">Default message if not found</span>
</div> </div>
<!-- At the top of the user settings --> <!-- At the top of the user settings -->
<h3 class="text-center"><span th:text="#{welcome} + ' ' + ${username}">User</span>!</h3> <h3 class="text-center"><span th:text="#{welcome} + ' ' + ${username}">User</span>!</h3>

View file

@ -16,19 +16,19 @@
<!-- User Settings Title --> <!-- User Settings Title -->
<h2 class="text-center" th:text="#{changeCreds.header}">User Settings</h2> <h2 class="text-center" th:text="#{changeCreds.header}">User Settings</h2>
<hr> <hr>
<div th:if="${notAuthenticated}" class="alert alert-danger" role="alert">
User not authenticated. <div th:if="${param.messageType != null and param.messageType.size() > 0 and param.messageType[0] == 'notAuthenticated'}" class="alert alert-danger">
<span th:text="#{notAuthenticatedMessage}">Default message if not found</span>
</div> </div>
<div th:if="${userNotFound}" class="alert alert-danger" role="alert"> <div th:if="${param.messageType != null and param.messageType.size() > 0 and param.messageType[0] == 'userNotFound'}" class="alert alert-danger">
User not found. <span th:text="#{userNotFoundMessage}">Default message if not found</span>
</div> </div>
<div th:if="${incorrectPassword}" class="alert alert-danger" role="alert"> <div th:if="${param.messageType != null and param.messageType.size() > 0 and param.messageType[0] == 'incorrectPassword'}" class="alert alert-danger">
Current password is incorrect. <span th:text="#{incorrectPasswordMessage}">Default message if not found</span>
</div> </div>
<div th:if="${usernameExists}" class="alert alert-danger" role="alert"> <div th:if="${param.messageType != null and param.messageType.size() > 0 and param.messageType[0] == 'usernameExists'}" class="alert alert-danger">
New username already exists. <span th:text="#{usernameExistsMessage}">Default message if not found</span>
</div> </div>
<div th:if="${changeCredsFlag}" class="alert alert-success" th:text="#{changeCredsMessage}"></div>
<!-- At the top of the user settings --> <!-- At the top of the user settings -->
<h3 class="text-center"><span th:text="#{welcome} + ' ' + ${username}">User</span>!</h3> <h3 class="text-center"><span th:text="#{welcome} + ' ' + ${username}">User</span>!</h3>

View file

@ -179,17 +179,10 @@ document.addEventListener('DOMContentLoaded', function() {
const urlParams = currentURL.searchParams; const urlParams = currentURL.searchParams;
const currentLangParam = urlParams.get('lang') || defaultLocale; const currentLangParam = urlParams.get('lang') || defaultLocale;
console.log("defaultLocale", defaultLocale);
console.log("storedLocale", storedLocale);
console.log("currentLangParam", currentLangParam);
if (defaultLocale !== storedLocale && currentLangParam !== storedLocale) { if (defaultLocale !== storedLocale && currentLangParam !== storedLocale) {
console.log("currentLangParam", currentLangParam)
console.log("storedLocale", storedLocale)
urlParams.set('lang', storedLocale); urlParams.set('lang', storedLocale);
currentURL.search = urlParams.toString(); currentURL.search = urlParams.toString();
console.log("redirecting to", currentURL.toString());
window.location.href = currentURL.toString(); window.location.href = currentURL.toString();
return; return;
} }
@ -263,8 +256,9 @@ function handleDropdownItemClick(event) {
<div th:if="${logoutMessage}" class="alert alert-success" <div th:if="${logoutMessage}" class="alert alert-success"
th:text="${logoutMessage}"></div> th:text="${logoutMessage}"></div>
<div th:if="${param.messageType != null and param.messageType.size() > 0 and param.messageType[0] == 'credsUpdated'}" class="alert alert-success">
<span th:text="#{changedCredsMessage}">Default message if not found</span>
</div>
<form th:action="@{login}" method="post"> <form th:action="@{login}" method="post">
<img class="mb-4" src="favicon.svg" alt="" width="144" height="144"> <img class="mb-4" src="favicon.svg" alt="" width="144" height="144">
<h1 class="h1 mb-3 fw-normal" th:text="${@appName}">Stirling-PDF</h1> <h1 class="h1 mb-3 fw-normal" th:text="${@appName}">Stirling-PDF</h1>