Skip to content

Commit

Permalink
Merge pull request #1394 from Stirling-Tools/disableConfigUpdater
Browse files Browse the repository at this point in the history
Disable config updater
  • Loading branch information
Frooodle authored Jun 6, 2024
2 parents e11fa01 + 03150c6 commit 7b08d98
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 77 deletions.
82 changes: 41 additions & 41 deletions src/main/java/stirling/software/SPDF/config/ConfigInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContextInitializer;
Expand Down Expand Up @@ -45,46 +44,47 @@ public void ensureConfigExists() throws IOException, URISyntaxException {
}
}
} else {
// Path templatePath =
// Paths.get(
// getClass()
// .getClassLoader()
// .getResource("settings.yml.template")
// .toURI());
// Path userPath = Paths.get("configs", "settings.yml");
//
// List<String> templateLines = Files.readAllLines(templatePath);
// List<String> userLines =
// Files.exists(userPath) ? Files.readAllLines(userPath) : new ArrayList<>();
//
// List<String> resultLines = new ArrayList<>();
// int position = 0;
// for (String templateLine : templateLines) {
// // Check if the line is a comment
// if (templateLine.trim().startsWith("#")) {
// String entry = templateLine.trim().substring(1).trim();
// if (!entry.isEmpty()) {
// // Check if this comment has been uncommented in userLines
// String key = entry.split(":")[0].trim();
// addLine(resultLines, userLines, templateLine, key, position);
// } else {
// resultLines.add(templateLine);
// }
// }
// // Check if the line is a key-value pair
// else if (templateLine.contains(":")) {
// String key = templateLine.split(":")[0].trim();
// addLine(resultLines, userLines, templateLine, key, position);
// }
// // Handle empty lines
// else if (templateLine.trim().length() == 0) {
// resultLines.add("");
// }
// position++;
// }
//
// // Write the result to the user settings file
// Files.write(userPath, resultLines);
// Path templatePath =
// Paths.get(
// getClass()
// .getClassLoader()
// .getResource("settings.yml.template")
// .toURI());
// Path userPath = Paths.get("configs", "settings.yml");
//
// List<String> templateLines = Files.readAllLines(templatePath);
// List<String> userLines =
// Files.exists(userPath) ? Files.readAllLines(userPath) : new
// ArrayList<>();
//
// List<String> resultLines = new ArrayList<>();
// int position = 0;
// for (String templateLine : templateLines) {
// // Check if the line is a comment
// if (templateLine.trim().startsWith("#")) {
// String entry = templateLine.trim().substring(1).trim();
// if (!entry.isEmpty()) {
// // Check if this comment has been uncommented in userLines
// String key = entry.split(":")[0].trim();
// addLine(resultLines, userLines, templateLine, key, position);
// } else {
// resultLines.add(templateLine);
// }
// }
// // Check if the line is a key-value pair
// else if (templateLine.contains(":")) {
// String key = templateLine.split(":")[0].trim();
// addLine(resultLines, userLines, templateLine, key, position);
// }
// // Handle empty lines
// else if (templateLine.trim().length() == 0) {
// resultLines.add("");
// }
// position++;
// }
//
// // Write the result to the user settings file
// Files.write(userPath, resultLines);
}

Path customSettingsPath = Paths.get("configs", "custom_settings.yml");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package stirling.software.SPDF.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.templateresolver.AbstractConfigurableTemplateResolver;
import org.thymeleaf.templateresource.ClassLoaderTemplateResource;
import org.thymeleaf.templateresource.FileTemplateResource;
import org.thymeleaf.templateresource.ITemplateResource;

import stirling.software.SPDF.model.InputStreamTemplateResource;

