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