View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.whatsitcalled.webflange.webapp;
18  
19  import org.apache.wicket.Application;
20  import org.apache.wicket.markup.html.form.CheckBox;
21  import org.apache.wicket.markup.html.form.FormComponent;
22  import org.apache.wicket.markup.html.form.PasswordTextField;
23  import org.apache.wicket.markup.html.form.StatelessForm;
24  import org.apache.wicket.markup.html.form.TextField;
25  import org.apache.wicket.markup.html.panel.Panel;
26  import org.apache.wicket.model.CompoundPropertyModel;
27  import org.apache.wicket.model.PropertyModel;
28  import org.apache.wicket.util.value.ValueMap;
29  
30  /**
31   * Panel for user authentication.
32   * 
33   * @author marrink
34   */
35  public abstract class UsernamePasswordSignInPanel extends Panel
36  {
37  	/**
38  	 * Constructor.
39  	 * 
40  	 * @param id
41  	 *            component id
42  	 */
43  	public UsernamePasswordSignInPanel(final String id)
44  	{
45  		super(id);
46  		add(new SignInForm("signInForm").setOutputMarkupId(false));
47  	}
48  
49  	/**
50  	 * The actual login process.
51  	 * 
52  	 * @param username
53  	 * @param password
54  	 * @return true, if the login was successful, false otherwise
55  	 */
56  	public abstract boolean signIn(String username, String password);
57  
58  	/**
59  	 * Sign in form.
60  	 */
61  	public final class SignInForm extends StatelessForm
62  	{
63  		private static final long serialVersionUID = 1L;
64  
65  		/**
66  		 * remember username
67  		 */
68  		private boolean rememberMe = true;
69  
70  		/**
71  		 * Constructor.
72  		 * 
73  		 * @param id
74  		 *            id of the form component
75  		 */
76  		public SignInForm(final String id)
77  		{
78  			super(id, new CompoundPropertyModel(new ValueMap()));
79  
80  			// only remember username, not passwords
81  			add(new TextField("username").setPersistent(rememberMe).setOutputMarkupId(false));
82  			add(new PasswordTextField("password").setOutputMarkupId(false));
83  			add(new CheckBox("rememberMe", new PropertyModel(this, "rememberMe")));
84  		}
85  
86  		/**
87  		 * 
88  		 * @see org.apache.wicket.Component#getMarkupId()
89  		 */
90  		public String getMarkupId()
91  		{
92  			// fix javascript id
93  			return getId();
94  		}
95  
96  		/**
97  		 * 
98  		 * @see org.apache.wicket.markup.html.form.Form#onSubmit()
99  		 */
100 		public final void onSubmit()
101 		{
102 			if (!rememberMe)
103 			{
104 				// delete persistent data
105 				getPage().removePersistedFormData(SignInForm.class, true);
106 			}
107 
108 			ValueMap values = (ValueMap)getModelObject();
109 			String username = values.getString("username");
110 			String password = values.getString("password");
111 
112 			if (signIn(username, password))
113 			{
114 				// continue or homepage?
115 				if (!getPage().continueToOriginalDestination())
116 				{
117 					setResponsePage(Application.get().getHomePage());
118 				}
119 			}
120 			else
121 			{
122 				// Try the component based localizer first. If not found try the
123 				// application localizer. Else use the default
124 				error(getLocalizer().getString("exception.login", this,
125 						"Illegal username password combo"));
126 			}
127 		}
128 
129 		/**
130 		 * @return true if formdata should be made persistent (cookie) for later
131 		 *         logins.
132 		 */
133 		public boolean getRememberMe()
134 		{
135 			return rememberMe;
136 		}
137 
138 		/**
139 		 * Remember form values for later logins?.
140 		 * 
141 		 * @param rememberMe
142 		 *            true if formdata should be remembered
143 		 */
144 		public void setRememberMe(boolean rememberMe)
145 		{
146 			this.rememberMe = rememberMe;
147 			((FormComponent)get("username")).setPersistent(rememberMe);
148 		}
149 	}
150 }