]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/admin/support/SupportUserDetailsPage.java
UPD: Restrict admin pages by "suuporter" group
[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.security.GeneralSecurityException;
5 import java.util.Date;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import org.cacert.gigi.dbObjects.Certificate;
13 import org.cacert.gigi.dbObjects.CertificateProfile;
14 import org.cacert.gigi.dbObjects.EmailAddress;
15 import org.cacert.gigi.dbObjects.Group;
16 import org.cacert.gigi.dbObjects.User;
17 import org.cacert.gigi.localisation.Language;
18 import org.cacert.gigi.output.DateSelector;
19 import org.cacert.gigi.output.template.IterableDataset;
20 import org.cacert.gigi.pages.Page;
21
22 public class SupportUserDetailsPage extends Page {
23
24     public static final String PATH = "/support/user/";
25
26     public SupportUserDetailsPage(String title) {
27         super(title);
28     }
29
30     @Override
31     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
32         int id;
33         String[] idP = req.getPathInfo().split("/");
34         id = Integer.parseInt(idP[idP.length - 1]);
35         final User user = User.getById(id);
36         SupportUserDetailsForm f = new SupportUserDetailsForm(req, user);
37         HashMap<String, Object> vars = new HashMap<String, Object>();
38         vars.put("details", f);
39         final EmailAddress[] addrs = user.getEmails();
40         vars.put("emails", new IterableDataset() {
41
42             int i = 0;
43
44             @Override
45             public boolean next(Language l, Map<String, Object> vars) {
46                 String address = addrs[i].getAddress();
47                 if ( !address.equals(user.getEmail())) {
48                     vars.put("secmail", address);
49                 }
50                 i++;
51                 return i != addrs.length - 1;
52             }
53         });
54         final Certificate[] certs = user.getCertificates(true);
55         final CertificateProfile[] profiles = CertificateProfile.getAll();
56         vars.put("types", new IterableDataset() {
57
58             int typeIndex = 0;
59
60             @Override
61             public boolean next(Language l, Map<String, Object> vars) {
62                 if (typeIndex > profiles.length - 1) {
63                     return false;
64                 }
65                 int valid = 0;
66                 int total = 0;
67                 long lastExpire = Long.MIN_VALUE;
68                 for (int i = 0; i < certs.length; i++) {
69                     try {
70                         if (certs[i].getProfile().getId() != profiles[typeIndex].getId()) {
71                             continue;
72                         }
73                         total++;
74                         certs[i].cert().checkValidity();
75                         lastExpire = Math.max(lastExpire, certs[i].cert().getNotAfter().getTime());
76                         valid++;
77                     } catch (GeneralSecurityException | IOException e) {
78                         continue;
79                     }
80                 }
81                 vars.put("total", total);
82                 vars.put("profile", profiles[typeIndex].getVisibleName());
83                 vars.put("valid", valid);
84                 vars.put("exp", total - valid);
85                 vars.put("rev", "TODO");
86                 if (lastExpire == Long.MIN_VALUE) {
87                     vars.put("lastdate", "-");
88                 } else {
89                     vars.put("lastdate", DateSelector.getDateFormat().format(new Date(lastExpire)));
90                 }
91                 typeIndex++;
92                 return true;
93             }
94         });
95         getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
96     }
97
98     @Override
99     public boolean isPermitted(User u) {
100         if (u == null) {
101             return false;
102         }
103         return u.isInGroup(Group.getByString("supporter"));
104     }
105 }