]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/account/ChangeForm.java
upd: rename package name and all references to it
[gigi.git] / src / club / wpia / gigi / pages / account / ChangeForm.java
1 package club.wpia.gigi.pages.account;
2
3 import java.io.PrintWriter;
4 import java.util.Map;
5
6 import javax.servlet.http.HttpServletRequest;
7
8 import club.wpia.gigi.GigiApiException;
9 import club.wpia.gigi.dbObjects.User;
10 import club.wpia.gigi.localisation.Language;
11 import club.wpia.gigi.output.template.Form;
12 import club.wpia.gigi.output.template.Template;
13 import club.wpia.gigi.output.template.TranslateCommand;
14
15 public class ChangeForm extends Form {
16
17     private User target;
18
19     public ChangeForm(HttpServletRequest hsr, User target) {
20         super(hsr);
21         this.target = target;
22     }
23
24     private static final Template t = new Template(ChangePasswordPage.class.getResource("ChangePasswordForm.templ"));
25
26     @Override
27     public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
28         t.output(out, l, vars);
29     }
30
31     @Override
32     public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
33         String oldpassword = req.getParameter("oldpassword");
34         String p1 = req.getParameter("pword1");
35         String p2 = req.getParameter("pword2");
36         GigiApiException error = new GigiApiException();
37         if (oldpassword == null || p1 == null || p2 == null) {
38             throw new GigiApiException("All fields are required.");
39         }
40         if ( !p1.equals(p2)) {
41             throw new GigiApiException("New passwords do not match.");
42         }
43         try {
44             target.changePassword(oldpassword, p1);
45         } catch (GigiApiException e) {
46             error.mergeInto(e);
47         }
48         if ( !error.isEmpty()) {
49             throw error;
50         }
51         return new SuccessMessageResult(new TranslateCommand("Password changed."));
52     }
53
54 }