feat: hid UsersView to normal users
This commit is contained in:
parent
dc95b06c6e
commit
8187115de2
1 changed files with 18 additions and 7 deletions
|
@ -2,6 +2,7 @@ package com.application.munera.views;
|
|||
|
||||
import com.application.munera.services.CSVService;
|
||||
import com.application.munera.services.ExpenseService;
|
||||
import com.application.munera.services.UserService;
|
||||
import com.application.munera.views.categories.CategoriesView;
|
||||
import com.application.munera.views.dashboard.DashboardView;
|
||||
import com.application.munera.views.events.EventsView;
|
||||
|
@ -22,6 +23,7 @@ import com.vaadin.flow.router.PageTitle;
|
|||
import com.vaadin.flow.server.StreamResource;
|
||||
import com.vaadin.flow.spring.security.AuthenticationContext;
|
||||
import com.vaadin.flow.theme.lumo.LumoUtility;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.vaadin.lineawesome.LineAwesomeIcon;
|
||||
|
||||
/**
|
||||
|
@ -34,11 +36,13 @@ public class MainLayout extends AppLayout {
|
|||
private final transient AuthenticationContext authContext;
|
||||
private final CSVService csvService;
|
||||
private final ExpenseService expenseService;
|
||||
private final UserService userService;
|
||||
|
||||
public MainLayout(AuthenticationContext authContext, CSVService csvService, ExpenseService expenseService) {
|
||||
public MainLayout(AuthenticationContext authContext, CSVService csvService, ExpenseService expenseService, UserService userService) {
|
||||
this.authContext = authContext;
|
||||
this.csvService = csvService;
|
||||
this.expenseService = expenseService;
|
||||
this.userService = userService;
|
||||
setPrimarySection(Section.DRAWER);
|
||||
addDrawerContent();
|
||||
addHeaderContent();
|
||||
|
@ -112,18 +116,26 @@ public class MainLayout extends AppLayout {
|
|||
|
||||
private SideNav createNavigation() {
|
||||
SideNav nav = new SideNav();
|
||||
|
||||
// Common menu items
|
||||
nav.addItem(new SideNavItem("Expenses", ExpensesView.class, LineAwesomeIcon.MONEY_BILL_SOLID.create()));
|
||||
nav.addItem(new SideNavItem("Categories", CategoriesView.class, LineAwesomeIcon.FOLDER.create()));
|
||||
nav.addItem(new SideNavItem("People", PeopleView.class, LineAwesomeIcon.USER.create()));
|
||||
nav.addItem(new SideNavItem("Events", EventsView.class, LineAwesomeIcon.BANDCAMP.create()));
|
||||
nav.addItem(new SideNavItem("Dashboard", DashboardView.class, LineAwesomeIcon.CHART_LINE_SOLID.create()));
|
||||
nav.addItem(new SideNavItem("Users", UsersView.class, LineAwesomeIcon.USER_LOCK_SOLID.create()));
|
||||
nav.addItem(new SideNavItem("Settings", SettingsView.class, LineAwesomeIcon.COG_SOLID.create()));
|
||||
|
||||
// Check user roles before adding sensitive menu items
|
||||
if (isUserAdmin())
|
||||
nav.addItem(new SideNavItem("Users", UsersView.class, LineAwesomeIcon.USER_LOCK_SOLID.create()));
|
||||
|
||||
nav.addItem(new SideNavItem("Settings", SettingsView.class, LineAwesomeIcon.COG_SOLID.create()));
|
||||
return nav;
|
||||
}
|
||||
|
||||
private boolean isUserAdmin() {
|
||||
final var user = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found"));
|
||||
return user.getRoles().contains("ROLE_ADMIN");
|
||||
}
|
||||
|
||||
private Footer createFooter() {
|
||||
return new Footer();
|
||||
}
|
||||
|
@ -138,7 +150,6 @@ public class MainLayout extends AppLayout {
|
|||
exportToCSVButton.setVisible(isExpensesView);
|
||||
}
|
||||
|
||||
|
||||
private String getCurrentPageTitle() {
|
||||
PageTitle title = getContent().getClass().getAnnotation(PageTitle.class);
|
||||
return title == null ? "" : title.value();
|
||||
|
|
Loading…
Reference in a new issue