public class FileFallbackTemplateResolver extends AbstractConfigurableTemplateResolver {

private final ResourceLoader resourceLoader;
Expand Down Expand Up @@ -40,9 +42,13 @@ protected ITemplateResource computeTemplateResource(

}

return new ClassLoaderTemplateResource(
Thread.currentThread().getContextClassLoader(),
"classpath:/templates/" + resourceName,
characterEncoding);
InputStream inputStream =
Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("templates/" + resourceName);
if (inputStream != null) {
return new InputStreamTemplateResource(inputStream, "UTF-8");
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,46 +66,46 @@ public RedirectView changeUsername(
RedirectAttributes redirectAttributes) {

if (!userService.isUsernameValid(newUsername)) {
return new RedirectView("/account?messageType=invalidUsername",true);
return new RedirectView("/account?messageType=invalidUsername", true);
}

if (principal == null) {
return new RedirectView("/account?messageType=notAuthenticated",true);
return new RedirectView("/account?messageType=notAuthenticated", true);
}

// The username MUST be unique when renaming
Optional<User> userOpt = userService.findByUsername(principal.getName());

if (userOpt == null || userOpt.isEmpty()) {
return new RedirectView("/account?messageType=userNotFound",true);
return new RedirectView("/account?messageType=userNotFound", true);
}

User user = userOpt.get();

if (user.getUsername().equals(newUsername)) {
return new RedirectView("/account?messageType=usernameExists",true);
return new RedirectView("/account?messageType=usernameExists", true);
}

if (!userService.isPasswordCorrect(user, currentPassword)) {
return new RedirectView("/account?messageType=incorrectPassword",true);
return new RedirectView("/account?messageType=incorrectPassword", true);
}

if (!user.getUsername().equals(newUsername) && userService.usernameExists(newUsername)) {
return new RedirectView("/account?messageType=usernameExists",true);
return new RedirectView("/account?messageType=usernameExists", true);
}

if (newUsername != null && newUsername.length() > 0) {
try {
userService.changeUsername(user, newUsername);
} catch (IllegalArgumentException e) {
return new RedirectView("/account?messageType=invalidUsername",true);
return new RedirectView("/account?messageType=invalidUsername", true);
}
}

// Logout using Spring's utility
new SecurityContextLogoutHandler().logout(request, response, null);

return new RedirectView(LOGIN_MESSAGETYPE_CREDSUPDATED,true);
return new RedirectView(LOGIN_MESSAGETYPE_CREDSUPDATED, true);
}

@PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")
Expand All @@ -118,27 +118,27 @@ public RedirectView changePasswordOnLogin(
HttpServletResponse response,
RedirectAttributes redirectAttributes) {
if (principal == null) {
return new RedirectView("/change-creds?messageType=notAuthenticated",true);
return new RedirectView("/change-creds?messageType=notAuthenticated", true);
}

Optional<User> userOpt = userService.findByUsernameIgnoreCase(principal.getName());

if (userOpt == null || userOpt.isEmpty()) {
return new RedirectView("/change-creds?messageType=userNotFound",true);
return new RedirectView("/change-creds?messageType=userNotFound", true);
}

User user = userOpt.get();

if (!userService.isPasswordCorrect(user, currentPassword)) {
return new RedirectView("/change-creds?messageType=incorrectPassword",true);
return new RedirectView("/change-creds?messageType=incorrectPassword", true);
}

userService.changePassword(user, newPassword);
userService.changeFirstUse(user, false);
// Logout using Spring's utility
new SecurityContextLogoutHandler().logout(request, response, null);

return new RedirectView(LOGIN_MESSAGETYPE_CREDSUPDATED,true);
return new RedirectView(LOGIN_MESSAGETYPE_CREDSUPDATED, true);
}

@PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")
Expand All @@ -151,27 +151,27 @@ public RedirectView changePassword(
HttpServletResponse response,
RedirectAttributes redirectAttributes) {
if (principal == null) {
return new RedirectView("/account?messageType=notAuthenticated",true);
return new RedirectView("/account?messageType=notAuthenticated", true);
}

Optional<User> userOpt = userService.findByUsernameIgnoreCase(principal.getName());

if (userOpt == null || userOpt.isEmpty()) {
return new RedirectView("/account?messageType=userNotFound",true);
return new RedirectView("/account?messageType=userNotFound", true);
}

User user = userOpt.get();

if (!userService.isPasswordCorrect(user, currentPassword)) {
return new RedirectView("/account?messageType=incorrectPassword",true);
return new RedirectView("/account?messageType=incorrectPassword", true);
}

userService.changePassword(user, newPassword);

// Logout using Spring's utility
new SecurityContextLogoutHandler().logout(request, response, null);

return new RedirectView(LOGIN_MESSAGETYPE_CREDSUPDATED,true);
return new RedirectView(LOGIN_MESSAGETYPE_CREDSUPDATED, true);
}

@PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")
Expand Down Expand Up @@ -204,34 +204,35 @@ public RedirectView saveUser(
boolean forceChange) {

if (!userService.isUsernameValid(username)) {
return new RedirectView("/addUsers?messageType=invalidUsername",true);
return new RedirectView("/addUsers?messageType=invalidUsername", true);
}

Optional<User> userOpt = userService.findByUsernameIgnoreCase(username);

if (userOpt.isPresent()) {
User user = userOpt.get();
if (user != null && user.getUsername().equalsIgnoreCase(username)) {
return new RedirectView("/addUsers?messageType=usernameExists",true);
return new RedirectView("/addUsers?messageType=usernameExists", true);
}
}
if (userService.usernameExistsIgnoreCase(username)) {
return new RedirectView("/addUsers?messageType=usernameExists",true);
return new RedirectView("/addUsers?messageType=usernameExists", true);
}
try {
// Validate the role
Role roleEnum = Role.fromString(role);
if (roleEnum == Role.INTERNAL_API_USER) {
// If the role is INTERNAL_API_USER, reject the request
return new RedirectView("/addUsers?messageType=invalidRole",true);
return new RedirectView("/addUsers?messageType=invalidRole", true);
}
} catch (IllegalArgumentException e) {
// If the role ID is not valid, redirect with an error message
return new RedirectView("/addUsers?messageType=invalidRole",true);
return new RedirectView("/addUsers?messageType=invalidRole", true);
}

userService.saveUser(username, password, role, forceChange);
return new RedirectView("/addUsers",true); // Redirect to account page after adding the user
return new RedirectView(
"/addUsers", true); // Redirect to account page after adding the user
}

@PreAuthorize("hasRole('ROLE_ADMIN')")
Expand All @@ -244,33 +245,34 @@ public RedirectView changeRole(
Optional<User> userOpt = userService.findByUsernameIgnoreCase(username);

if (!userOpt.isPresent()) {
return new RedirectView("/addUsers?messageType=userNotFound",true);
return new RedirectView("/addUsers?messageType=userNotFound", true);
}
if (!userService.usernameExistsIgnoreCase(username)) {
return new RedirectView("/addUsers?messageType=userNotFound",true);
return new RedirectView("/addUsers?messageType=userNotFound", true);
}
// Get the currently authenticated username
String currentUsername = authentication.getName();

// Check if the provided username matches the current session's username
if (currentUsername.equalsIgnoreCase(username)) {
return new RedirectView("/addUsers?messageType=downgradeCurrentUser",true);
return new RedirectView("/addUsers?messageType=downgradeCurrentUser", true);
}
try {
// Validate the role
Role roleEnum = Role.fromString(role);
if (roleEnum == Role.INTERNAL_API_USER) {
// If the role is INTERNAL_API_USER, reject the request
return new RedirectView("/addUsers?messageType=invalidRole",true);
return new RedirectView("/addUsers?messageType=invalidRole", true);
}
} catch (IllegalArgumentException e) {
// If the role ID is not valid, redirect with an error message
return new RedirectView("/addUsers?messageType=invalidRole",true);
return new RedirectView("/addUsers?messageType=invalidRole", true);
}
User user = userOpt.get();

userService.changeRole(user, role);
return new RedirectView("/addUsers",true); // Redirect to account page after adding the user
return new RedirectView(
"/addUsers", true); // Redirect to account page after adding the user
}

@PreAuthorize("hasRole('ROLE_ADMIN')")
Expand All @@ -279,19 +281,19 @@ public RedirectView deleteUser(
@PathVariable(name = "username") String username, Authentication authentication) {

if (!userService.usernameExistsIgnoreCase(username)) {
return new RedirectView("/addUsers?messageType=deleteUsernameExists",true);
return new RedirectView("/addUsers?messageType=deleteUsernameExists", true);
}

// Get the currently authenticated username
String currentUsername = authentication.getName();

// Check if the provided username matches the current session's username
if (currentUsername.equalsIgnoreCase(username)) {
return new RedirectView("/addUsers?messageType=deleteCurrentUser",true);
return new RedirectView("/addUsers?messageType=deleteCurrentUser", true);
}
invalidateUserSessions(username);
userService.deleteUser(username);
return new RedirectView("/addUsers",true);
return new RedirectView("/addUsers", true);
}

@Autowired private SessionRegistry sessionRegistry;
Expand Down
Loading

0 comments on commit 7b08d98

Please sign in to comment.