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