1 package org.cacert.gigi.dbObjects;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.HashMap;
6 import java.util.Locale;
8 import javax.servlet.http.HttpServletRequest;
10 import org.cacert.gigi.GigiApiException;
11 import org.cacert.gigi.database.GigiPreparedStatement;
12 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.output.template.MailTemplate;
15 import org.cacert.gigi.output.template.Outputable;
16 import org.cacert.gigi.output.template.SprintfCommand;
17 import org.cacert.gigi.output.template.TranslateCommand;
18 import org.cacert.gigi.pages.PasswordResetPage;
19 import org.cacert.gigi.util.DayDate;
20 import org.cacert.gigi.util.ServerConstants;
22 public class SupportedUser {
26 private User supporter;
28 private String ticket;
30 public SupportedUser(User target, User supporter, String ticket) {
31 this.supporter = supporter;
36 public boolean setDob(DayDate dob) throws GigiApiException {
37 if (dob.equals(target.getDoB())) {
40 writeSELog("SE dob change");
41 target.setDoBAsSupport(dob);
42 String subject = "Change DoB Data";
43 // send notification to support
44 Outputable message = new TranslateCommand("The DoB was changed.");
45 sendSupportNotification(subject, message);
46 // send notification to user
47 message = SprintfCommand.createSimple("The DoB in your account was changed to {0}.", dob);
48 sendSupportUserNotification(subject, message);
52 public void revokeAllCertificates() throws GigiApiException {
53 writeSELog("SE Revoke certificates");
54 Certificate[] certs = target.getCertificates(false);
55 // TODO Check for open jobs!
56 for (int i = 0; i < certs.length; i++) {
57 if (certs[i].getStatus() == CertificateStatus.ISSUED) {
61 // send notification to support
62 Outputable message = SprintfCommand.createSimple("All certificates in the account {0} <{1}> have been revoked.", target.getPreferredName().toString(), target.getEmail());
63 sendSupportNotification("Revoke certificates", message);
64 // send notification to user
65 sendSupportUserNotification("Revoke certificate", new TranslateCommand("All certificates in your account have been revoked."));
68 public void revokeCertificate(Certificate cert) throws GigiApiException {
70 // TODO Check for open jobs!
71 if (cert.getStatus() == CertificateStatus.ISSUED) {
72 writeSELog("SE Revoke certificate");
73 cert.revoke().waitFor(60000);
74 // send notification to support
75 String subject = "Revoke certificate";
76 Outputable message = SprintfCommand.createSimple("Certificate with serial number {0} for {1} <{2}> has been revoked.", cert.getSerial(), target.getPreferredName().toString(), target.getEmail());
77 sendSupportNotification(subject, message);
78 // send notification to user
79 subject = "Revoke certificate";
80 message = SprintfCommand.createSimple("Certificate with serial number {0} with subject distinguished name {1} has been revoked.", cert.getSerial(), cert.getDistinguishedName());
81 sendSupportUserNotification(subject, message);
85 private void writeSELog(String type) throws GigiApiException {
87 throw new GigiApiException("No ticket set!");
89 try (GigiPreparedStatement prep = new GigiPreparedStatement("INSERT INTO `adminLog` SET uid=?, admin=?, type=?, information=?")) {
90 prep.setInt(1, target.getId());
91 prep.setInt(2, supporter.getId());
92 prep.setString(3, type);
93 prep.setString(4, ticket);
99 return target.getId();
102 public Certificate[] getCertificates(boolean includeRevoked) {
103 return target.getCertificates(includeRevoked);
106 public String getTicket() {
110 public User getTargetUser() {
114 public void grant(Group toMod) throws GigiApiException {
115 target.grantGroup(supporter, toMod);
116 String subject = "Change Group Permissions";
117 // send notification to support
118 Outputable message = SprintfCommand.createSimple("The group permission '{0}' was granted.", toMod.getName());
119 sendSupportNotification(subject, message);
120 // send notification to user
121 message = SprintfCommand.createSimple("The group permission '{0}' was granted to your account.", toMod.getName());
122 sendSupportUserNotification(subject, message);
125 public void revoke(Group toMod) {
126 target.revokeGroup(supporter, toMod);
127 String subject = "Change Group Permissions";
128 // send notification to support
129 Outputable message = SprintfCommand.createSimple("The group permission '{0}' was revoked.", toMod.getName());
130 sendSupportNotification(subject, message);
131 // send notification to user
132 message = SprintfCommand.createSimple("The group permission '{0}' was revoked from your account.", toMod.getName());
133 sendSupportUserNotification(subject, message);
136 private static final MailTemplate supportNotification = new MailTemplate(SupportedUser.class.getResource("SupportNotificationMail.templ"));
138 private void sendSupportNotification(String subject, Outputable message) {
140 HashMap<String, Object> vars = new HashMap<>();
141 vars.put("supporter", supporter.getPreferredName().toString());
142 vars.put("action", message);
143 vars.put("ticket", this.getTicket());
144 vars.put("subject", subject);
146 String supportemailaddress = ServerConstants.getSupportMailAddress();
147 supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, supportemailaddress);
148 } catch (IOException e) {
153 private static final MailTemplate supportUserNotification = new MailTemplate(SupportedUser.class.getResource("SupportUserNotificationMail.templ"));
155 private void sendSupportUserNotification(String subject, Outputable message) {
157 HashMap<String, Object> vars = new HashMap<>();
158 vars.put("action", message);
159 vars.put("ticket", this.getTicket());
160 vars.put("subject", subject);
162 supportUserNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, target.getEmail());
163 } catch (IOException e) {
168 public void triggerPasswordReset(String aword, PrintWriter out, HttpServletRequest req) {
169 Language l = Language.getInstance(target.getPreferredLocale());
170 String method = l.getTranslation("A password reset was triggered. Please enter the required text sent to you by support on this page:");
171 String subject = l.getTranslation("Password reset by support.");
172 PasswordResetPage.initPasswordResetProcess(out, target, req, aword, l, method, subject);
173 Outputable message = new TranslateCommand("A password reset was triggered and an email was sent to user.");
174 sendSupportNotification(subject, message);