upd: change the find user routine to search for all email addresses
[gigi.git] / src / org / cacert / gigi / pages / admin / support / FindUserPage.java
1 package org.cacert.gigi.pages.admin.support;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.cacert.gigi.GigiApiException;
11 import org.cacert.gigi.dbObjects.EmailAddress;
12 import org.cacert.gigi.localisation.Language;
13 import org.cacert.gigi.output.template.Form;
14 import org.cacert.gigi.output.template.IterableDataset;
15 import org.cacert.gigi.pages.Page;
16 import org.cacert.gigi.util.AuthorizationContext;
17
18 public class FindUserPage extends Page {
19
20     public static final String PATH = "/support/find/user";
21
22     public FindUserPage() {
23         super("Find User");
24     }
25
26     @Override
27     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
28         HashMap<String, Object> vars = new HashMap<String, Object>();
29         vars.put("first", true);
30         new FindUserForm(req).output(resp.getWriter(), Page.getLanguage(req), vars);
31     }
32
33     @Override
34     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
35         FindUserForm form = Form.getForm(req, FindUserForm.class);
36         try {
37             form.submit(resp.getWriter(), req);
38             final EmailAddress[] emails = form.getEmails();
39             if (emails.length == 1) {
40                 resp.sendRedirect(SupportUserDetailsPage.PATH + emails[0].getOwner().getId());
41             } else {
42                 HashMap<String, Object> vars = new HashMap<String, Object>();
43                 vars.put("first", false);
44                 vars.put("usertable", new IterableDataset() {
45
46                     int i = 0;
47
48                     @Override
49                     public boolean next(Language l, Map<String, Object> vars) {
50                         if (i == emails.length) {
51                             return false;
52                         }
53                         vars.put("usrid", emails[i].getOwner().getId());
54                         vars.put("usermail", emails[i].getAddress());
55                         i++;
56                         return true;
57                     }
58                 });
59                 form.output(resp.getWriter(), getLanguage(req), vars);
60             }
61         } catch (GigiApiException e) {
62             e.format(resp.getWriter(), Page.getLanguage(req));
63             doGet(req, resp);
64         }
65     }
66
67     @Override
68     public boolean isPermitted(AuthorizationContext ac) {
69         return ac != null && ac.canSupport();
70     }
71
72 }