]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/account/ChangeForm.java
upd: use a more strict pattern for handling forms
[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.dbObjects.User;
10 import org.cacert.gigi.localisation.Language;
11 import org.cacert.gigi.output.template.Form;
12 import org.cacert.gigi.output.template.Template;
13
14 public class ChangeForm extends Form {
15
16     private User target;
17
18     public ChangeForm(HttpServletRequest hsr, User target) {
19         super(hsr);
20         this.target = target;
21     }
22
23     private static final Template t = new Template(ChangePasswordPage.class.getResource("ChangePasswordForm.templ"));
24
25     @Override
26     public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
27         t.output(out, l, vars);
28     }
29
30     @Override
31     public boolean submit(HttpServletRequest req) throws GigiApiException {
32         String oldpassword = req.getParameter("oldpassword");
33         String p1 = req.getParameter("pword1");
34         String p2 = req.getParameter("pword2");
35         GigiApiException error = new GigiApiException();
36         if (oldpassword == null || p1 == null || p2 == null) {
37             throw new GigiApiException("All fields are required.");
38         }
39         if ( !p1.equals(p2)) {
40             throw new GigiApiException("New passwords do not match.");
41         }
42         try {
43             target.changePassword(oldpassword, p1);
44         } catch (GigiApiException e) {
45             error.mergeInto(e);
46         }
47         if ( !error.isEmpty()) {
48             throw error;
49         }
50         return true;
51     }
52
53 }