upd: transform existing mails into mail templates
[gigi.git] / src / org / cacert / gigi / dbObjects / SupportedUser.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Locale;
6
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.util.DayDate;
14 import org.cacert.gigi.util.ServerConstants;
15
16 public class SupportedUser {
17
18     private User target;
19
20     private User supporter;
21
22     private String ticket;
23
24     public SupportedUser(User target, User supporter, String ticket) {
25         this.supporter = supporter;
26         this.target = target;
27         this.ticket = ticket;
28     }
29
30     public boolean setDob(DayDate dob) throws GigiApiException {
31         if (dob.equals(target.getDoB())) {
32             return false;
33         }
34         writeSELog("SE dob change");
35         target.setDoBAsSupport(dob);
36         return true;
37     }
38
39     public void revokeAllCertificates() throws GigiApiException {
40         writeSELog("SE Revoke certificates");
41         Certificate[] certs = target.getCertificates(false);
42         // TODO Check for open jobs!
43         for (int i = 0; i < certs.length; i++) {
44             if (certs[i].getStatus() == CertificateStatus.ISSUED) {
45                 certs[i].revoke();
46             }
47         }
48     }
49
50     private void writeSELog(String type) throws GigiApiException {
51         if (ticket == null) {
52             throw new GigiApiException("No ticket set!");
53         }
54         try (GigiPreparedStatement prep = new GigiPreparedStatement("INSERT INTO `adminLog` SET uid=?, admin=?, type=?, information=?")) {
55             prep.setInt(1, target.getId());
56             prep.setInt(2, supporter.getId());
57             prep.setString(3, type);
58             prep.setString(4, ticket);
59             prep.executeUpdate();
60         }
61     }
62
63     public int getId() {
64         return target.getId();
65     }
66
67     public Certificate[] getCertificates(boolean includeRevoked) {
68         return target.getCertificates(includeRevoked);
69     }
70
71     public String getTicket() {
72         return ticket;
73     }
74
75     public User getTargetUser() {
76         return target;
77     }
78
79     public void grant(Group toMod) {
80         target.grantGroup(supporter, toMod);
81     }
82
83     public void revoke(Group toMod) {
84         target.revokeGroup(supporter, toMod);
85     }
86
87     private static final MailTemplate supportNotification = new MailTemplate(SupportedUser.class.getResource("SupportNotificationMail.templ"));
88
89     public void sendSupportNotification(String subject, Outputable message) {
90         try {
91             HashMap<String, Object> vars = new HashMap<>();
92             vars.put("supporter", supporter.getPreferredName().toString());
93             vars.put("action", message);
94             vars.put("ticket", this.getTicket());
95             vars.put("subject", subject);
96
97             String supportemailaddress = "support@" + ServerConstants.getWwwHostName().replaceFirst("^www\\.", "");
98             supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, supportemailaddress);
99         } catch (IOException e) {
100             e.printStackTrace();
101         }
102     }
103 }