From d75e84bdffe987c797cc6d92c33662afffb9d78b Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Sun, 13 Aug 2023 01:14:14 +0100 Subject: [PATCH] Create InitialSetup.java --- .../SPDF/config/security/InitialSetup.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/java/stirling/software/SPDF/config/security/InitialSetup.java diff --git a/src/main/java/stirling/software/SPDF/config/security/InitialSetup.java b/src/main/java/stirling/software/SPDF/config/security/InitialSetup.java new file mode 100644 index 00000000..64923dc7 --- /dev/null +++ b/src/main/java/stirling/software/SPDF/config/security/InitialSetup.java @@ -0,0 +1,27 @@ +package stirling.software.SPDF.config.security; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Component; + +import jakarta.annotation.PostConstruct; +import stirling.software.SPDF.model.Role; +@Component +public class InitialSetup { + + @Autowired + private UserService userService; + + @PostConstruct + public void init() { + if(!userService.hasUsers()) { + String initialUsername = System.getenv("INITIAL_USERNAME"); + String initialPassword = System.getenv("INITIAL_PASSWORD"); + if(initialUsername != null && initialPassword != null) { + userService.saveUser(initialUsername, initialPassword, Role.ADMIN); + } else { + userService.saveUser("admin", "password", Role.ADMIN); + } + } + } +}