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