You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AccessControlForViews.asciidoc 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. ---
  2. title: Access Control For Views
  3. order: 46
  4. layout: page
  5. ---
  6. [[access-control-for-views]]
  7. Access control for views
  8. ------------------------
  9. The Navigator API provides a simple mechanism to allow or disallow
  10. navigating to a View. Before a View is shown, each ViewChangeListener
  11. that is registered with the Navigator is given the opportunity to veto
  12. the View change.
  13. One can also make the View itself trigger a navigation to another View
  14. in navigateTo(), but let's take a look at the more flexible
  15. beforeViewChange() and afterViewChange(), that exists specifically for
  16. this purpose.
  17. First, let's continue from previous examples and create a MessageView
  18. for secret messages:
  19. [source,java]
  20. ....
  21. import com.vaadin.navigator.View;
  22. import com.vaadin.ui.Label;
  23. public class SecretView extends MessageView implements View {
  24. public static final String NAME = "secret";
  25. public SecretView() {
  26. setCaption("Private messages");
  27. ((Layout) getContent()).addComponent(new Label("Some private stuff."));
  28. }
  29. }
  30. ....
  31. As you can see, there is absolutely nothing special going on here, we
  32. just customize the View enough to be able to distinguish from the
  33. regular MessageView.
  34. Next, we'll register this new View with the Navigator, exactly as
  35. before. At this point our SecretView is not secret at all, but let's fix
  36. that by adding a ViewChangeListener to the Navigator:
  37. [source,java]
  38. ....
  39. navigator.addViewChangeListener(new ViewChangeListener() {
  40. @Override
  41. public boolean beforeViewChange(ViewChangeEvent event) {
  42. if (event.getNewView() instanceof SecretView &&
  43. ((NavigationtestUI)UI.getCurrent()).getLoggedInUser() == null) {
  44. Notification.show("Permission denied", Type.ERROR_MESSAGE);
  45. return false;
  46. } else {
  47. return true;
  48. }
  49. }
  50. @Override
  51. public void afterViewChange(ViewChangeEvent event) {
  52. }
  53. });
  54. ....
  55. So if we're on our way to the SecretView, but not logged in
  56. (getLoggedInUser() == null), the View change is cancelled. Quite simple
  57. rules in our case, but you could check anything - most probably you'll
  58. want to call a helper method that checks the user for permission.
  59. Let's go ahead and add some links to the MainView again, so that we
  60. don't have to muck with the address-bar to try it out:
  61. [source,java]
  62. ....
  63. import com.vaadin.navigator.View;
  64. import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
  65. import com.vaadin.server.ExternalResource;
  66. import com.vaadin.ui.Button;
  67. import com.vaadin.ui.Button.ClickEvent;
  68. import com.vaadin.ui.Link;
  69. import com.vaadin.ui.Panel;
  70. import com.vaadin.ui.UI;
  71. import com.vaadin.ui.VerticalLayout;
  72. public class MainView extends Panel implements View {
  73. public static final String NAME = "";
  74. public MainView() {
  75. VerticalLayout layout = new VerticalLayout();
  76. Link lnk = new Link("Count", new ExternalResource("#!" + CountView.NAME));
  77. layout.addComponent(lnk);
  78. lnk = new Link("Message: Hello", new ExternalResource("#!"
  79. + MessageView.NAME + "/Hello"));
  80. layout.addComponent(lnk);
  81. lnk = new Link("Message: Bye", new ExternalResource("#!"
  82. + MessageView.NAME + "/Bye/Goodbye"));
  83. layout.addComponent(lnk);
  84. lnk = new Link("Private message: Secret", new ExternalResource("#!"
  85. + SecretView.NAME + "/Secret"));
  86. layout.addComponent(lnk);
  87. lnk = new Link("Private message: Topsecret", new ExternalResource("#!"
  88. + SecretView.NAME + "/Topsecret"));
  89. layout.addComponent(lnk);
  90. // login/logout toggle so we can test this
  91. Button logInOut = new Button("Toggle login",
  92. new Button.ClickListener() {
  93. public void buttonClick(ClickEvent event) {
  94. Object user = ((NavigationtestUI)UI.getCurrent()).getLoggedInUser();
  95. ((NavigationtestUI)UI.getCurrent()).setLoggedInUser(
  96. user == null ? "Smee" : null);
  97. }
  98. });
  99. layout.addComponent(logInOut);
  100. setContent(layout);
  101. }
  102. @Override
  103. public void enter(ViewChangeEvent event) {
  104. }
  105. }
  106. ....
  107. Instead of just showing a notification and leaving the user wondering,
  108. we should obviously allow the user to log in and continue. We'll do just
  109. that in the separate tutorial about Handling login, but for now we just
  110. add a button that toggles our logged in/out state.
  111. Meanwhile, here is the the full source for the UI so far:
  112. [source,java]
  113. ....
  114. import com.vaadin.navigator.Navigator;
  115. import com.vaadin.navigator.ViewChangeListener;
  116. import com.vaadin.server.VaadinRequest;
  117. import com.vaadin.ui.Notification;
  118. import com.vaadin.ui.Notification.Type;
  119. import com.vaadin.ui.UI;
  120. public class NavigationtestUI extends UI {
  121. Navigator navigator;
  122. String loggedInUser;
  123. @Override
  124. public void init(VaadinRequest request) {
  125. // Create Navigator, make it control the ViewDisplay
  126. navigator = new Navigator(this, this);
  127. // Add some Views
  128. navigator.addView(MainView.NAME, new MainView()); // no fragment
  129. // #!count will be a new instance each time we navigate to it, counts:
  130. navigator.addView(CountView.NAME, CountView.class);
  131. // #!message adds a label with whatever it receives as a parameter
  132. navigator.addView(MessageView.NAME, new MessageView());
  133. // #!secret works as #!message, but you need to be logged in
  134. navigator.addView(SecretView.NAME, new SecretView());
  135. // we'll handle permissions with a listener here, you could also do
  136. // that in the View itself.
  137. navigator.addViewChangeListener(new ViewChangeListener() {
  138. @Override
  139. public boolean beforeViewChange(ViewChangeEvent event) {
  140. if (event.getNewView() instanceof SecretView
  141. && ((NavigationtestUI)UI.getCurrent()).getLoggedInUser() == null) {
  142. Notification.show("Permission denied", Type.ERROR_MESSAGE);
  143. return false;
  144. } else {
  145. return true;
  146. }
  147. }
  148. @Override
  149. public void afterViewChange(ViewChangeEvent event) {
  150. System.out.println("After view change");
  151. }
  152. });
  153. }
  154. public String getLoggedInUser(){
  155. return loggedInUser;
  156. }
  157. public void setLoggedInUser(String user){
  158. loggedInUser = user;
  159. }
  160. }
  161. ....