Skip to content

Commit

Permalink
Added logout button
Browse files Browse the repository at this point in the history
  • Loading branch information
ScarletRedMan committed Mar 25, 2024
1 parent c5d6e8d commit 38cadc8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,49 @@

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());
}

Expand All @@ -39,6 +57,19 @@ private Component createLogo() {
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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public void logout() {
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()));
}
}

0 comments on commit 38cadc8

Please sign in to comment.