]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/account/mail/MailOverview.java
upd: enforce a more strict Form call pattern.
[gigi.git] / src / org / cacert / gigi / pages / account / mail / MailOverview.java
1 package org.cacert.gigi.pages.account.mail;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
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.Form.CSRFException;
13 import org.cacert.gigi.pages.ManagedMultiFormPage;
14 import org.cacert.gigi.pages.Page;
15 import org.cacert.gigi.util.AuthorizationContext;
16
17 public class MailOverview extends ManagedMultiFormPage {
18
19     public static final String DEFAULT_PATH = "/account/mails";
20
21     public MailOverview() {
22         super("Email addresses");
23     }
24
25     @Override
26     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
27         User user = getUser(req);
28         output(req, resp, new MailAddForm(req, user), new MailManagementForm(req, user));
29     }
30
31     private void output(HttpServletRequest req, HttpServletResponse resp, MailAddForm addForm, MailManagementForm mgmtForm) throws IOException {
32         Language lang = Page.getLanguage(req);
33         HashMap<String, Object> vars = new HashMap<>();
34         vars.put("addForm", addForm);
35         vars.put("manForm", mgmtForm);
36         getDefaultTemplate().output(resp.getWriter(), lang, vars);
37     }
38
39     @Override
40     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
41         Form current = getForm(req);
42         if (Form.printFormErrors(req, resp.getWriter())) {
43             User user = getUser(req);
44             if (current instanceof MailAddForm) {
45                 output(req, resp, (MailAddForm) current, new MailManagementForm(req, user));
46             } else {
47                 output(req, resp, new MailAddForm(req, user), (MailManagementForm) current);
48             }
49         }
50     }
51
52     @Override
53     public Form getForm(HttpServletRequest req) throws CSRFException {
54         if (req.getParameter("addmail") != null) {
55             return Form.getForm(req, MailAddForm.class);
56         } else {
57             return Form.getForm(req, MailManagementForm.class);
58         }
59     }
60
61     @Override
62     public boolean isPermitted(AuthorizationContext ac) {
63         return ac != null && ac.getTarget() instanceof User;
64     }
65
66 }