]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/admin/support/SupportUserDetailsPage.java
04898f8ca20be6777c2ace5aff6dafcc6c66e15a
[gigi.git] / src / org / cacert / gigi / pages / admin / support / SupportUserDetailsPage.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.Domain;
12 import org.cacert.gigi.dbObjects.EmailAddress;
13 import org.cacert.gigi.dbObjects.SupportedUser;
14 import org.cacert.gigi.dbObjects.User;
15 import org.cacert.gigi.localisation.Language;
16 import org.cacert.gigi.output.template.Form;
17 import org.cacert.gigi.output.template.IterableDataset;
18 import org.cacert.gigi.pages.LoginPage;
19 import org.cacert.gigi.pages.Page;
20 import org.cacert.gigi.util.AuthorizationContext;
21
22 public class SupportUserDetailsPage extends Page {
23
24     public static final String PATH = "/support/user/";
25
26     public SupportUserDetailsPage() {
27         super("Support: User Details");
28     }
29
30     @Override
31     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
32         int id = -1;
33         if ( !req.getPathInfo().endsWith("/")) {
34             resp.sendError(404);
35         }
36         String[] idP = req.getPathInfo().split("/");
37         try {
38             id = Integer.parseInt(idP[idP.length - 1]);
39         } catch (NumberFormatException e) {
40             resp.sendError(404);
41         }
42         final User user = User.getById(id);
43         SupportedUser targetUser = new SupportedUser(user, getUser(req), LoginPage.getAuthorizationContext(req).getSupporterTicketId());
44         SupportUserDetailsForm f = new SupportUserDetailsForm(req, targetUser);
45         HashMap<String, Object> vars = new HashMap<String, Object>();
46         vars.put("details", f);
47         final EmailAddress[] addrs = user.getEmails();
48         vars.put("emails", new IterableDataset() {
49
50             int i = 0;
51
52             @Override
53             public boolean next(Language l, Map<String, Object> vars) {
54                 for (; i < addrs.length;) {
55                     EmailAddress secAddress = addrs[i++];
56                     String address = secAddress.getAddress();
57                     if ( !address.equals(user.getEmail())) {
58                         vars.put("secmail", address);
59                         vars.put("status", l.getTranslation(secAddress.isVerified() ? "verified" : "not verified"));
60                         return true;
61                     }
62                 }
63                 return false;
64             }
65         });
66
67         final Domain[] doms = user.getDomains();
68         vars.put("domains", new IterableDataset() {
69
70             private int point = 0;
71
72             @Override
73             public boolean next(Language l, Map<String, Object> vars) {
74                 if (point >= doms.length) {
75                     return false;
76                 }
77                 Domain domain = doms[point];
78                 vars.put("domain", domain.getSuffix());
79                 vars.put("status", l.getTranslation(domain.isVerified() ? "verified" : "not verified"));
80                 point++;
81                 return true;
82             }
83         });
84
85         vars.put("certifrevoke", new SupportRevokeCertificatesForm(req, targetUser));
86         getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
87     }
88
89     @Override
90     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
91         try {
92             if (req.getParameter("revokeall") != null) {
93                 if ( !Form.getForm(req, SupportRevokeCertificatesForm.class).submit(resp.getWriter(), req)) {
94                     throw new GigiApiException("No ticket number set.");
95                 }
96             } else if (req.getParameter("detailupdate") != null || req.getParameter("resetPass") != null || req.getParameter("removeGroup") != null || req.getParameter("addGroup") != null) {
97                 if ( !Form.getForm(req, SupportUserDetailsForm.class).submit(resp.getWriter(), req)) {
98                     throw new GigiApiException("No ticket number set.");
99                 }
100             }
101         } catch (GigiApiException e) {
102             e.printStackTrace();
103             e.format(resp.getWriter(), getLanguage(req));
104         }
105         super.doPost(req, resp);
106     }
107
108     @Override
109     public boolean isPermitted(AuthorizationContext ac) {
110         return ac != null && ac.canSupport();
111     }
112 }