]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/SupportedUser.java
193a32b0853492069c33059b007510c2636eb279
[gigi.git] / src / org / cacert / gigi / dbObjects / SupportedUser.java
1 package org.cacert.gigi.dbObjects;
2
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;
8
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;
18
19 public class SupportedUser {
20
21     private User target;
22
23     private User supporter;
24
25     private String ticket;
26
27     public SupportedUser(User target, User supporter, String ticket) {
28         this.supporter = supporter;
29         this.target = target;
30         this.ticket = ticket;
31     }
32
33     public boolean setDob(DayDate dob) throws GigiApiException {
34         if (dob.equals(target.getDoB())) {
35             return false;
36         }
37         writeSELog("SE dob change");
38         target.setDoBAsSupport(dob);
39         return true;
40     }
41
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) {
48                 certs[i].revoke();
49             }
50         }
51     }
52
53     private void writeSELog(String type) throws GigiApiException {
54         if (ticket == null) {
55             throw new GigiApiException("No ticket set!");
56         }
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);
62             prep.executeUpdate();
63         }
64     }
65
66     public int getId() {
67         return target.getId();
68     }
69
70     public Certificate[] getCertificates(boolean includeRevoked) {
71         return target.getCertificates(includeRevoked);
72     }
73
74     public String getTicket() {
75         return ticket;
76     }
77
78     public User getTargetUser() {
79         return target;
80     }
81
82     public void grant(Group toMod) {
83         target.grantGroup(supporter, toMod);
84     }
85
86     public void revoke(Group toMod) {
87         target.revokeGroup(supporter, toMod);
88     }
89
90     public void sendSupportNotification(String subject, Outputable message) {
91         try {
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");
100             outMail.close();
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) {
104             e.printStackTrace();
105         }
106     }
107 }