Access control for views

The Navigator API provides a simple mechanism to allow or disallow navigating to a View. Before a View is shown, each ViewChangeListener that is registered with the Navigator is given the opportunity to veto the View change.

One can also make the View itself trigger a navigation to another View in navigateTo(), but let’s take a look at the more flexible beforeViewChange() and afterViewChange(), that exists specifically for this purpose.

First, let’s continue from previous examples and create a MessageView for secret messages:

Java

  1. import com.vaadin.navigator.View;
  2. import com.vaadin.ui.Label;
  3. public class SecretView extends MessageView implements View {
  4. public static final String NAME = "secret";
  5. public SecretView() {
  6. setCaption("Private messages");
  7. ((Layout) getContent()).addComponent(new Label("Some private stuff."));
  8. }
  9. }

As you can see, there is absolutely nothing special going on here, we just customize the View enough to be able to distinguish from the regular MessageView.

Next, we’ll register this new View with the Navigator, exactly as before. At this point our SecretView is not secret at all, but let’s fix that by adding a ViewChangeListener to the Navigator:

Java

  1. navigator.addViewChangeListener(new ViewChangeListener() {
  2. @Override
  3. public boolean beforeViewChange(ViewChangeEvent event) {
  4. if (event.getNewView() instanceof SecretView &&
  5. ((NavigationtestUI)UI.getCurrent()).getLoggedInUser() == null) {
  6. Notification.show("Permission denied", Type.ERROR_MESSAGE);
  7. return false;
  8. } else {
  9. return true;
  10. }
  11. }
  12. @Override
  13. public void afterViewChange(ViewChangeEvent event) {
  14. }
  15. });

So if we’re on our way to the SecretView, but not logged in (getLoggedInUser() == null), the View change is cancelled. Quite simple rules in our case, but you could check anything - most probably you’ll want to call a helper method that checks the user for permission.

Let’s go ahead and add some links to the MainView again, so that we don’t have to muck with the address-bar to try it out:

Java

  1. import com.vaadin.navigator.View;
  2. import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
  3. import com.vaadin.server.ExternalResource;
  4. import com.vaadin.ui.Button;
  5. import com.vaadin.ui.Button.ClickEvent;
  6. import com.vaadin.ui.Link;
  7. import com.vaadin.ui.Panel;
  8. import com.vaadin.ui.UI;
  9. import com.vaadin.ui.VerticalLayout;
  10. public class MainView extends Panel implements View {
  11. public static final String NAME = "";
  12. public MainView() {
  13. VerticalLayout layout = new VerticalLayout();
  14. Link lnk = new Link("Count", new ExternalResource("#!" + CountView.NAME));
  15. layout.addComponent(lnk);
  16. lnk = new Link("Message: Hello", new ExternalResource("#!"
  17. + MessageView.NAME + "/Hello"));
  18. layout.addComponent(lnk);
  19. lnk = new Link("Message: Bye", new ExternalResource("#!"
  20. + MessageView.NAME + "/Bye/Goodbye"));
  21. layout.addComponent(lnk);
  22. lnk = new Link("Private message: Secret", new ExternalResource("#!"
  23. + SecretView.NAME + "/Secret"));
  24. layout.addComponent(lnk);
  25. lnk = new Link("Private message: Topsecret", new ExternalResource("#!"
  26. + SecretView.NAME + "/Topsecret"));
  27. layout.addComponent(lnk);
  28. // login/logout toggle so we can test this
  29. Button logInOut = new Button("Toggle login",
  30. new Button.ClickListener() {
  31. public void buttonClick(ClickEvent event) {
  32. Object user = ((NavigationtestUI)UI.getCurrent()).getLoggedInUser();
  33. ((NavigationtestUI)UI.getCurrent()).setLoggedInUser(
  34. user == null ? "Smee" : null);
  35. }
  36. });
  37. layout.addComponent(logInOut);
  38. setContent(layout);
  39. }
  40. @Override
  41. public void enter(ViewChangeEvent event) {
  42. }
  43. }

Instead of just showing a notification and leaving the user wondering, we should obviously allow the user to log in and continue. We’ll do just that in the separate tutorial about Handling login, but for now we just add a button that toggles our logged in/out state.

Meanwhile, here is the the full source for the UI so far:

Java

  1. import com.vaadin.navigator.Navigator;
  2. import com.vaadin.navigator.ViewChangeListener;
  3. import com.vaadin.server.VaadinRequest;
  4. import com.vaadin.ui.Notification;
  5. import com.vaadin.ui.Notification.Type;
  6. import com.vaadin.ui.UI;
  7. public class NavigationtestUI extends UI {
  8. Navigator navigator;
  9. String loggedInUser;
  10. @Override
  11. public void init(VaadinRequest request) {
  12. // Create Navigator, make it control the ViewDisplay
  13. navigator = new Navigator(this, this);
  14. // Add some Views
  15. navigator.addView(MainView.NAME, new MainView()); // no fragment
  16. // #!count will be a new instance each time we navigate to it, counts:
  17. navigator.addView(CountView.NAME, CountView.class);
  18. // #!message adds a label with whatever it receives as a parameter
  19. navigator.addView(MessageView.NAME, new MessageView());
  20. // #!secret works as #!message, but you need to be logged in
  21. navigator.addView(SecretView.NAME, new SecretView());
  22. // we'll handle permissions with a listener here, you could also do
  23. // that in the View itself.
  24. navigator.addViewChangeListener(new ViewChangeListener() {
  25. @Override
  26. public boolean beforeViewChange(ViewChangeEvent event) {
  27. if (event.getNewView() instanceof SecretView
  28. && ((NavigationtestUI)UI.getCurrent()).getLoggedInUser() == null) {
  29. Notification.show("Permission denied", Type.ERROR_MESSAGE);
  30. return false;
  31. } else {
  32. return true;
  33. }
  34. }
  35. @Override
  36. public void afterViewChange(ViewChangeEvent event) {
  37. System.out.println("After view change");
  38. }
  39. });
  40. }
  41. public String getLoggedInUser(){
  42. return loggedInUser;
  43. }
  44. public void setLoggedInUser(String user){
  45. loggedInUser = user;
  46. }
  47. }