/* * Copyright 2005 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.photos.tapestry; import java.security.AccessController; import javax.security.auth.Subject; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.tapestry.BaseComponent; import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.RedirectException; /** * Border component providing general page layout and functions available on every page. */ public class Border extends BaseComponent { private static final Log LOG = LogFactory.getLog(Border.class); private static final String LOGOUT_URL = "logout.jsp"; /** * Checks if the user is logged in. * @return True if the user is logged in. */ public boolean isLoggedIn() { return getEngine().getUser() != null; } public String getCurrentUser() { return getEngine().getUser().getName(); } /** * @return True if the user is logged in and is an administrator. */ public boolean isAdministrator() { String adminGroup = getEngine().getAdministratorGroup(); return getEngine().getUser().isInGroup(adminGroup); } /** * Gets a reference to the home page. * @return Home page reference. */ public AlbumPageRef getHome() { return new AlbumPageRef(1, 0, "/", true); } public void editProfile(IRequestCycle aCycle) { aCycle.activate(EditProfilePage.PAGE_NAME); } /** * Logout the user. * @param aCycle Request cycle. */ public void logout(IRequestCycle aCycle) { LOG.info("User '" + Subject.getSubject(AccessController.getContext()) + "' logging out"); // An alternative is to restart the engine but this solution is fragile since Tapestry itself // tries to access the session after that in some case and this leads to an error since // tapestry already invalidated the session. Therefore, we just redirect to a JSP which // invalidates the session and forwards back to the home page of the application. // In this way, the logout functionality completely bypasses Tapestry. throw new RedirectException(LOGOUT_URL); } public void viewAdminPage(IRequestCycle aCycle) { aCycle.activate(AdminPage.PAGE_NAME); } private PhotosEngine getEngine() { return (PhotosEngine)getPage().getEngine(); } }