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