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.localisation.Language;
13 import club.wpia.gigi.output.template.MailTemplate;
14 import club.wpia.gigi.output.template.Outputable;
15 import club.wpia.gigi.output.template.SprintfCommand;
16 import club.wpia.gigi.output.template.TranslateCommand;
17 import club.wpia.gigi.pages.PasswordResetPage;
18 import club.wpia.gigi.util.DayDate;
19 import club.wpia.gigi.util.ServerConstants;
21 public class SupportedUser {
25 private User supporter;
27 private String ticket;
29 public SupportedUser(User target, User supporter, String ticket) {
30 this.supporter = supporter;
35 public boolean setDob(DayDate dob) throws GigiApiException {
36 if (dob.equals(target.getDoB())) {
39 writeSELog("SE dob change");
40 target.setDoBAsSupport(dob);
41 String subject = "Change DoB Data";
42 // send notification to support
43 Outputable message = new TranslateCommand("The DoB was changed.");
44 sendSupportNotification(subject, message);
45 // send notification to user
46 message = SprintfCommand.createSimple("The DoB in your account was changed to {0}.", dob);
47 sendSupportUserNotification(subject, message);
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) {
60 // send notification to support
61 Outputable message = SprintfCommand.createSimple("All certificates in the account {0} <{1}> have been revoked.", target.getPreferredName().toString(), target.getEmail());
62 sendSupportNotification("Revoke certificates", message);
63 // send notification to user
64 sendSupportUserNotification("Revoke certificate", new TranslateCommand("All certificates in your account have been revoked."));
67 public void revokeCertificate(Certificate cert) throws GigiApiException {
69 // TODO Check for open jobs!
70 if (cert.getStatus() == CertificateStatus.ISSUED) {
71 writeSELog("SE Revoke certificate");
72 cert.revoke().waitFor(60000);
73 // send notification to support
74 String subject = "Revoke certificate";
75 Outputable message = SprintfCommand.createSimple("Certificate with serial number {0} for {1} <{2}> has been revoked.", cert.getSerial(), target.getPreferredName().toString(), target.getEmail());
76 sendSupportNotification(subject, message);
77 // send notification to user
78 subject = "Revoke certificate";
79 message = SprintfCommand.createSimple("Certificate with serial number {0} with subject distinguished name {1} has been revoked.", cert.getSerial(), cert.getDistinguishedName());
80 sendSupportUserNotification(subject, message);
84 private void writeSELog(String type) throws GigiApiException {
86 throw new GigiApiException("No ticket set!");
88 try (GigiPreparedStatement prep = new GigiPreparedStatement("INSERT INTO `adminLog` SET uid=?, admin=?, type=?, information=?")) {
89 prep.setInt(1, target.getId());
90 prep.setInt(2, supporter.getId());
91 prep.setString(3, type);
92 prep.setString(4, ticket);
98 return target.getId();
101 public Certificate[] getCertificates(boolean includeRevoked) {
102 return target.getCertificates(includeRevoked);
105 public String getTicket() {
109 public User getTargetUser() {
113 public void grant(Group toMod) throws GigiApiException {
114 target.grantGroup(supporter, toMod);
115 String subject = "Change Group Permissions";
116 // send notification to support
117 Outputable message = SprintfCommand.createSimple("The group permission '{0}' was granted.", toMod.getName());
118 sendSupportNotification(subject, message);
119 // send notification to user
120 message = SprintfCommand.createSimple("The group permission '{0}' was granted to your account.", toMod.getName());
121 sendSupportUserNotification(subject, message);
122 if (toMod == Group.SUPPORTER) {
123 subject = "Support role granted";
124 message = SprintfCommand.createSimple("The group permission '{0}' was granted for '{1}'.", toMod.getName(), target.getPreferredName().toString());
125 sendBoardNotification(subject, message);
129 public void revoke(Group toMod) throws GigiApiException {
130 target.revokeGroup(supporter, toMod);
131 String subject = "Change Group Permissions";
132 // send notification to support
133 Outputable message = SprintfCommand.createSimple("The group permission '{0}' was revoked.", toMod.getName());
134 sendSupportNotification(subject, message);
135 // send notification to user
136 message = SprintfCommand.createSimple("The group permission '{0}' was revoked from your account.", toMod.getName());
137 sendSupportUserNotification(subject, message);
138 if (toMod == Group.SUPPORTER) {
139 subject = "Support role revoked";
140 message = SprintfCommand.createSimple("The group permission '{0}' was revoked for '{1}'.", toMod.getName(), target.getPreferredName().toString());
141 sendBoardNotification(subject, message);
145 private static final MailTemplate supportNotification = new MailTemplate(SupportedUser.class.getResource("SupportNotificationMail.templ"));
147 private void sendSupportNotification(String subject, Outputable message) {
149 HashMap<String, Object> vars = new HashMap<>();
150 vars.put("supporter", supporter.getPreferredName().toString());
151 vars.put("action", message);
152 vars.put("ticket", this.getTicket());
153 vars.put("subject", subject);
155 String supportemailaddress = ServerConstants.getSupportMailAddress();
156 supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, supportemailaddress);
157 } catch (IOException e) {
162 private static final MailTemplate supportUserNotification = new MailTemplate(SupportedUser.class.getResource("SupportUserNotificationMail.templ"));
164 private void sendSupportUserNotification(String subject, Outputable message) {
166 HashMap<String, Object> vars = new HashMap<>();
167 vars.put("action", message);
168 vars.put("ticket", this.getTicket());
169 vars.put("subject", subject);
171 supportUserNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, target.getEmail());
172 } catch (IOException e) {
177 public void triggerPasswordReset(String aword, HttpServletRequest req) {
178 Language l = Language.getInstance(target.getPreferredLocale());
179 String method = l.getTranslation("A password reset was triggered. Please enter the required text sent to you by support on this page:");
180 String subject = l.getTranslation("Password reset by support.");
181 PasswordResetPage.initPasswordResetProcess(target, req, aword, l, method, subject);
182 Outputable message = new TranslateCommand("A password reset was triggered and an email was sent to user.");
183 sendSupportNotification(subject, message);
186 private void sendBoardNotification(String subject, Outputable message) {
188 HashMap<String, Object> vars = new HashMap<>();
189 vars.put("supporter", supporter.getPreferredName().toString());
190 vars.put("action", message);
191 vars.put("ticket", this.getTicket());
192 vars.put("subject", subject);
194 String boardemailaddress = ServerConstants.getBoardMailAddress();
195 supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, boardemailaddress);
196 } catch (IOException e) {