From 11ab5e4b551027edd486e7dda86f9c072a9a0aea Mon Sep 17 00:00:00 2001 From: filippo-ferrari Date: Tue, 10 Sep 2024 18:40:35 +0200 Subject: [PATCH] fix: solved TODO --- .../application/munera/repositories/UserRepository.java | 6 ++---- .../munera/security/SecurityConfiguration.java | 7 +++---- .../com/application/munera/services/ExpenseService.java | 6 ++++-- .../com/application/munera/services/PersonService.java | 3 ++- .../com/application/munera/services/UserService.java | 9 ++++++--- .../application/munera/views/expenses/ExpensesView.java | 5 ++--- .../application/munera/views/settings/SettingsView.java | 7 +++---- 7 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/application/munera/repositories/UserRepository.java b/src/main/java/com/application/munera/repositories/UserRepository.java index 6b385f3..7a627fe 100644 --- a/src/main/java/com/application/munera/repositories/UserRepository.java +++ b/src/main/java/com/application/munera/repositories/UserRepository.java @@ -4,13 +4,11 @@ import com.application.munera.data.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import javax.annotation.Nonnull; 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); + Optional findByUsername(final @Nonnull String username); } diff --git a/src/main/java/com/application/munera/security/SecurityConfiguration.java b/src/main/java/com/application/munera/security/SecurityConfiguration.java index 0762d63..a43c1f6 100644 --- a/src/main/java/com/application/munera/security/SecurityConfiguration.java +++ b/src/main/java/com/application/munera/security/SecurityConfiguration.java @@ -42,10 +42,9 @@ public class SecurityConfiguration extends VaadinWebSecurity { return new InMemoryUserDetailsManager() { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - com.application.munera.data.User user = userRepository.findByUsername(username); - if (user == null) { - throw new UsernameNotFoundException("User not found"); - } + final var user = userRepository.findByUsername(username) + .orElseThrow(() -> new UsernameNotFoundException("User not found")); + return User.withUsername(user.getUsername()) .password(user.getPassword()) .roles(user.getRoles().split(",")) diff --git a/src/main/java/com/application/munera/services/ExpenseService.java b/src/main/java/com/application/munera/services/ExpenseService.java index 7a83fb0..4a8da53 100644 --- a/src/main/java/com/application/munera/services/ExpenseService.java +++ b/src/main/java/com/application/munera/services/ExpenseService.java @@ -1,16 +1,17 @@ package com.application.munera.services; -import com.application.munera.security.SecurityUtils; import com.application.munera.data.Expense; import com.application.munera.data.ExpenseType; import com.application.munera.data.Person; import com.application.munera.repositories.ExpenseRepository; import com.application.munera.repositories.PersonRepository; import com.application.munera.repositories.UserRepository; +import com.application.munera.security.SecurityUtils; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import javax.annotation.Nonnull; @@ -225,7 +226,8 @@ public class ExpenseService { throw new IllegalStateException("No logged-in user found"); } // Fetch the logged-in user - final var loggedInUserId = userRepository.findByUsername(userDetails.getUsername()).getId(); + final var loggedInUserId = userRepository.findByUsername(userDetails.getUsername()) + .orElseThrow(() -> new UsernameNotFoundException("User not found")).getId(); Person loggedInPerson = this.personRepository.findByUserId(loggedInUserId).orElse(null); if (loggedInPerson == null) throw new IllegalStateException("No associated Person entity found for logged-in user"); diff --git a/src/main/java/com/application/munera/services/PersonService.java b/src/main/java/com/application/munera/services/PersonService.java index 4e9a177..e15b2dc 100644 --- a/src/main/java/com/application/munera/services/PersonService.java +++ b/src/main/java/com/application/munera/services/PersonService.java @@ -6,6 +6,7 @@ import com.application.munera.repositories.PersonRepository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import java.math.BigDecimal; @@ -88,7 +89,7 @@ public class PersonService { * @return Person entity of the logged-in user, or null if not found. */ public Person getLoggedInPerson() { - final var user = userService.getLoggedInUser(); + final var user = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found")); return Objects.requireNonNull(personRepository.findByUserId(user.getId()).orElse(null)); } diff --git a/src/main/java/com/application/munera/services/UserService.java b/src/main/java/com/application/munera/services/UserService.java index 1828d32..6ebb6e8 100644 --- a/src/main/java/com/application/munera/services/UserService.java +++ b/src/main/java/com/application/munera/services/UserService.java @@ -7,6 +7,8 @@ import com.application.munera.repositories.UserRepository; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; +import java.util.Optional; + import static com.application.munera.security.SecurityUtils.getLoggedInUserDetails; @Service @@ -20,15 +22,16 @@ public class UserService { this.personRepository = personRepository; } - public User findByUsername (String username) { + public Optional findByUsername (String username) { return this.userRepository.findByUsername(username); } /** * Fetches the logged-in User entity. + * * @return User entity of the logged-in user, or null if not found. */ - public User getLoggedInUser() { + public Optional getLoggedInUser() { UserDetails userDetails = getLoggedInUserDetails(); if (userDetails != null) { String username = userDetails.getUsername(); @@ -58,7 +61,7 @@ public class UserService { //TODO: look if this method can substitute the one above: updateUser, they seem to do similar things // Check if the user already exists in the database - final var existingUserOptional = userRepository.findOptionalByUsername(user.getUsername()); + final var existingUserOptional = userRepository.findByUsername(user.getUsername()); User existingUser; 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 d171f6b..d3c1b1c 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -31,6 +31,7 @@ import jakarta.annotation.security.PermitAll; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.ObjectOptimisticLockingFailureException; import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.vaadin.klaudeta.PaginatedGrid; import java.util.Objects; @@ -319,8 +320,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver { UserDetails userDetails = SecurityUtils.getLoggedInUserDetails(); if (userDetails != null) { String username = userDetails.getUsername(); - final var user = this.userService.findByUsername(username); - if (user != null) { + final var user = this.userService.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException("User not found")); Optional loggedInPerson = personService.findByUserId(user.getId()); if (loggedInPerson.isPresent()) { Person person = loggedInPerson.get(); @@ -328,7 +328,6 @@ public class ExpensesView extends Div implements BeforeEnterObserver { payer.setValue(person); beneficiary.setValue(person); } - } } } } \ No newline at end of file diff --git a/src/main/java/com/application/munera/views/settings/SettingsView.java b/src/main/java/com/application/munera/views/settings/SettingsView.java index f91a5f5..d27096b 100644 --- a/src/main/java/com/application/munera/views/settings/SettingsView.java +++ b/src/main/java/com/application/munera/views/settings/SettingsView.java @@ -1,6 +1,5 @@ package com.application.munera.views.settings; -import com.application.munera.data.User; import com.application.munera.services.UserService; import com.application.munera.views.MainLayout; import com.vaadin.flow.component.button.Button; @@ -17,6 +16,7 @@ import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; import jakarta.annotation.security.PermitAll; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.userdetails.UsernameNotFoundException; @PageTitle("Settings") @PermitAll @@ -54,9 +54,8 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver } private void saveUserData() { - User loggedInUser = userService.getLoggedInUser(); + final var loggedInUser = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found")); - // Assuming you have methods to update user details loggedInUser.setFirstName(firstNameField.getValue()); loggedInUser.setLastName(lastNameField.getValue()); @@ -76,7 +75,7 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver @Override public void beforeEnter(BeforeEnterEvent event) { - final var loggedInUser = userService.getLoggedInUser(); + final var loggedInUser = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found")); firstNameField.setValue(loggedInUser.getFirstName()); lastNameField.setValue(loggedInUser.getLastName()); monthlyIncomeField.setValue(""); //TODO: implement monthly income