1 | package eu.clarin.cmdi.virtualcollectionregistry.gui.pages; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.virtualcollectionregistry.gui.Application; |
---|
4 | import org.apache.wicket.AttributeModifier; |
---|
5 | import org.apache.wicket.authorization.IAuthorizationStrategy; |
---|
6 | import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeAction; |
---|
7 | import org.apache.wicket.markup.html.WebPage; |
---|
8 | import org.apache.wicket.markup.html.basic.Label; |
---|
9 | import org.apache.wicket.markup.html.link.BookmarkablePageLink; |
---|
10 | import org.apache.wicket.markup.html.link.Link; |
---|
11 | import org.apache.wicket.markup.html.panel.Panel; |
---|
12 | import org.apache.wicket.model.AbstractReadOnlyModel; |
---|
13 | import org.apache.wicket.model.IModel; |
---|
14 | import org.apache.wicket.model.Model; |
---|
15 | import org.apache.wicket.util.lang.Classes; |
---|
16 | |
---|
17 | @SuppressWarnings("serial") |
---|
18 | public class MenuItem<T extends WebPage> extends Panel { |
---|
19 | |
---|
20 | private static final String ENABLE = "ENABLE"; |
---|
21 | private final String pageClassName; |
---|
22 | |
---|
23 | public MenuItem(final IModel<String> title, final Class<T> pageClass) { |
---|
24 | super("menuitem"); |
---|
25 | this.pageClassName = pageClass.getName(); |
---|
26 | final Link<T> link = new BookmarkablePageLink<T>("link", pageClass) |
---|
27 | .setAutoEnable(false); |
---|
28 | link.add(new Label("title", title).setRenderBodyOnly(true)); |
---|
29 | add(link); |
---|
30 | |
---|
31 | add(new AttributeModifier("class", true, new AbstractReadOnlyModel<String>() { |
---|
32 | @Override |
---|
33 | public String getObject() { |
---|
34 | if (pageClass.equals(getPage().getClass())) { |
---|
35 | return "active"; |
---|
36 | } else { |
---|
37 | return null; |
---|
38 | } |
---|
39 | } |
---|
40 | })); |
---|
41 | } |
---|
42 | |
---|
43 | @Override |
---|
44 | protected void onBeforeRender() { |
---|
45 | final IAuthorizationStrategy strategy |
---|
46 | = getApplication().getSecuritySettings() |
---|
47 | .getAuthorizationStrategy(); |
---|
48 | final Class<T> pageClass = getPageClass(); |
---|
49 | if (!strategy.isInstantiationAuthorized(pageClass)) { |
---|
50 | boolean visible = true; |
---|
51 | AuthorizeAction action |
---|
52 | = pageClass.getAnnotation(AuthorizeAction.class); |
---|
53 | if ((action != null) && ENABLE.equalsIgnoreCase(action.action())) { |
---|
54 | final Application app = (Application) getApplication(); |
---|
55 | if (app.hasAnyRole(action.deny())) { |
---|
56 | visible = false; |
---|
57 | } else { |
---|
58 | visible = app.hasAnyRole(action.roles()); |
---|
59 | } |
---|
60 | } |
---|
61 | setVisible(visible); |
---|
62 | } |
---|
63 | super.onBeforeRender(); |
---|
64 | } |
---|
65 | |
---|
66 | private Class<T> getPageClass() { |
---|
67 | return Classes.resolveClass(pageClassName); |
---|
68 | } |
---|
69 | |
---|
70 | } // class MenuItem |
---|