1 | package eu.clarin.cmdi.virtualcollectionregistry.gui.pages; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.virtualcollectionregistry.AdminUsersService; |
---|
4 | import eu.clarin.cmdi.virtualcollectionregistry.gui.ApplicationSession; |
---|
5 | import java.security.Principal; |
---|
6 | import javax.servlet.http.HttpServletRequest; |
---|
7 | import org.apache.wicket.RestartResponseException; |
---|
8 | import org.apache.wicket.WicketRuntimeException; |
---|
9 | import org.apache.wicket.markup.html.WebPage; |
---|
10 | import org.apache.wicket.markup.html.panel.FeedbackPanel; |
---|
11 | import org.apache.wicket.model.IModel; |
---|
12 | import org.apache.wicket.model.Model; |
---|
13 | import org.apache.wicket.spring.injection.annot.SpringBean; |
---|
14 | |
---|
15 | public class BasePage extends WebPage { |
---|
16 | |
---|
17 | @SpringBean |
---|
18 | private AdminUsersService adminUsersService; |
---|
19 | |
---|
20 | protected BasePage(IModel<?> model) { |
---|
21 | super(model); |
---|
22 | // authentication state |
---|
23 | add(new AuthenticationStatePanel("authstate")); |
---|
24 | |
---|
25 | // main navigation menu |
---|
26 | final Menu menu = new Menu("menu"); |
---|
27 | menu.addMenuItem(new MenuItem<BrowsePublicCollectionsPage>( |
---|
28 | new Model<String>("Virtual Collections"), |
---|
29 | BrowsePublicCollectionsPage.class)); |
---|
30 | menu.addMenuItem(new MenuItem<BrowsePrivateCollectionsPage>( |
---|
31 | new Model<String>("My Virtual Collections"), |
---|
32 | BrowsePrivateCollectionsPage.class)); |
---|
33 | menu.addMenuItem(new MenuItem<CreateVirtualCollectionPage>( |
---|
34 | new Model<String>("Create Virtual Collection"), |
---|
35 | CreateVirtualCollectionPage.class)); |
---|
36 | menu.addMenuItem(new MenuItem<AdminPage>( |
---|
37 | new Model<String>("Admin Page"), |
---|
38 | AdminPage.class)); |
---|
39 | add(menu); |
---|
40 | |
---|
41 | add(new FeedbackPanel("feedback")); |
---|
42 | } |
---|
43 | |
---|
44 | protected BasePage() { |
---|
45 | this(null); |
---|
46 | } |
---|
47 | |
---|
48 | @Override |
---|
49 | protected void onBeforeRender() { |
---|
50 | // skip lazy auto-auth for login page |
---|
51 | if (!this.getClass().isInstance(LoginPage.class)) { |
---|
52 | final HttpServletRequest request = |
---|
53 | getWebRequestCycle().getWebRequest().getHttpServletRequest(); |
---|
54 | final ApplicationSession session = |
---|
55 | (ApplicationSession) getSession(); |
---|
56 | if (!session.isSignedIn()) { |
---|
57 | if (request.getAuthType() != null) { |
---|
58 | // FIXME: better logging |
---|
59 | System.err.println("Auth, but no authed session -> login"); |
---|
60 | final Principal principal = request.getUserPrincipal(); |
---|
61 | if (!session.signIn(principal)) { |
---|
62 | throw new RestartResponseException(getApplication() |
---|
63 | .getApplicationSettings() |
---|
64 | .getAccessDeniedPage()); |
---|
65 | } |
---|
66 | } |
---|
67 | } else { |
---|
68 | if (request.getAuthType() == null) { |
---|
69 | // FIXME: better logging |
---|
70 | System.err.println("Lost Session!"); |
---|
71 | session.invalidate(); |
---|
72 | throw new RestartResponseException(getApplication() |
---|
73 | .getApplicationSettings() |
---|
74 | .getPageExpiredErrorPage()); |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | super.onBeforeRender(); |
---|
79 | } |
---|
80 | |
---|
81 | protected Principal getUser() { |
---|
82 | ApplicationSession session = (ApplicationSession) getSession(); |
---|
83 | Principal principal = session.getPrincipal(); |
---|
84 | if (principal == null) { |
---|
85 | throw new WicketRuntimeException("principal == null"); |
---|
86 | } |
---|
87 | return principal; |
---|
88 | } |
---|
89 | |
---|
90 | protected boolean isUserAdmin() { |
---|
91 | final String userName = getUser().getName(); |
---|
92 | return userName != null && adminUsersService.isAdmin(userName); |
---|
93 | } |
---|
94 | |
---|
95 | @Override |
---|
96 | public ApplicationSession getSession() { |
---|
97 | return (ApplicationSession) super.getSession(); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | } // class BasePage |
---|