1 package org.cacert.gigi.dbObjects;
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Locale;
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;
17 public class SupportedUser {
21 private User supporter;
23 private String ticket;
25 public SupportedUser(User target, User supporter, String ticket) {
26 this.supporter = supporter;
31 public boolean setDob(DayDate dob) throws GigiApiException {
32 if (dob.equals(target.getDoB())) {
35 writeSELog("SE dob change");
36 target.setDoBAsSupport(dob);
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) {
51 public void revokeCertificate(Certificate cert) throws GigiApiException {
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);
68 private void writeSELog(String type) throws GigiApiException {
70 throw new GigiApiException("No ticket set!");
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);
82 return target.getId();
85 public Certificate[] getCertificates(boolean includeRevoked) {
86 return target.getCertificates(includeRevoked);
89 public String getTicket() {
93 public User getTargetUser() {
97 public void grant(Group toMod) throws GigiApiException {
98 target.grantGroup(supporter, toMod);
101 public void revoke(Group toMod) {
102 target.revokeGroup(supporter, toMod);
105 private static final MailTemplate supportNotification = new MailTemplate(SupportedUser.class.getResource("SupportNotificationMail.templ"));
107 public void sendSupportNotification(String subject, Outputable message) {
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);
115 String supportemailaddress = ServerConstants.getSupportMailAddress();
116 supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, supportemailaddress);
117 } catch (IOException e) {
122 private static final MailTemplate supportUserNotification = new MailTemplate(SupportedUser.class.getResource("SupportUserNotificationMail.templ"));
124 public void sendSupportUserNotification(String subject, Outputable message) {
126 HashMap<String, Object> vars = new HashMap<>();
127 vars.put("action", message);
128 vars.put("ticket", this.getTicket());
129 vars.put("subject", subject);
131 supportUserNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, target.getEmail());
132 } catch (IOException e) {