Added logout button
This commit is contained in:
parent
c5d6e8d0dc
commit
38cadc804f
@ -2,31 +2,49 @@ package ru.dragonestia.picker.cp.page;
|
||||
|
||||
import com.vaadin.flow.component.Component;
|
||||
import com.vaadin.flow.component.Html;
|
||||
import com.vaadin.flow.component.Unit;
|
||||
import com.vaadin.flow.component.applayout.AppLayout;
|
||||
import com.vaadin.flow.component.applayout.DrawerToggle;
|
||||
import com.vaadin.flow.component.button.Button;
|
||||
import com.vaadin.flow.component.html.Hr;
|
||||
import com.vaadin.flow.component.html.Span;
|
||||
import com.vaadin.flow.component.icon.Icon;
|
||||
import com.vaadin.flow.component.icon.VaadinIcon;
|
||||
import com.vaadin.flow.component.orderedlayout.FlexComponent;
|
||||
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
|
||||
import com.vaadin.flow.component.orderedlayout.Scroller;
|
||||
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
|
||||
import com.vaadin.flow.component.sidenav.SideNav;
|
||||
import com.vaadin.flow.component.sidenav.SideNavItem;
|
||||
import ru.dragonestia.picker.api.impl.RoomPickerClient;
|
||||
import ru.dragonestia.picker.api.repository.response.RoomPickerInfoResponse;
|
||||
import ru.dragonestia.picker.cp.annotation.ServerURL;
|
||||
import ru.dragonestia.picker.cp.model.Account;
|
||||
import ru.dragonestia.picker.cp.service.SecurityService;
|
||||
|
||||
public class MainLayout extends AppLayout {
|
||||
|
||||
private final SecurityService securityService;
|
||||
private final RoomPickerInfoResponse serverInfo;
|
||||
private final String serverUrl;
|
||||
private final Account account;
|
||||
private final boolean isAdmin;
|
||||
|
||||
public MainLayout(RoomPickerClient adminClient, @ServerURL String serverUrl) {
|
||||
public MainLayout(SecurityService securityService, RoomPickerClient adminClient, @ServerURL String serverUrl) {
|
||||
this.securityService = securityService;
|
||||
this.serverInfo = adminClient.getServerInfo();
|
||||
this.serverUrl = serverUrl;
|
||||
account = securityService.getAuthenticatedAccount();
|
||||
isAdmin = securityService.hasRole("ADMIN");
|
||||
|
||||
var toggle = new DrawerToggle();
|
||||
var scroller = new Scroller(createSideNav());
|
||||
scroller.setWidth(100, Unit.PERCENTAGE);
|
||||
|
||||
addToDrawer(scroller);
|
||||
var navLayout = new VerticalLayout(createAccountButtons(), new Hr(), scroller);
|
||||
navLayout.setPadding(false);
|
||||
|
||||
addToDrawer(navLayout);
|
||||
addToNavbar(toggle, createLogo());
|
||||
}
|
||||
|
||||
@ -39,6 +57,19 @@ public class MainLayout extends AppLayout {
|
||||
return layout;
|
||||
}
|
||||
|
||||
private Component createAccountButtons() {
|
||||
var layout = new VerticalLayout();
|
||||
var username = new Span(new Icon(isAdmin? VaadinIcon.USER_STAR : VaadinIcon.USER));
|
||||
username.add(account.getUsername());
|
||||
layout.add(username);
|
||||
|
||||
var logoutButton = new Button("Logout", event -> securityService.logout());
|
||||
logoutButton.setWidth(100, Unit.PERCENTAGE);
|
||||
layout.add(logoutButton);
|
||||
|
||||
return layout;
|
||||
}
|
||||
|
||||
private SideNav createSideNav() {
|
||||
var nav = new SideNav();
|
||||
nav.addItem(new SideNavItem("Nodes list", NodesPage.class, VaadinIcon.FOLDER_O.create()));
|
||||
|
||||
@ -25,4 +25,9 @@ public class SecurityService {
|
||||
var logoutHandler = new SecurityContextLogoutHandler();
|
||||
logoutHandler.logout(VaadinServletRequest.getCurrent().getHttpServletRequest(), null, null);
|
||||
}
|
||||
|
||||
public boolean hasRole(String role) {
|
||||
var r = "ROLE_" + role;
|
||||
return getAuthenticatedAccount().getAuthorities().stream().anyMatch(permission -> r.equals(permission.getAuthority()));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user