001 /*
002 * Databinder: a simple bridge from Wicket to Hibernate
003 * Copyright (C) 2006 Nathan Hamblen nathan@technically.us
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either
008 * version 2.1 of the License, or (at your option) any later version.
009 *
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public
016 * License along with this library; if not, write to the Free Software
017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
018 */
019 package net.databinder.auth.components;
020
021 import net.databinder.auth.AuthApplication;
022 import net.databinder.auth.AuthSession;
023 import net.databinder.auth.components.DataSignInPageBase.ReturnPage;
024 import net.databinder.auth.data.DataUser;
025
026 import org.apache.wicket.Page;
027 import org.apache.wicket.authorization.strategies.role.Roles;
028 import org.apache.wicket.markup.html.WebMarkupContainer;
029 import org.apache.wicket.markup.html.WebPage;
030 import org.apache.wicket.markup.html.basic.Label;
031 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
032 import org.apache.wicket.markup.html.link.Link;
033 import org.apache.wicket.markup.html.panel.Panel;
034 import org.apache.wicket.model.AbstractReadOnlyModel;
035
036 /**
037 * Displays sign in and out links, as well as current user if any.
038 * Replaceable String resources: <pre>
039 * data.auth.status.account
040 * data.auth.status.admin
041 * data.auth.status.sign_out
042 * data.auth.status.sign_in</pre>
043 */
044 public abstract class DataUserStatusPanelBase extends Panel {
045 /**
046 * Constructs sign in and out links.
047 * @param id Wicket id
048 */
049 public DataUserStatusPanelBase(String id) {
050 super(id);
051
052 WebMarkupContainer wrapper = new WebMarkupContainer("signedInWrapper") {
053 public boolean isVisible() {
054 return getAuthSession().isSignedIn();
055 }
056 };
057 add(wrapper);
058 wrapper.add(new Label("username", new AbstractReadOnlyModel() {
059 @Override
060 public Object getObject() {
061 return getAuthSession().getUser().getUsername();
062 }
063 }));
064 wrapper.add(new Link("profile") {
065 @Override
066 public void onClick() {
067 setResponsePage(profilePage(new DataSignInPageBase.ReturnPage() {
068 public Page get() {
069 return DataUserStatusPanelBase.this.getPage();
070 }
071 }));
072 }
073 });
074
075 wrapper.add(new BookmarkablePageLink("admin", adminPageClass()) {
076 @Override
077 public boolean isEnabled() {
078 return !adminPageClass().isInstance(getPage());
079 }
080 @Override
081 public boolean isVisible() {
082 DataUser user = ((AuthSession) getSession()).getUser();
083 return user != null && user.hasRole(Roles.ADMIN);
084 }
085 });
086
087 wrapper.add(new Link("signOut") {
088 @Override
089 public void onClick() {
090 getAuthSession().signOut();
091 setResponsePage(getApplication().getHomePage());
092 }
093 });
094
095 add(getSignInLink("signIn"));
096 }
097
098 /**
099 * @param returnPage current page, to be returned to after profile update
100 * @return new page instance for user profile
101 */
102 protected abstract WebPage profilePage(ReturnPage returnPage);
103
104 /** @return page class for user administration */
105 protected abstract Class<? extends WebPage> adminPageClass();
106
107 /**
108 * Returns link to sign-in page from <tt>AuthDataApplication</tt> subclass. Uses redirect
109 * to intercept page so that user will return to current page once signed in. Override
110 * for other behavior.
111 */
112 protected Link getSignInLink(String id) {
113 return new Link(id) {
114 @Override
115 public void onClick() {
116 redirectToInterceptPage(getPageFactory().newPage(
117 ((AuthApplication)getApplication()).getSignInPageClass()));
118 }
119 @Override
120 public boolean isVisible() {
121 return !getAuthSession().isSignedIn();
122 }
123 };
124 }
125
126 /** @return casted web session*/
127 protected AuthSession getAuthSession() {
128 return (AuthSession) getSession();
129 }
130 }