]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/account/ChangeForm.java
Implement and test change password form.
[gigi.git] / src / org / cacert / gigi / pages / account / ChangeForm.java
1 package org.cacert.gigi.pages.account;
2
3 import java.io.PrintWriter;
4 import java.util.Map;
5
6 import javax.servlet.http.HttpServletRequest;
7
8 import org.cacert.gigi.GigiApiException;
9 import org.cacert.gigi.Language;
10 import org.cacert.gigi.User;
11 import org.cacert.gigi.output.Form;
12 import org.cacert.gigi.output.template.Template;
13 import org.cacert.gigi.pages.Page;
14
15 public class ChangeForm extends Form {
16         User target;
17
18         public ChangeForm(HttpServletRequest hsr, User target) {
19                 super(hsr);
20                 this.target = target;
21         }
22
23         private static Template t;
24         static {
25                 t = new Template(ChangePasswordPage.class.getResource("ChangePasswordForm.templ"));
26         }
27
28         @Override
29         public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
30                 t.output(out, l, vars);
31         }
32
33         @Override
34         public boolean submit(PrintWriter out, HttpServletRequest req) {
35                 String oldpassword = req.getParameter("oldpassword");
36                 String p1 = req.getParameter("pword1");
37                 String p2 = req.getParameter("pword2");
38                 GigiApiException error = new GigiApiException();
39                 if (oldpassword == null || p1 == null || p2 == null) {
40                         new GigiApiException("All fields are required.").format(out, Page.getLanguage(req));
41                         return false;
42                 }
43                 if (!p1.equals(p2)) {
44                         new GigiApiException("New passwords do not match.").format(out, Page.getLanguage(req));
45                         return false;
46                 }
47                 try {
48                         target.changePassword(oldpassword, p1);
49                 } catch (GigiApiException e) {
50                         error.mergeInto(e);
51                 }
52                 if (!error.isEmpty()) {
53                         error.format(out, Page.getLanguage(req));
54                         return false;
55                 }
56                 return true;
57         }
58
59 }