SideNavItem and @RolesAllowed when making a hierarchical menu

Probably an embarrassing simple oversight on my part, but I can’t seem to find the solution.

I would like to have a menu like this:
image

But when I build all menu items myself it doesn’t respect the @RolesAllowed or @PermitAll.

I have views like this:

@PageTitle("Company list")
@Menu(order = 2, icon = "vaadin:clipboard-check", title = "Companies")
@Route(value = "company", layout = MainLayout.class)
@PermitAll // When security is enabled, allow all authenticated users
public class TenantCompanyPreApprovedView extends VerticalLayout {

and this:

@PageTitle("Document Scraping")
@Menu(order = 5, icon = "line-awesome/svg/home-solid.svg")
@Route(value = "docscraping", layout = MainLayout.class)
@RolesAllowed({ SecurityRole.Fields.Administrator})
public class DocumentScrapingView extends VerticalLayout {

and my code in MainLayout looks like this:

private SideNav createNavigation() {

        SideNav nav = new SideNav();
/*

        // This respects the @RolesAllowed, but gives me no hierarchy for the menu items
        List<MenuEntry> menuEntries = MenuConfiguration.getMenuEntries();
        menuEntries.forEach(entry -> {
            if (entry.icon() != null) {
                System.out.println(entry.title() + " - " + entry.path());
                nav.addItem(new SideNavItem(entry.title(), entry.path(), new SvgIcon(entry.icon())));
            } else {
                nav.addItem(new SideNavItem(entry.title(), entry.path()));
            }
        });
*/

        // This gives me a hierarchy for the menu items, but it doesn't respect the @RolesAllowed
        SideNavItem sideNavSkiSetup = new SideNavItem("SKI settings");                                                              // How to set @RolesAllowed on a menu item without a view??
        sideNavSkiSetup.setPrefixComponent(VaadinIcon.COG.create());
        // sideNavSkiSetup.addItem(new SideNavItem("Load Appendix", AppendixLoaderView.class,  VaadinIcon.FILE_TEXT.create()));            // appendixloader
        sideNavSkiSetup.addItem(new SideNavItem("Load Appendix", "/appendixloader",  VaadinIcon.FILE_TEXT.create()));            // appendixloader
        sideNavSkiSetup.addItem(new SideNavItem("Load Appendix Demands", AppendixDemandLoaderView.class,  VaadinIcon.FILE_TEXT.create()));
        sideNavSkiSetup.addItem(new SideNavItem("Doc scraping demo", DocumentScrapingView.class,  VaadinIcon.SPINNER.create()));

        nav.addItem(new SideNavItem("Welcome", FrontpageView.class, LineAwesomeIcon.INFO_SOLID.create()));
        // nav.addItem(new SideNavItem("Companies", TenantCompanyPreApprovedView.class, LineAwesomeIcon.INFO_SOLID.create()));         // company
        nav.addItem(new SideNavItem("Companies", "/company", LineAwesomeIcon.INFO_SOLID.create()));         // company
        nav.addItem(sideNavSkiSetup);


        return nav;
}

The “menuEntries.forEach(entry → {” … code respects the @RolesAllowed

The currently uncommented code where I add the sideNavItems manually, does not respect the @RolesAllowed or @PermitAll.

On top of that, how do I make the top level menu item:

SideNavItem sideNavSkiSetup = new SideNavItem("SKI settings");    

respect any kind of @RolesAllowed or @PermitAll?

Br
Mikael

Filtering by access control is not done by the SideNav component but instead by MenuConfiguration.getMenuEntries() in the original code that you commented out. There’s lots of indirection involved in how this works but you can have a look at this snippet for the core of the implementation:

1 Like