0

In my Vaadin application I have a grid displaying book information. I do need to add a context menu to the grid but it shall be conditional. For anonymous user (i.e. hasn't logged in yet) there shall be no context menu. For logged in user, menu items shall depend on user privileges. I.e. regular user can only add or edit/delete his personal book review. Admin users shall be able to do more - edit book information, change status, etc.

Shall I set/reset context menu after login occurs? If two users simultaneously using the application, will they get different context menus? When user logged out, shall I set context menu to null?

No code is written yet, therefore I am not providing it here.

You advise will be greatly appreciated.

Gary Greenberg
  • 468
  • 1
  • 9
  • 22
  • There is no right or wrong IMO, but it would be simpler to check the security context upon user attempt to click secondary mouse button on a row (or whatever action you use for showing the context menu), and then build the context menu based on user login status and their privileges. Two users definitely have different session and security context, so no collision should happen for them. – STaefi Dec 15 '22 at 13:18

1 Answers1

0

I found the solution by extending GridContextMenu and adding User property into it;

public class BookContextMenu extends GridContextMenu<Book> {

private User user;

private final PanelsConfig config;
private final GridMenuItem<Book> editBookMenuItem;
private final GridMenuItem<Book> reviewMenuItem;

public BookContextMenu(PanelsConfig panelsConfig) {
    super();
    this.config = panelsConfig;
    this.editBookMenuItem = addItem("Edit book", 
            event -> event.getItem().ifPresent(book -> {
        BookPanel bookPanel = config.getBookPanel();
        bookPanel.setBook(book);
        bookPanel.open();
    }));
    this.reviewMenuItem = addItem("Reader's review", 
            event -> event.getItem().ifPresent(book -> {
        ReviewPanel reviewPanel = config.getReviewPanel();
        reviewPanel.setUser(user);
        reviewPanel.setBook(book);
        reviewPanel.open();
    }));
    if (user == null || !user.isReaderActive()) {
        editBookMenuItem.setVisible(false);
        reviewMenuItem.setVisible(false);
    } else if (!user.isAdmin()) {
        editBookMenuItem.setVisible(false);
    }
}

public void setUser(User user) {
    this.user = user;
    if (user == null || !user.isActive()) {
        editBookMenuItem.setVisible(false);
        reviewMenuItem.setVisible(false);
    } else {
        this.setEnabled(true);
        reviewMenuItem.setVisible(true);
        if (user.isAdmin()) {
            editBookMenuItem.setVisible(true);
        }
    }
}

}

Gary Greenberg
  • 468
  • 1
  • 9
  • 22