]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/SupportedUser.java
add: send notification to support and user after support actions
[gigi.git] / src / org / cacert / gigi / dbObjects / SupportedUser.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Locale;
6
7 import org.cacert.gigi.GigiApiException;
8 import org.cacert.gigi.database.GigiPreparedStatement;
9 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
10 import org.cacert.gigi.localisation.Language;
11 import org.cacert.gigi.output.template.MailTemplate;
12 import org.cacert.gigi.output.template.Outputable;
13 import org.cacert.gigi.output.template.SprintfCommand;
14 import org.cacert.gigi.util.DayDate;
15 import org.cacert.gigi.util.ServerConstants;
16
17 public class SupportedUser {
18
19     private User target;
20
21     private User supporter;
22
23     private String ticket;
24
25     public SupportedUser(User target, User supporter, String ticket) {
26         this.supporter = supporter;
27         this.target = target;
28         this.ticket = ticket;
29     }
30
31     public boolean setDob(DayDate dob) throws GigiApiException {
32         if (dob.equals(target.getDoB())) {
33             return false;
34         }
35         writeSELog("SE dob change");
36         target.setDoBAsSupport(dob);
37         return true;
38     }
39
40     public void revokeAllCertificates() throws GigiApiException {
41         writeSELog("SE Revoke certificates");
42         Certificate[] certs = target.getCertificates(false);
43         // TODO Check for open jobs!
44         for (int i = 0; i < certs.length; i++) {
45             if (certs[i].getStatus() == CertificateStatus.ISSUED) {
46                 certs[i].revoke();
47             }
48         }
49     }
50
51     public void revokeCertificate(Certificate cert) throws GigiApiException {
52
53         // TODO Check for open jobs!
54         if (cert.getStatus() == CertificateStatus.ISSUED) {
55             writeSELog("SE Revoke certificate");
56             cert.revoke().waitFor(60000);
57             // send notification to support
58             String subject = "Revoke certificate";
59             Outputable message = SprintfCommand.createSimple("Certificate with serial number {0} for {1} <{2}>, has been revoked.", cert.getSerial(), target.getPreferredName().toString(), target.getEmail());
60             sendSupportNotification(subject, message);
61             // send notification to user
62             subject = "Revoke certificate";
63             message = SprintfCommand.createSimple("Certificate with serial number {0} with subject distinguished name {1} has been revoked.", cert.getSerial(), cert.getDistinguishedName());
64             sendSupportUserNotification(subject, message);
65         }
66     }
67
68     private void writeSELog(String type) throws GigiApiException {
69         if (ticket == null) {
70             throw new GigiApiException("No ticket set!");
71         }
72         try (GigiPreparedStatement prep = new GigiPreparedStatement("INSERT INTO `adminLog` SET uid=?, admin=?, type=?, information=?")) {
73             prep.setInt(1, target.getId());
74             prep.setInt(2, supporter.getId());
75             prep.setString(3, type);
76             prep.setString(4, ticket);
77             prep.executeUpdate();
78         }
79     }
80
81     public int getId() {
82         return target.getId();
83     }
84
85     public Certificate[] getCertificates(boolean includeRevoked) {
86         return target.getCertificates(includeRevoked);
87     }
88
89     public String getTicket() {
90         return ticket;
91     }
92
93     public User getTargetUser() {
94         return target;
95     }
96
97     public void grant(Group toMod) throws GigiApiException {
98         target.grantGroup(supporter, toMod);
99     }
100
101     public void revoke(Group toMod) {
102         target.revokeGroup(supporter, toMod);
103     }
104
105     private static final MailTemplate supportNotification = new MailTemplate(SupportedUser.class.getResource("SupportNotificationMail.templ"));
106
107     public void sendSupportNotification(String subject, Outputable message) {
108         try {
109             HashMap<String, Object> vars = new HashMap<>();
110             vars.put("supporter", supporter.getPreferredName().toString());
111             vars.put("action", message);
112             vars.put("ticket", this.getTicket());
113             vars.put("subject", subject);
114
115             String supportemailaddress = ServerConstants.getSupportMailAddress();
116             supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, supportemailaddress);
117         } catch (IOException e) {
118             e.printStackTrace();
119         }
120     }
121
122     private static final MailTemplate supportUserNotification = new MailTemplate(SupportedUser.class.getResource("SupportUserNotificationMail.templ"));
123
124     public void sendSupportUserNotification(String subject, Outputable message) {
125         try {
126             HashMap<String, Object> vars = new HashMap<>();
127             vars.put("action", message);
128             vars.put("ticket", this.getTicket());
129             vars.put("subject", subject);
130
131             supportUserNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, target.getEmail());
132         } catch (IOException e) {
133             e.printStackTrace();
134         }
135     }
136 }