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 setName(Name newName) throws GigiApiException {
34 if (newName.equals(target.getName())) {
37 writeSELog("SE Name change");
38 target.setName(newName);
42 public boolean setDob(DayDate dob) throws GigiApiException {
43 if (dob.equals(target.getDoB())) {
46 writeSELog("SE dob change");
51 public void revokeAllCertificates() throws GigiApiException {
52 writeSELog("SE Revoke certificates");
53 Certificate[] certs = target.getCertificates(false);
54 // TODO Check for open jobs!
55 for (int i = 0; i < certs.length; i++) {
56 if (certs[i].getStatus() == CertificateStatus.ISSUED) {
62 private void writeSELog(String type) throws GigiApiException {
64 throw new GigiApiException("No ticket set!");
66 try (GigiPreparedStatement prep = new GigiPreparedStatement("INSERT INTO `adminLog` SET uid=?, admin=?, type=?, information=?")) {
67 prep.setInt(1, target.getId());
68 prep.setInt(2, supporter.getId());
69 prep.setString(3, type);
70 prep.setString(4, ticket);
76 return target.getId();
79 public Certificate[] getCertificates(boolean includeRevoked) {
80 return target.getCertificates(includeRevoked);
83 public String getTicket() {
87 public User getTargetUser() {
91 public void submitSupportAction() throws GigiApiException {
92 target.rawUpdateUserData();
95 public void grant(Group toMod) {
96 target.grantGroup(supporter, toMod);
99 public void revoke(Group toMod) {
100 target.revokeGroup(supporter, toMod);
103 public void sendSupportNotification(String subject, Outputable message) {
105 StringWriter sw = new StringWriter();
106 PrintWriter outMail = new PrintWriter(sw);
107 outMail.print("Hi," + "\n\n");
108 SprintfCommand.createSimple("supporter {0} triggered:", supporter.getName().toString()).output(outMail, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
109 outMail.print("\n\n");
110 message.output(outMail, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
111 outMail.print("\n\n");
112 outMail.print("RA DB");
114 String supportemailaddress = "support@" + ServerConstants.getWwwHostName().replaceFirst("^www\\.", "");
115 SendMail.getInstance().sendMail(supportemailaddress, "[" + this.getTicket() + "] RA DB " + subject, sw.toString(), supportemailaddress, null, null, null, null, false);
116 } catch (IOException e) {