]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/account/mail/MailOverview.java
upd: use a more strict pattern for handling forms
[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.GigiApiException;
10 import org.cacert.gigi.dbObjects.User;
11 import org.cacert.gigi.localisation.Language;
12 import org.cacert.gigi.output.template.Form;
13 import org.cacert.gigi.pages.Page;
14 import org.cacert.gigi.util.AuthorizationContext;
15
16 public class MailOverview extends Page {
17
18     public static final String DEFAULT_PATH = "/account/mails";
19
20     public MailOverview() {
21         super("Email addresses");
22     }
23
24     @Override
25     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
26         final User us = getUser(req);
27         Language lang = Page.getLanguage(req);
28         HashMap<String, Object> vars = new HashMap<>();
29         vars.put("addForm", new MailAddForm(req, us));
30         vars.put("manForm", new MailManagementForm(req, us));
31         getDefaultTemplate().output(resp.getWriter(), lang, vars);
32     }
33
34     @Override
35     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
36         try {
37             if (req.getParameter("addmail") != null) {
38                 MailAddForm f = Form.getForm(req, MailAddForm.class);
39                 if (f.submit(req)) {
40                     resp.sendRedirect(MailOverview.DEFAULT_PATH);
41                 }
42             } else {
43                 MailManagementForm f = Form.getForm(req, MailManagementForm.class);
44                 if (f.submit(req)) {
45                     resp.sendRedirect(MailOverview.DEFAULT_PATH);
46                 }
47             }
48         } catch (GigiApiException e) {
49             e.format(resp.getWriter(), getLanguage(req));
50         }
51         super.doPost(req, resp);
52     }
53
54     @Override
55     public boolean isPermitted(AuthorizationContext ac) {
56         return ac != null && ac.getTarget() instanceof User;
57     }
58 }