]> WPIA git - gigi.git/blob - src/club/wpia/gigi/dbObjects/SupportedUser.java
84c43ab8e0db5261d745d200d34d69663be20ef8
[gigi.git] / src / club / wpia / gigi / dbObjects / SupportedUser.java
1 package club.wpia.gigi.dbObjects;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Locale;
6
7 import javax.servlet.http.HttpServletRequest;
8
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;
21
22 public class SupportedUser {
23
24     private User target;
25
26     private User supporter;
27
28     private String ticket;
29
30     public SupportedUser(User target, User supporter, String ticket) {
31         this.supporter = supporter;
32         this.target = target;
33         this.ticket = ticket;
34     }
35
36     public boolean setDob(DayDate dob) throws GigiApiException {
37         if (dob.equals(target.getDoB())) {
38             return false;
39         }
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);
49         return true;
50     }
51
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);
59             }
60         }
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."));
66     }
67
68     public void revokeCertificate(Certificate cert) throws GigiApiException {
69
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);
82         }
83     }
84
85     private void writeSELog(String type) throws GigiApiException {
86         if (ticket == null) {
87             throw new GigiApiException("No ticket set!");
88         }
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);
94             prep.executeUpdate();
95         }
96     }
97
98     public int getId() {
99         return target.getId();
100     }
101
102     public Certificate[] getCertificates(boolean includeRevoked) {
103         return target.getCertificates(includeRevoked);
104     }
105
106     public String getTicket() {
107         return ticket;
108     }
109
110     public User getTargetUser() {
111         return target;
112     }
113
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);
127         }
128     }
129
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);
143         }
144     }
145
146     private static final MailTemplate supportNotification = new MailTemplate(SupportedUser.class.getResource("SupportNotificationMail.templ"));
147
148     private void sendSupportNotification(String subject, Outputable message) {
149         try {
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);
155
156             String supportemailaddress = ServerConstants.getSupportMailAddress();
157             supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, supportemailaddress);
158         } catch (IOException e) {
159             e.printStackTrace();
160         }
161     }
162
163     private static final MailTemplate supportUserNotification = new MailTemplate(SupportedUser.class.getResource("SupportUserNotificationMail.templ"));
164
165     private void sendSupportUserNotification(String subject, Outputable message) {
166         try {
167             HashMap<String, Object> vars = new HashMap<>();
168             vars.put("action", message);
169             vars.put("ticket", this.getTicket());
170             vars.put("subject", subject);
171
172             supportUserNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, target.getEmail());
173         } catch (IOException e) {
174             e.printStackTrace();
175         }
176     }
177
178     public void triggerPasswordReset(String aword, HttpServletRequest req) throws GigiApiException {
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);
185         writeSELog("SE triggered password reset");
186     }
187
188     private void sendBoardNotification(String subject, Outputable message) {
189         try {
190             HashMap<String, Object> vars = new HashMap<>();
191             vars.put("supporter", supporter.getPreferredName().toString());
192             vars.put("action", message);
193             vars.put("ticket", this.getTicket());
194             vars.put("subject", subject);
195
196             String boardemailaddress = ServerConstants.getBoardMailAddress();
197             supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, boardemailaddress);
198         } catch (IOException e) {
199             e.printStackTrace();
200         }
201     }
202 }