]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/admin/support/SupportRevokeCertificatesForm.java
add: send mail notification to support after support action
[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.Outputable;
20 import org.cacert.gigi.output.template.Template;
21 import org.cacert.gigi.output.template.TranslateCommand;
22
23 public class SupportRevokeCertificatesForm extends Form {
24
25     private static final Template t = new Template(SupportRevokeCertificatesForm.class.getResource("SupportRevokeCertificatesForm.templ"));
26
27     private SupportedUser user;
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             String subject = "Revoke certificates";
39             Outputable message = new TranslateCommand("All certificates in the account have been revoked.");
40             user.sendSupportNotification(subject, message);
41             return true;
42         }
43         return false;
44     }
45
46     @Override
47     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
48         final Certificate[] certs = user.getCertificates(true);
49         final CertificateProfile[] profiles = CertificateProfile.getAll();
50         vars.put("types", new IterableDataset() {
51
52             int typeIndex = 0;
53
54             @Override
55             public boolean next(Language l, Map<String, Object> vars) {
56                 if (typeIndex > profiles.length - 1) {
57                     return false;
58                 }
59                 int valid = 0;
60                 int total = 0;
61                 int revoked = 0;
62                 long lastExpire = Long.MIN_VALUE;
63                 for (int i = 0; i < certs.length; i++) {
64                     try {
65                         if (certs[i].getProfile().getId() != profiles[typeIndex].getId()) {
66                             continue;
67                         }
68                         total++;
69                         if (certs[i].getStatus() == CertificateStatus.DRAFT) {
70                             continue;
71                         }
72                         if (certs[i].getStatus() == CertificateStatus.REVOKED) {
73                             revoked++;
74                             continue;
75                         }
76                         certs[i].cert().checkValidity();
77                         lastExpire = Math.max(lastExpire, certs[i].cert().getNotAfter().getTime());
78                         valid++;
79                     } catch (GeneralSecurityException | IOException e) {
80                         continue;
81                     }
82                 }
83                 vars.put("total", total);
84                 vars.put("profile", profiles[typeIndex].getVisibleName());
85                 vars.put("valid", valid);
86                 vars.put("exp", total - valid);
87                 vars.put("rev", revoked);
88                 if (lastExpire == Long.MIN_VALUE) {
89                     vars.put("lastdate", "-");
90                 } else {
91                     vars.put("lastdate", new Date(lastExpire));
92                 }
93                 typeIndex++;
94                 return true;
95             }
96         });
97         t.output(out, l, vars);
98     }
99
100 }