1
0
mirror of https://github.com/Stirling-Tools/Stirling-PDF.git synced 2024-09-30 00:20:13 +02:00

testing messages

This commit is contained in:
Anthony Stirling 2023-09-03 19:44:16 +01:00
parent adadf7428c
commit 0bb2df135b
6 changed files with 74 additions and 36 deletions

View File

@ -44,7 +44,7 @@ public class UserAuthenticationFilter extends OncePerRequestFilter {
filterChain.doFilter(request, response); filterChain.doFilter(request, response);
return; return;
} }
String requestURI = request.getRequestURI();
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// Check for API key in the request headers if no authentication exists // Check for API key in the request headers if no authentication exists
@ -74,13 +74,14 @@ public class UserAuthenticationFilter extends OncePerRequestFilter {
// If we still don't have any authentication, deny the request // If we still don't have any authentication, deny the request
if (authentication == null || !authentication.isAuthenticated()) { if (authentication == null || !authentication.isAuthenticated()) {
String method = request.getMethod(); String method = request.getMethod();
if ("GET".equalsIgnoreCase(method)) { if ("GET".equalsIgnoreCase(method) && !"/login".equals(requestURI)) {
response.sendRedirect("/login"); // redirect to the login page response.sendRedirect("/login"); // redirect to the login page
return; return;
} else {
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.getWriter().write("Authentication required. Please provide a X-API-KEY in request header.\nThis is found in Settings -> Account Settings -> API Key\nAlternativly you can disable authentication if this is unexpected");
return;
} }
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.getWriter().write("Authentication required. Please provide a X-API-KEY in request header.\nThis is found in Settings -> Account Settings -> API Key\nAlternativly you can disable authentication if this is unexpected");
return;
} }
filterChain.doFilter(request, response); filterChain.doFilter(request, response);

View File

@ -50,26 +50,26 @@ public class UserController {
HttpServletResponse response, HttpServletResponse response,
RedirectAttributes redirectAttributes) { RedirectAttributes redirectAttributes) {
if (principal == null) { if (principal == null) {
redirectAttributes.addFlashAttribute("error", "User not authenticated."); redirectAttributes.addFlashAttribute("notAuthenticated", true);
return new RedirectView("/error"); 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("error", "User not found."); redirectAttributes.addFlashAttribute("userNotFound", true);
return new RedirectView("/error"); 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("error", "Current password is incorrect."); redirectAttributes.addFlashAttribute("incorrectPassword", true);
return new RedirectView("/error"); return new RedirectView("/change-creds");
} }
if (!user.getUsername().equals(newUsername) && userService.usernameExists(newUsername)) { if (!user.getUsername().equals(newUsername) && userService.usernameExists(newUsername)) {
redirectAttributes.addFlashAttribute("error", "New username already exists."); redirectAttributes.addFlashAttribute("usernameExists", true);
return new RedirectView("/error"); return new RedirectView("/change-creds");
} }
userService.changePassword(user, newPassword); userService.changePassword(user, newPassword);
@ -95,25 +95,25 @@ public class UserController {
HttpServletResponse response, HttpServletResponse response,
RedirectAttributes redirectAttributes) { RedirectAttributes redirectAttributes) {
if (principal == null) { if (principal == null) {
redirectAttributes.addFlashAttribute("error", "User not authenticated."); redirectAttributes.addFlashAttribute("notAuthenticated", true);
return new RedirectView("/account"); 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("error", "User not found."); redirectAttributes.addFlashAttribute("userNotFound", true);
return new RedirectView("/account"); return new RedirectView("/account");
} }
User user = userOpt.get(); User user = userOpt.get();
if (!userService.isPasswordCorrect(user, currentPassword)) { if (!userService.isPasswordCorrect(user, currentPassword)) {
redirectAttributes.addFlashAttribute("error", "Current password is incorrect."); redirectAttributes.addFlashAttribute("incorrectPassword", true);
return new RedirectView("/account"); return new RedirectView("/account");
} }
if (userService.usernameExists(newUsername)) { if (userService.usernameExists(newUsername)) {
redirectAttributes.addFlashAttribute("error", "New username already exists."); redirectAttributes.addFlashAttribute("usernameExists", true);
return new RedirectView("/account"); return new RedirectView("/account");
} }
@ -134,20 +134,20 @@ public class UserController {
HttpServletResponse response, HttpServletResponse response,
RedirectAttributes redirectAttributes) { RedirectAttributes redirectAttributes) {
if (principal == null) { if (principal == null) {
redirectAttributes.addFlashAttribute("error", "User not authenticated."); redirectAttributes.addFlashAttribute("notAuthenticated", true);
return new RedirectView("/account"); 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("error", "User not found."); redirectAttributes.addFlashAttribute("userNotFound", true);
return new RedirectView("/account"); return new RedirectView("/account");
} }
User user = userOpt.get(); User user = userOpt.get();
if (!userService.isPasswordCorrect(user, currentPassword)) { if (!userService.isPasswordCorrect(user, currentPassword)) {
redirectAttributes.addFlashAttribute("error", "Current password is incorrect."); redirectAttributes.addFlashAttribute("incorrectPassword", true);
return new RedirectView("/account"); return new RedirectView("/account");
} }

View File

@ -42,8 +42,12 @@ red=Red
green=Green green=Green
blue=Blue blue=Blue
custom=Custom... custom=Custom...
changeCredsMessage=First time login, Please change your username and/or password! changedCredsMessage=Credentials changed!
notAuthenticatedMessage=User not authenticated.
userNotFoundMessage=User not found.
incorrectPasswordMessage=Current password is incorrect.
usernameExistsMessage=New Username already exists.

View File

@ -16,7 +16,21 @@
<!-- 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="${changeCredsFlag}" class="alert alert-success" th:text="#{changeCredsMessage}"></div> <div th:if="${notAuthenticated}" class="alert alert-danger" role="alert">
User not authenticated.
</div>
<div th:if="${userNotFound}" class="alert alert-danger" role="alert">
User not found.
</div>
<div th:if="${incorrectPassword}" class="alert alert-danger" role="alert">
Current password is incorrect.
</div>
<div th:if="${usernameExists}" class="alert alert-danger" role="alert">
New username already exists.
</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,6 +16,18 @@
<!-- 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>
<div th:if="${userNotFound}" class="alert alert-danger" role="alert">
User not found.
</div>
<div th:if="${incorrectPassword}" class="alert alert-danger" role="alert">
Current password is incorrect.
</div>
<div th:if="${usernameExists}" class="alert alert-danger" role="alert">
New username already exists.
</div>
<div th:if="${changeCredsFlag}" class="alert alert-success" th:text="#{changeCredsMessage}"></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 -->

View File

@ -179,11 +179,13 @@ 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("defaultLocale", defaultLocale);
console.log("storedLocale", storedLocale) console.log("storedLocale", storedLocale);
console.log("currentLangParam", currentLangParam) console.log("currentLangParam", currentLangParam);
if (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();
@ -237,15 +239,18 @@ function handleDropdownItemClick(event) {
const dropdown = document.getElementById('languageDropdown'); const dropdown = document.getElementById('languageDropdown');
if (languageCode) { if (languageCode) {
localStorage.setItem('languageCode', languageCode); localStorage.setItem('languageCode', languageCode);
const currentLang = document.documentElement.getAttribute('lang');
const currentUrl = window.location.href; if (currentLang !== languageCode) {
if (currentUrl.indexOf('?lang=') === -1) { console.log("currentLang", currentLang)
window.location.href = currentUrl + '?lang=' + languageCode; console.log("languageCode", languageCode)
} else { const currentUrl = window.location.href;
window.location.href = currentUrl.replace(/\?lang=\w{2,}/, '?lang=' + languageCode); if (currentUrl.indexOf('?lang=') === -1) {
} window.location.href = currentUrl + '?lang=' + languageCode;
} else {
window.location.href = currentUrl.replace(/\?lang=\w{2,}/, '?lang=' + languageCode);
}
}
dropdown.innerHTML = event.currentTarget.innerHTML; // Update the dropdown button's content dropdown.innerHTML = event.currentTarget.innerHTML; // Update the dropdown button's content
} else { } else {
console.error("Language code is not set for this item."); console.error("Language code is not set for this item.");
@ -258,6 +263,8 @@ 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>
<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>