2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.photos.wicket;
18 import java.security.Principal;
19 import javax.inject.Inject;
20 import javax.servlet.http.HttpServletRequest;
22 import org.apache.wicket.RedirectToUrlException;
23 import org.apache.wicket.markup.html.CSSPackageResource;
24 import org.apache.wicket.markup.html.WebMarkupContainer;
25 import org.apache.wicket.markup.html.WebPage;
26 import org.apache.wicket.markup.html.basic.Label;
27 import org.apache.wicket.markup.html.link.Link;
28 import org.apache.wicket.markup.html.panel.FeedbackPanel;
29 import org.apache.wicket.model.IModel;
30 import org.wamblee.security.authentication.UserAdministration;
31 import org.wamblee.wicket.behavior.TitleAttributeTooltipBehavior;
32 import org.wamblee.wicket.css.ResetCssBehavior;
33 import org.wamblee.wicket.page.ExpireBehavior;
34 import org.wamblee.wicket.page.WebApplicationBasePage;
36 public class BasePage extends WebApplicationBasePage {
39 private HttpServletRequest request;
42 private transient UserAdministration userAdmin;
44 private boolean isExpired = false;
50 public BasePage(IModel aModel) {
53 Principal userPrincipal = request.getUserPrincipal();
54 if (userPrincipal == null) {
55 throw redirectToLoginPage();
57 String username = userPrincipal.getName();
58 if (isAdminPage() && !isAdministrator(username)) {
59 error("Unauthorized URL accessed");
60 throw redirectToLoginPage();
63 add(new ResetCssBehavior());
64 add(new TitleAttributeTooltipBehavior());
65 add(CSSPackageResource.getHeaderContribution(BasePage.class, "photos.css"));
68 add(new FeedbackPanel("feedback"));
69 add(new Label("title", getTitle()));
71 addBehavior(new ExpireBehavior() {
73 protected boolean isExpired(WebPage aPage) {
78 add(new Link("logout") {
80 public void onClick() {
81 getRequestCycle().getSession().invalidate();
82 throw redirectToLoginPage();
86 WebMarkupContainer adminAccess = new WebMarkupContainer("adminAccess");
87 if (!isAdministrator(username)) {
88 adminAccess.setVisible(false);
93 protected boolean isAdminPage() {
97 protected boolean isAdministrator(String aUsername) {
98 return userAdmin.isInGroup(aUsername, "administrators");
101 private RedirectToUrlException redirectToLoginPage() {
102 return new RedirectToUrlException("login.jsp");
105 public void setExpired(boolean aExpired) {
106 isExpired = aExpired;
109 private String getTitle() {
110 String name = getClass().getSimpleName();
111 name = name.replaceAll("([A-Z]+)([A-Z][a-z])", "$1 $2");
112 name = name.replaceAll("[A-Z]+", " $0");
113 name = name.replaceAll("^ ", "");
114 name = name.replaceAll(" ", " ");