001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.authentication.panel;
018:
019: import org.apache.wicket.PageParameters;
020: import org.apache.wicket.authentication.AuthenticatedWebSession;
021: import org.apache.wicket.markup.html.WebMarkupContainer;
022: import org.apache.wicket.markup.html.form.CheckBox;
023: import org.apache.wicket.markup.html.form.Form;
024: import org.apache.wicket.markup.html.form.PasswordTextField;
025: import org.apache.wicket.markup.html.form.TextField;
026: import org.apache.wicket.markup.html.panel.FeedbackPanel;
027: import org.apache.wicket.markup.html.panel.Panel;
028: import org.apache.wicket.model.PropertyModel;
029: import org.apache.wicket.util.value.ValueMap;
030:
031: /**
032: * Reusable user sign in panel with username and password as well as support for
033: * cookie persistence of the both. When the SignInPanel's form is submitted, the
034: * method signIn(String, String) is called, passing the username and password
035: * submitted. The signIn() method should authenticate the user's session. The
036: * default implementation calls AuthenticatedWebSession.get().signIn().
037: *
038: * @author Jonathan Locke
039: * @author Juergen Donnerstag
040: * @author Eelco Hillenius
041: */
042: public class SignInPanel extends Panel {
043: private static final long serialVersionUID = 1L;
044:
045: /** True if the panel should display a remember-me checkbox */
046: private boolean includeRememberMe = true;
047:
048: /** Field for password. */
049: private PasswordTextField password;
050:
051: /** True if the user should be remembered via form persistence (cookies) */
052: private boolean rememberMe = true;
053:
054: /** Field for user name. */
055: private TextField username;
056:
057: /**
058: * Sign in form.
059: */
060: public final class SignInForm extends Form {
061: private static final long serialVersionUID = 1L;
062:
063: /** El-cheapo model for form. */
064: private final ValueMap properties = new ValueMap();
065:
066: /**
067: * Constructor.
068: *
069: * @param id
070: * id of the form component
071: */
072: public SignInForm(final String id) {
073: super (id);
074:
075: // Attach textfield components that edit properties map
076: // in lieu of a formal beans model
077: add(username = new TextField("username", new PropertyModel(
078: properties, "username")));
079: add(password = new PasswordTextField("password",
080: new PropertyModel(properties, "password")));
081:
082: // MarkupContainer row for remember me checkbox
083: final WebMarkupContainer rememberMeRow = new WebMarkupContainer(
084: "rememberMeRow");
085: add(rememberMeRow);
086:
087: // Add rememberMe checkbox
088: rememberMeRow.add(new CheckBox("rememberMe",
089: new PropertyModel(SignInPanel.this , "rememberMe")));
090:
091: // Make form values persistent
092: setPersistent(rememberMe);
093:
094: // Show remember me checkbox?
095: rememberMeRow.setVisible(includeRememberMe);
096: }
097:
098: /**
099: * @see org.apache.wicket.markup.html.form.Form#onSubmit()
100: */
101: public final void onSubmit() {
102: if (signIn(getUsername(), getPassword())) {
103: onSignInSucceeded();
104: } else {
105: onSignInFailed();
106: }
107: }
108: }
109:
110: /**
111: * @see org.apache.wicket.Component#Component(String)
112: */
113: public SignInPanel(final String id) {
114: this (id, true);
115: }
116:
117: /**
118: * @param id
119: * See Component constructor
120: * @param includeRememberMe
121: * True if form should include a remember-me checkbox
122: * @see org.apache.wicket.Component#Component(String)
123: */
124: public SignInPanel(final String id, final boolean includeRememberMe) {
125: super (id);
126:
127: this .includeRememberMe = includeRememberMe;
128:
129: // Create feedback panel and add to page
130: final FeedbackPanel feedback = new FeedbackPanel("feedback");
131: add(feedback);
132:
133: // Add sign-in form to page, passing feedback panel as
134: // validation error handler
135: add(new SignInForm("signInForm"));
136: }
137:
138: /**
139: * Removes persisted form data for the signin panel (forget me)
140: */
141: public final void forgetMe() {
142: // Remove persisted user data. Search for child component
143: // of type SignInForm and remove its related persistence values.
144: getPage().removePersistedFormData(SignInPanel.SignInForm.class,
145: true);
146: }
147:
148: /**
149: * Convenience method to access the password.
150: *
151: * @return The password
152: */
153: public String getPassword() {
154: return password.getInput();
155: }
156:
157: /**
158: * Get model object of the rememberMe checkbox
159: *
160: * @return True if user should be remembered in the future
161: */
162: public boolean getRememberMe() {
163: return rememberMe;
164: }
165:
166: /**
167: * Convenience method to access the username.
168: *
169: * @return The user name
170: */
171: public String getUsername() {
172: return username.getModelObjectAsString();
173: }
174:
175: /**
176: * Convenience method set persistence for username and password.
177: *
178: * @param enable
179: * Whether the fields should be persistent
180: */
181: public void setPersistent(final boolean enable) {
182: username.setPersistent(enable);
183: password.setPersistent(enable);
184: }
185:
186: /**
187: * Set model object for rememberMe checkbox
188: *
189: * @param rememberMe
190: */
191: public void setRememberMe(final boolean rememberMe) {
192: this .rememberMe = rememberMe;
193: this .setPersistent(rememberMe);
194: }
195:
196: /**
197: * Sign in user if possible.
198: *
199: * @param username
200: * The username
201: * @param password
202: * The password
203: * @return True if signin was successful
204: */
205: public boolean signIn(String username, String password) {
206: return AuthenticatedWebSession.get().signIn(username, password);
207: }
208:
209: protected void onSignInFailed() {
210: // Try the component based localizer first. If not found try the
211: // application localizer. Else use the default
212: error(getLocalizer().getString("signInFailed", this ,
213: "Sign in failed"));
214: }
215:
216: protected void onSignInSucceeded() {
217: // If login has been called because the user was not yet
218: // logged in, than continue to the original destination,
219: // otherwise to the Home page
220: if (!continueToOriginalDestination()) {
221: setResponsePage(getApplication().getSessionSettings()
222: .getPageFactory().newPage(
223: getApplication().getHomePage(),
224: (PageParameters) null));
225: }
226: }
227:
228: }
|