1 package org.cacert.gigi.dbObjects;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.io.StringWriter;
6 import java.util.HashMap;
7 import java.util.Locale;
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.database.GigiPreparedStatement;
11 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
12 import org.cacert.gigi.email.SendMail;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.output.template.Outputable;
15 import org.cacert.gigi.output.template.SprintfCommand;
16 import org.cacert.gigi.util.DayDate;
17 import org.cacert.gigi.util.ServerConstants;
19 public class SupportedUser {
23 private User supporter;
25 private String ticket;
27 public SupportedUser(User target, User supporter, String ticket) {
28 this.supporter = supporter;
33 public boolean setDob(DayDate dob) throws GigiApiException {
34 if (dob.equals(target.getDoB())) {
37 writeSELog("SE dob change");
38 target.setDoBAsSupport(dob);
42 public void revokeAllCertificates() throws GigiApiException {
43 writeSELog("SE Revoke certificates");
44 Certificate[] certs = target.getCertificates(false);
45 // TODO Check for open jobs!
46 for (int i = 0; i < certs.length; i++) {
47 if (certs[i].getStatus() == CertificateStatus.ISSUED) {
53 private void writeSELog(String type) throws GigiApiException {
55 throw new GigiApiException("No ticket set!");
57 try (GigiPreparedStatement prep = new GigiPreparedStatement("INSERT INTO `adminLog` SET uid=?, admin=?, type=?, information=?")) {
58 prep.setInt(1, target.getId());
59 prep.setInt(2, supporter.getId());
60 prep.setString(3, type);
61 prep.setString(4, ticket);
67 return target.getId();
70 public Certificate[] getCertificates(boolean includeRevoked) {
71 return target.getCertificates(includeRevoked);
74 public String getTicket() {
78 public User getTargetUser() {
82 public void grant(Group toMod) {
83 target.grantGroup(supporter, toMod);
86 public void revoke(Group toMod) {
87 target.revokeGroup(supporter, toMod);
90 public void sendSupportNotification(String subject, Outputable message) {
92 StringWriter sw = new StringWriter();
93 PrintWriter outMail = new PrintWriter(sw);
94 outMail.print("Hi," + "\n\n");
95 SprintfCommand.createSimple("supporter {0} triggered:", supporter.getPreferredName().toString()).output(outMail, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
96 outMail.print("\n\n");
97 message.output(outMail, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
98 outMail.print("\n\n");
99 outMail.print("RA DB");
101 String supportemailaddress = "support@" + ServerConstants.getWwwHostName().replaceFirst("^www\\.", "");
102 SendMail.getInstance().sendMail(supportemailaddress, "[" + this.getTicket() + "] RA DB " + subject, sw.toString(), supportemailaddress, null, null, null, null, false);
103 } catch (IOException e) {