fix: solved TODO

This commit is contained in:
filippo-ferrari 2024-09-10 18:40:35 +02:00
parent 7e15be27c1
commit 11ab5e4b55
7 changed files with 22 additions and 21 deletions

View file

@ -4,13 +4,11 @@ import com.application.munera.data.User;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import javax.annotation.Nonnull;
import java.util.Optional; import java.util.Optional;
@Repository @Repository
public interface UserRepository extends JpaRepository<User, Long> { public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(final String username); Optional<User> findByUsername(final @Nonnull String username);
//TODO: join these two methods
Optional<User> findOptionalByUsername(final String username);
} }

View file

@ -42,10 +42,9 @@ public class SecurityConfiguration extends VaadinWebSecurity {
return new InMemoryUserDetailsManager() { return new InMemoryUserDetailsManager() {
@Override @Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
com.application.munera.data.User user = userRepository.findByUsername(username); final var user = userRepository.findByUsername(username)
if (user == null) { .orElseThrow(() -> new UsernameNotFoundException("User not found"));
throw new UsernameNotFoundException("User not found");
}
return User.withUsername(user.getUsername()) return User.withUsername(user.getUsername())
.password(user.getPassword()) .password(user.getPassword())
.roles(user.getRoles().split(",")) .roles(user.getRoles().split(","))

View file

@ -1,16 +1,17 @@
package com.application.munera.services; package com.application.munera.services;
import com.application.munera.security.SecurityUtils;
import com.application.munera.data.Expense; import com.application.munera.data.Expense;
import com.application.munera.data.ExpenseType; import com.application.munera.data.ExpenseType;
import com.application.munera.data.Person; import com.application.munera.data.Person;
import com.application.munera.repositories.ExpenseRepository; import com.application.munera.repositories.ExpenseRepository;
import com.application.munera.repositories.PersonRepository; import com.application.munera.repositories.PersonRepository;
import com.application.munera.repositories.UserRepository; import com.application.munera.repositories.UserRepository;
import com.application.munera.security.SecurityUtils;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -225,7 +226,8 @@ public class ExpenseService {
throw new IllegalStateException("No logged-in user found"); throw new IllegalStateException("No logged-in user found");
} }
// Fetch the logged-in user // 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); Person loggedInPerson = this.personRepository.findByUserId(loggedInUserId).orElse(null);
if (loggedInPerson == null) throw new IllegalStateException("No associated Person entity found for logged-in user"); if (loggedInPerson == null) throw new IllegalStateException("No associated Person entity found for logged-in user");

View file

@ -6,6 +6,7 @@ import com.application.munera.repositories.PersonRepository;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -88,7 +89,7 @@ public class PersonService {
* @return Person entity of the logged-in user, or null if not found. * @return Person entity of the logged-in user, or null if not found.
*/ */
public Person getLoggedInPerson() { 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)); return Objects.requireNonNull(personRepository.findByUserId(user.getId()).orElse(null));
} }

View file

@ -7,6 +7,8 @@ import com.application.munera.repositories.UserRepository;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Optional;
import static com.application.munera.security.SecurityUtils.getLoggedInUserDetails; import static com.application.munera.security.SecurityUtils.getLoggedInUserDetails;
@Service @Service
@ -20,15 +22,16 @@ public class UserService {
this.personRepository = personRepository; this.personRepository = personRepository;
} }
public User findByUsername (String username) { public Optional<User> findByUsername (String username) {
return this.userRepository.findByUsername(username); return this.userRepository.findByUsername(username);
} }
/** /**
* Fetches the logged-in User entity. * Fetches the logged-in User entity.
*
* @return User entity of the logged-in user, or null if not found. * @return User entity of the logged-in user, or null if not found.
*/ */
public User getLoggedInUser() { public Optional<User> getLoggedInUser() {
UserDetails userDetails = getLoggedInUserDetails(); UserDetails userDetails = getLoggedInUserDetails();
if (userDetails != null) { if (userDetails != null) {
String username = userDetails.getUsername(); 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 //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 // 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; User existingUser;

View file

@ -31,6 +31,7 @@ import jakarta.annotation.security.PermitAll;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ObjectOptimisticLockingFailureException; import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.vaadin.klaudeta.PaginatedGrid; import org.vaadin.klaudeta.PaginatedGrid;
import java.util.Objects; import java.util.Objects;
@ -319,8 +320,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
UserDetails userDetails = SecurityUtils.getLoggedInUserDetails(); UserDetails userDetails = SecurityUtils.getLoggedInUserDetails();
if (userDetails != null) { if (userDetails != null) {
String username = userDetails.getUsername(); String username = userDetails.getUsername();
final var user = this.userService.findByUsername(username); final var user = this.userService.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException("User not found"));
if (user != null) {
Optional<Person> loggedInPerson = personService.findByUserId(user.getId()); Optional<Person> loggedInPerson = personService.findByUserId(user.getId());
if (loggedInPerson.isPresent()) { if (loggedInPerson.isPresent()) {
Person person = loggedInPerson.get(); Person person = loggedInPerson.get();
@ -328,7 +328,6 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
payer.setValue(person); payer.setValue(person);
beneficiary.setValue(person); beneficiary.setValue(person);
} }
}
} }
} }
} }

View file

@ -1,6 +1,5 @@
package com.application.munera.views.settings; package com.application.munera.views.settings;
import com.application.munera.data.User;
import com.application.munera.services.UserService; import com.application.munera.services.UserService;
import com.application.munera.views.MainLayout; import com.application.munera.views.MainLayout;
import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.Button;
@ -17,6 +16,7 @@ import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route; import com.vaadin.flow.router.Route;
import jakarta.annotation.security.PermitAll; import jakarta.annotation.security.PermitAll;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@PageTitle("Settings") @PageTitle("Settings")
@PermitAll @PermitAll
@ -54,9 +54,8 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver
} }
private void saveUserData() { 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.setFirstName(firstNameField.getValue());
loggedInUser.setLastName(lastNameField.getValue()); loggedInUser.setLastName(lastNameField.getValue());
@ -76,7 +75,7 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver
@Override @Override
public void beforeEnter(BeforeEnterEvent event) { 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()); firstNameField.setValue(loggedInUser.getFirstName());
lastNameField.setValue(loggedInUser.getLastName()); lastNameField.setValue(loggedInUser.getLastName());
monthlyIncomeField.setValue(""); //TODO: implement monthly income monthlyIncomeField.setValue(""); //TODO: implement monthly income