]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/SupportedUser.java
add: send mail notification to support after support action
[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 setName(Name newName) throws GigiApiException {
34         if (newName.equals(target.getName())) {
35             return false;
36         }
37         writeSELog("SE Name change");
38         target.setName(newName);
39         return true;
40     }
41
42     public boolean setDob(DayDate dob) throws GigiApiException {
43         if (dob.equals(target.getDoB())) {
44             return false;
45         }
46         writeSELog("SE dob change");
47         target.setDoB(dob);
48         return true;
49     }
50
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) {
57                 certs[i].revoke();
58             }
59         }
60     }
61
62     private void writeSELog(String type) throws GigiApiException {
63         if (ticket == null) {
64             throw new GigiApiException("No ticket set!");
65         }
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);
71             prep.executeUpdate();
72         }
73     }
74
75     public int getId() {
76         return target.getId();
77     }
78
79     public Certificate[] getCertificates(boolean includeRevoked) {
80         return target.getCertificates(includeRevoked);
81     }
82
83     public String getTicket() {
84         return ticket;
85     }
86
87     public User getTargetUser() {
88         return target;
89     }
90
91     public void submitSupportAction() throws GigiApiException {
92         target.rawUpdateUserData();
93     }
94
95     public void grant(Group toMod) {
96         target.grantGroup(supporter, toMod);
97     }
98
99     public void revoke(Group toMod) {
100         target.revokeGroup(supporter, toMod);
101     }
102
103     public void sendSupportNotification(String subject, Outputable message) {
104         try {
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");
113             outMail.close();
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) {
117             e.printStackTrace();
118         }
119     }
120 }