1 package club.wpia.gigi.dbObjects;
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Locale;
7 import javax.servlet.http.HttpServletRequest;
9 import club.wpia.gigi.GigiApiException;
10 import club.wpia.gigi.database.GigiPreparedStatement;
11 import club.wpia.gigi.dbObjects.Certificate.CertificateStatus;
12 import club.wpia.gigi.dbObjects.Certificate.RevocationType;
13 import club.wpia.gigi.localisation.Language;
14 import club.wpia.gigi.output.template.MailTemplate;
15 import club.wpia.gigi.output.template.Outputable;
16 import club.wpia.gigi.output.template.SprintfCommand;
17 import club.wpia.gigi.output.template.TranslateCommand;
18 import club.wpia.gigi.pages.PasswordResetPage;
19 import club.wpia.gigi.util.DayDate;
20 import club.wpia.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) {
58 certs[i].revoke(RevocationType.SUPPORT);
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(RevocationType.SUPPORT).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);
123 if (toMod == Group.SUPPORTER) {
124 subject = "Support role granted";
125 message = SprintfCommand.createSimple("The group permission '{0}' was granted for '{1}'.", toMod.getName(), target.getPreferredName().toString());
126 sendBoardNotification(subject, message);
130 public void revoke(Group toMod) throws GigiApiException {
131 target.revokeGroup(supporter, toMod);
132 String subject = "Change Group Permissions";
133 // send notification to support
134 Outputable message = SprintfCommand.createSimple("The group permission '{0}' was revoked.", toMod.getName());
135 sendSupportNotification(subject, message);
136 // send notification to user
137 message = SprintfCommand.createSimple("The group permission '{0}' was revoked from your account.", toMod.getName());
138 sendSupportUserNotification(subject, message);
139 if (toMod == Group.SUPPORTER) {
140 subject = "Support role revoked";
141 message = SprintfCommand.createSimple("The group permission '{0}' was revoked for '{1}'.", toMod.getName(), target.getPreferredName().toString());
142 sendBoardNotification(subject, message);
146 private static final MailTemplate supportNotification = new MailTemplate(SupportedUser.class.getResource("SupportNotificationMail.templ"));
148 private void sendSupportNotification(String subject, Outputable message) {
150 HashMap<String, Object> vars = new HashMap<>();
151 vars.put("supporter", supporter.getPreferredName().toString());
152 vars.put("action", message);
153 vars.put("ticket", this.getTicket());
154 vars.put("subject", subject);
156 String supportemailaddress = ServerConstants.getSupportMailAddress();
157 supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, supportemailaddress);
158 } catch (IOException e) {
163 private static final MailTemplate supportUserNotification = new MailTemplate(SupportedUser.class.getResource("SupportUserNotificationMail.templ"));
165 private void sendSupportUserNotification(String subject, Outputable message) {
167 HashMap<String, Object> vars = new HashMap<>();
168 vars.put("action", message);
169 vars.put("ticket", this.getTicket());
170 vars.put("subject", subject);
172 supportUserNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, target.getEmail());
173 } catch (IOException e) {
178 public void triggerPasswordReset(String aword, HttpServletRequest req) {
179 Language l = Language.getInstance(target.getPreferredLocale());
180 String method = l.getTranslation("A password reset was triggered. Please enter the required text sent to you by support on this page:");
181 String subject = l.getTranslation("Password reset by support.");
182 PasswordResetPage.initPasswordResetProcess(target, req, aword, l, method, subject);
183 Outputable message = new TranslateCommand("A password reset was triggered and an email was sent to user.");
184 sendSupportNotification(subject, message);
187 private void sendBoardNotification(String subject, Outputable message) {
189 HashMap<String, Object> vars = new HashMap<>();
190 vars.put("supporter", supporter.getPreferredName().toString());
191 vars.put("action", message);
192 vars.put("ticket", this.getTicket());
193 vars.put("subject", subject);
195 String boardemailaddress = ServerConstants.getBoardMailAddress();
196 supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, boardemailaddress);
197 } catch (IOException e) {