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.3KB

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