Added edit profile page.
[photos] / src / main / java / org / wamblee / photos / wicket / EditProfilePage.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.photos.wicket;
17
18 import java.util.logging.Logger;
19 import javax.inject.Inject;
20
21 import org.apache.wicket.PageParameters;
22 import org.apache.wicket.markup.html.form.Form;
23 import org.apache.wicket.markup.html.form.PasswordTextField;
24 import org.apache.wicket.model.Model;
25 import org.wamblee.security.authentication.User;
26 import org.wamblee.security.authentication.UserAdministration;
27
28 /**
29  * Homepage
30  */
31 public class EditProfilePage extends BasePage {
32
33     private static final Logger LOGGER = Logger.getLogger(EditProfilePage.class.getName());
34
35     private static final long serialVersionUID = 1L;
36
37     @Inject
38     private User user;
39
40     @Inject
41     private UserAdministration userAdmin;
42
43     private PasswordTextField _currentPassword;
44     private PasswordTextField _password1;
45     private PasswordTextField _password2;
46
47     /**
48      * Constructor that is invoked when page is invoked without a session.
49      *
50      * @param parameters Page parameters
51      */
52     public EditProfilePage(final PageParameters parameters) throws Exception {
53         super();
54
55         Form form = new Form("changePasswordForm") {
56             @Override
57             protected void onSubmit() {
58                 String current = _currentPassword.getValue();
59                 String pw1 = _password1.getValue();
60                 String pw2 = _password2.getValue();
61                 if (!userAdmin.checkPassword(user.getName(), current)) {
62                     error("password invalid");
63                     return;
64                 }
65                 if (!pw1.equals(pw2)) {
66                     error("Entered passwords differ");
67                     return;
68                 }
69                 if (userAdmin.changePassword(user.getName(), current, pw1)) {
70                     info("Password changed successfully");
71                     setResponsePage(HomePage.class);
72                 }
73                 error("Could not change password");
74             }
75         };
76         add(form);
77         _currentPassword = new PasswordTextField("currentPassword", new Model(""));
78         _password1 = new PasswordTextField("password1", new Model(""));
79         _password2 = new PasswordTextField("password2", new Model(""));
80         form.add(_currentPassword);
81         form.add(_password1);
82         form.add(_password2);
83     }
84 }