]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/admin/support/SupportRevokeCertificatesForm.java
e4bedb34e819f7686a63fe53e72f884c8cd3df86
[gigi.git] / src / org / cacert / gigi / pages / admin / support / SupportRevokeCertificatesForm.java
1 package org.cacert.gigi.pages.admin.support;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.security.GeneralSecurityException;
6 import java.util.Date;
7 import java.util.Map;
8
9 import javax.servlet.http.HttpServletRequest;
10
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.dbObjects.Certificate;
13 import org.cacert.gigi.dbObjects.CertificateProfile;
14 import org.cacert.gigi.dbObjects.SupportedUser;
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.output.template.Template;
19
20 public class SupportRevokeCertificatesForm extends Form {
21
22     private static Template t;
23
24     private SupportedUser user;
25     static {
26         t = new Template(SupportRevokeCertificatesForm.class.getResource("SupportRevokeCertificatesForm.templ"));
27     }
28
29     public SupportRevokeCertificatesForm(HttpServletRequest hsr, SupportedUser user) {
30         super(hsr);
31         this.user = user;
32     }
33
34     @Override
35     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
36         if (user.getTicket() != null) {
37             user.revokeAllCertificates();
38             return true;
39         }
40         return false;
41     }
42
43     @Override
44     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
45         final Certificate[] certs = user.getCertificates(true);
46         final CertificateProfile[] profiles = CertificateProfile.getAll();
47         vars.put("types", new IterableDataset() {
48
49             int typeIndex = 0;
50
51             @Override
52             public boolean next(Language l, Map<String, Object> vars) {
53                 if (typeIndex > profiles.length - 1) {
54                     return false;
55                 }
56                 int valid = 0;
57                 int total = 0;
58                 long lastExpire = Long.MIN_VALUE;
59                 for (int i = 0; i < certs.length; i++) {
60                     try {
61                         if (certs[i].getProfile().getId() != profiles[typeIndex].getId()) {
62                             continue;
63                         }
64                         total++;
65                         certs[i].cert().checkValidity();
66                         lastExpire = Math.max(lastExpire, certs[i].cert().getNotAfter().getTime());
67                         valid++;
68                     } catch (GeneralSecurityException | IOException e) {
69                         continue;
70                     }
71                 }
72                 vars.put("total", total);
73                 vars.put("profile", profiles[typeIndex].getVisibleName());
74                 vars.put("valid", valid);
75                 vars.put("exp", total - valid);
76                 vars.put("rev", "TODO");
77                 if (lastExpire == Long.MIN_VALUE) {
78                     vars.put("lastdate", "-");
79                 } else {
80                     vars.put("lastdate", new Date(lastExpire));
81                 }
82                 typeIndex++;
83                 return true;
84             }
85         });
86         t.output(out, l, vars);
87     }
88
89 }