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