From c716570bc2b063e2f2ad56c996cbd395d34db815 Mon Sep 17 00:00:00 2001 From: filippo-ferrari Date: Sun, 8 Sep 2024 16:56:55 +0200 Subject: [PATCH] fix: other major changes --- .../com/application/munera/SecurityUtils.java | 16 +++++++ .../com/application/munera/data/Person.java | 2 +- .../munera/services/PersonService.java | 3 ++ .../munera/services/UserService.java | 19 ++++++++ .../munera/views/expenses/ExpensesView.java | 46 ++++++++++++++++++- 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/application/munera/SecurityUtils.java create mode 100644 src/main/java/com/application/munera/services/UserService.java diff --git a/src/main/java/com/application/munera/SecurityUtils.java b/src/main/java/com/application/munera/SecurityUtils.java new file mode 100644 index 0000000..433305c --- /dev/null +++ b/src/main/java/com/application/munera/SecurityUtils.java @@ -0,0 +1,16 @@ +package com.application.munera; + +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; + +public class SecurityUtils { + + public static UserDetails getLoggedInUserDetails() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication != null && authentication.getPrincipal() instanceof UserDetails) { + return (UserDetails) authentication.getPrincipal(); + } + return null; + } +} \ No newline at end of file diff --git a/src/main/java/com/application/munera/data/Person.java b/src/main/java/com/application/munera/data/Person.java index 5277eb1..5a5f001 100644 --- a/src/main/java/com/application/munera/data/Person.java +++ b/src/main/java/com/application/munera/data/Person.java @@ -50,7 +50,7 @@ public class Person extends AbstractEntity { @ManyToMany(mappedBy = "participants") private Set events; - @Column(name = "Username", unique = true, nullable = false) + @Column(name = "Username", unique = true) private String username; // This field will link to the User entity @Column(name = "UserId", unique = true) diff --git a/src/main/java/com/application/munera/services/PersonService.java b/src/main/java/com/application/munera/services/PersonService.java index a0893c6..198f95f 100644 --- a/src/main/java/com/application/munera/services/PersonService.java +++ b/src/main/java/com/application/munera/services/PersonService.java @@ -40,6 +40,9 @@ public class PersonService { return this.personRepository.findAll(); } + public Optional findByUserId(Long id) { + return this.personRepository.findByUserId(id); + } /** * Finds all people excluding the users'ones. diff --git a/src/main/java/com/application/munera/services/UserService.java b/src/main/java/com/application/munera/services/UserService.java new file mode 100644 index 0000000..85f934c --- /dev/null +++ b/src/main/java/com/application/munera/services/UserService.java @@ -0,0 +1,19 @@ +package com.application.munera.services; + +import com.application.munera.data.User; +import com.application.munera.repositories.UserRepository; +import org.springframework.stereotype.Service; + +@Service +public class UserService { + + private final UserRepository userRepository; + + public UserService(UserRepository userRepository) { + this.userRepository = userRepository; + } + + public User findByUsername (String username) { + return this.userRepository.findByUsername(username); + } +} 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 3a9daf6..63d8cd2 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -1,5 +1,6 @@ package com.application.munera.views.expenses; +import com.application.munera.SecurityUtils; import com.application.munera.data.*; import com.application.munera.services.*; import com.application.munera.views.MainLayout; @@ -27,7 +28,9 @@ import com.vaadin.flow.data.converter.StringToBigDecimalConverter; import com.vaadin.flow.data.renderer.ComponentRenderer; import com.vaadin.flow.router.*; 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.vaadin.klaudeta.PaginatedGrid; import java.util.Objects; @@ -57,6 +60,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver { private final PersonService personService; private final EventService eventService; private final ViewService viewService; + private final UserService userService; private TextField name; private TextField cost; private ComboBox category; @@ -70,12 +74,14 @@ public class ExpensesView extends Div implements BeforeEnterObserver { private ComboBox beneficiary; private ComboBox event; - public ExpensesView(ExpenseService expenseService, CategoryService categoryService, PersonService personService, EventService eventService, ViewService viewService) { + @Autowired + public ExpensesView(ExpenseService expenseService, CategoryService categoryService, PersonService personService, EventService eventService, ViewService viewService, UserService userService) { this.expenseService = expenseService; this.categoryService = categoryService; this.personService = personService; this.eventService = eventService; this.viewService = viewService; + this.userService = userService; addClassNames("expenses-view"); // Create UI @@ -152,7 +158,21 @@ public class ExpensesView extends Div implements BeforeEnterObserver { } }); - +// // Event listeners that will remove the selected creditors from the debtors list and vice versa +// // Done so that the user cant create an expense with the same person as creditor and debtor +// payer.addValueChangeListener(event -> { +// Person selectedDebtors = event.getValue(); +// final var creditorsSet = new HashSet<>(personService.findAllWithoutUser()); +// creditorsSet.removeIf(creditorsSet::contains); +// payer.setItems(creditorsSet); +// }); +// +// beneficiary.addValueChangeListener(event -> { +// Person selectedCreditors = event.getValue(); +// final var debtorsSet = new HashSet<>(personService.findAllWithoutUser()); +// debtorsSet.removeIf(debtorsSet::contains); +// beneficiary.setItems(debtorsSet); +// }); cancel.addClickListener(e -> { clearForm(); @@ -193,10 +213,14 @@ public class ExpensesView extends Div implements BeforeEnterObserver { n.addThemeVariants(NotificationVariant.LUMO_ERROR); } }); + + // Initialize ComboBox with the logged-in user's Person entity as default + initializeComboBoxes(); } @Override public void beforeEnter(BeforeEnterEvent event) { + initializeComboBoxes(); Optional expenseId = event.getRouteParameters().get(EXPENSE_ID).map(Long::parseLong); if (expenseId.isPresent()) { Optional expenseFromBackend = expenseService.get(expenseId.get()); @@ -289,4 +313,22 @@ public class ExpensesView extends Div implements BeforeEnterObserver { periodUnit.setVisible(isPeriodicChecked); periodInterval.setVisible(isPeriodicChecked); } + + private void initializeComboBoxes() { + // Fetch the logged-in user's Person entity + UserDetails userDetails = SecurityUtils.getLoggedInUserDetails(); + if (userDetails != null) { + String username = userDetails.getUsername(); + final var user = this.userService.findByUsername(username); + if (user != null) { + Optional loggedInPerson = personService.findByUserId(user.getId()); + if (loggedInPerson.isPresent()) { + Person person = loggedInPerson.get(); + // Set default values for payer and beneficiary ComboBoxes + payer.setValue(person); + beneficiary.setValue(person); + } + } + } + } } \ No newline at end of file