diff --git a/docker-compose.yml b/docker-compose.yml index dc1f0b7..056154d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,12 @@ services: SPRING_DATASOURCE_URL: ${DB_ADDRESS}/${DB_NAME} SPRING_DATASOURCE_USERNAME: ${DB_USER} SPRING_DATASOURCE_PASSWORD: ${DB_PASSWORD} + ADMIN_USERNAME: ${ADMIN_USERNAME} + ADMIN_PASSWORD: ${ADMIN_PASSWORD} + ADMIN_FIRST_NAME: ${ADMIN_FIRST_NAME} + ADMIN_LAST_NAME: ${ADMIN_LAST_NAME} + ADMIN_ROLES: ${ADMIN_ROLES} + ADMIN_EMAIL: ${ADMIN_EMAIL} depends_on: - db diff --git a/src/main/java/com/application/munera/StartupInitializer.java b/src/main/java/com/application/munera/StartupInitializer.java deleted file mode 100644 index 302917d..0000000 --- a/src/main/java/com/application/munera/StartupInitializer.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.application.munera; - -import com.application.munera.data.Person; -import com.application.munera.repositories.PersonRepository; -import com.application.munera.repositories.UserRepository; -import jakarta.annotation.PostConstruct; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - - -@Component -public class StartupInitializer { - - private final UserRepository userRepository; - private final PersonRepository personRepository; - - @Autowired - public StartupInitializer(UserRepository userRepository, PersonRepository personRepository) { - this.userRepository = userRepository; - this.personRepository = personRepository; - } - - /** - * Initializes Person entities for all existing users. - */ - @PostConstruct - public void initializePersonEntities() { - userRepository.findAll().forEach(user -> { - // Check if Person entity exists for the user by userId - if (personRepository.findByUserId(user.getId()).isEmpty()) { - // Create and save the Person entity - Person newPerson = new Person(); - newPerson.setFirstName(user.getFirstName()); - newPerson.setLastName(user.getLastName()); - newPerson.setUsername(user.getUsername()); - newPerson.setUserId(user.getId()); - personRepository.save(newPerson); - } - }); - } -} diff --git a/src/main/java/com/application/munera/data/Person.java b/src/main/java/com/application/munera/data/Person.java index 5a5f001..806613e 100644 --- a/src/main/java/com/application/munera/data/Person.java +++ b/src/main/java/com/application/munera/data/Person.java @@ -40,7 +40,6 @@ public class Person extends AbstractEntity { @Column(name = "credit") private BigDecimal credit; - // Updated to match the new field names in Expense @OneToMany(mappedBy = "payer") private Set expensesAsPayer; diff --git a/src/main/java/com/application/munera/repositories/UserRepository.java b/src/main/java/com/application/munera/repositories/UserRepository.java index 3c0daac..6b385f3 100644 --- a/src/main/java/com/application/munera/repositories/UserRepository.java +++ b/src/main/java/com/application/munera/repositories/UserRepository.java @@ -4,8 +4,13 @@ import com.application.munera.data.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface UserRepository extends JpaRepository { User findByUsername(final String username); + + //TODO: join these two methods + Optional findOptionalByUsername(final String username); } diff --git a/src/main/java/com/application/munera/security/AdminProperties.java b/src/main/java/com/application/munera/security/AdminProperties.java new file mode 100644 index 0000000..a976d2e --- /dev/null +++ b/src/main/java/com/application/munera/security/AdminProperties.java @@ -0,0 +1,19 @@ +package com.application.munera.security; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Getter +@Setter +@Configuration +@ConfigurationProperties(prefix = "admin") +public class AdminProperties { + private String username; + private String password; + private String firstName; + private String lastName; + private String roles; + private String email; +} \ No newline at end of file diff --git a/src/main/java/com/application/munera/SecurityConfiguration.java b/src/main/java/com/application/munera/security/SecurityConfiguration.java similarity index 98% rename from src/main/java/com/application/munera/SecurityConfiguration.java rename to src/main/java/com/application/munera/security/SecurityConfiguration.java index 555aad5..0762d63 100644 --- a/src/main/java/com/application/munera/SecurityConfiguration.java +++ b/src/main/java/com/application/munera/security/SecurityConfiguration.java @@ -1,4 +1,4 @@ -package com.application.munera; +package com.application.munera.security; import com.application.munera.repositories.UserRepository; import com.application.munera.views.login.LoginView; diff --git a/src/main/java/com/application/munera/SecurityUtils.java b/src/main/java/com/application/munera/security/SecurityUtils.java similarity index 93% rename from src/main/java/com/application/munera/SecurityUtils.java rename to src/main/java/com/application/munera/security/SecurityUtils.java index 433305c..4ac1065 100644 --- a/src/main/java/com/application/munera/SecurityUtils.java +++ b/src/main/java/com/application/munera/security/SecurityUtils.java @@ -1,4 +1,4 @@ -package com.application.munera; +package com.application.munera.security; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; diff --git a/src/main/java/com/application/munera/security/UserInitializer.java b/src/main/java/com/application/munera/security/UserInitializer.java new file mode 100644 index 0000000..10035ec --- /dev/null +++ b/src/main/java/com/application/munera/security/UserInitializer.java @@ -0,0 +1,31 @@ +package com.application.munera.security; + +import com.application.munera.data.User; +import com.application.munera.services.UserService; +import jakarta.annotation.PostConstruct; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class UserInitializer { + + @Autowired + private UserService userService; + + @Autowired + private AdminProperties adminProperties; + + @PostConstruct + public void init() { + if (userService.count() == 0) { + User adminUser = new User(); + adminUser.setUsername(adminProperties.getUsername()); + adminUser.setPassword(adminProperties.getPassword()); + adminUser.setFirstName(adminProperties.getFirstName()); + adminUser.setLastName(adminProperties.getLastName()); + adminUser.setRoles(adminProperties.getRoles()); + adminUser.setEmail(adminProperties.getEmail()); + userService.saveUserAndConnectedPerson(adminUser); + } + } +} diff --git a/src/main/java/com/application/munera/services/ExpenseService.java b/src/main/java/com/application/munera/services/ExpenseService.java index 9df0391..bed2480 100644 --- a/src/main/java/com/application/munera/services/ExpenseService.java +++ b/src/main/java/com/application/munera/services/ExpenseService.java @@ -1,6 +1,6 @@ package com.application.munera.services; -import com.application.munera.SecurityUtils; +import com.application.munera.security.SecurityUtils; import com.application.munera.data.Expense; import com.application.munera.data.ExpenseType; import com.application.munera.data.Person; diff --git a/src/main/java/com/application/munera/services/UserService.java b/src/main/java/com/application/munera/services/UserService.java index edf5862..0cf5048 100644 --- a/src/main/java/com/application/munera/services/UserService.java +++ b/src/main/java/com/application/munera/services/UserService.java @@ -7,7 +7,7 @@ import com.application.munera.repositories.UserRepository; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; -import static com.application.munera.SecurityUtils.getLoggedInUserDetails; +import static com.application.munera.security.SecurityUtils.getLoggedInUserDetails; @Service public class UserService { @@ -39,9 +39,38 @@ public class UserService { public void updateUser(User user) { userRepository.save(user); - Person person = personRepository.findByUserId(user.getId()) + final var person = personRepository.findByUserId(user.getId()) .orElseThrow(() -> new IllegalStateException("Associated Person not found")); person.setFirstName(user.getFirstName()); person.setLastName(user.getLastName()); personRepository.save(person); - }} + } + + public void saveUserAndConnectedPerson(User user) { + // Check if the user already exists in the database + final var existingUserOptional = userRepository.findOptionalByUsername(user.getUsername()); + + User existingUser; + + // Save the new user entity if he doesn't exist yet + existingUser = existingUserOptional.orElseGet(() -> userRepository.save(user)); + + // Check if the associated person exists for the user + final var existingPerson = personRepository.findByUserId(existingUser.getId()); + + if (existingPerson.isEmpty()) { + // If no person is associated with the user, create a new Person entity and link it to the User + Person person = new Person(); + person.setUsername(existingUser.getUsername()); + person.setFirstName(existingUser.getFirstName()); + person.setLastName(existingUser.getLastName()); + person.setEmail(existingUser.getEmail()); + person.setUserId(existingUser.getId()); + personRepository.save(person); + } + } + + public Long count() { + return this.userRepository.count(); + } +} diff --git a/src/main/java/com/application/munera/views/expenses/ExpensesView.java b/src/main/java/com/application/munera/views/expenses/ExpensesView.java index a294a29..d171f6b 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -1,6 +1,6 @@ package com.application.munera.views.expenses; -import com.application.munera.SecurityUtils; +import com.application.munera.security.SecurityUtils; import com.application.munera.data.*; import com.application.munera.services.*; import com.application.munera.views.MainLayout; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index d3d3cc6..a431fdf 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -11,6 +11,15 @@ spring.profiles.active=dev spring.datasource.url = ${DB_ADDRESS}/${DB_NAME} spring.datasource.username = ${DB_USER} spring.datasource.password = ${DB_PASSWORD} + +# Default user +admin.username=${ADMIN_USERNAME} +admin.password=${ADMIN_PASSWORD} +admin.first_name=${ADMIN_FIRST_NAME} +admin.last_name=${ADMIN_LAST_NAME} +admin.roles=${ADMIN_ROLES} +#admin.email=${ADMIN_EMAIL} + spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect spring.jpa.hibernate.ddl-auto = update # To improve the performance during development.