]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/SupportedUser.java
upd: use a more strict pattern for handling forms
[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 javax.servlet.http.HttpServletRequest;
8
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.database.GigiPreparedStatement;
11 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
12 import org.cacert.gigi.localisation.Language;
13 import org.cacert.gigi.output.template.MailTemplate;
14 import org.cacert.gigi.output.template.Outputable;
15 import org.cacert.gigi.output.template.SprintfCommand;
16 import org.cacert.gigi.output.template.TranslateCommand;
17 import org.cacert.gigi.pages.PasswordResetPage;
18 import org.cacert.gigi.util.DayDate;
19 import org.cacert.gigi.util.ServerConstants;
20
21 public class SupportedUser {
22
23     private User target;
24
25     private User supporter;
26
27     private String ticket;
28
29     public SupportedUser(User target, User supporter, String ticket) {
30         this.supporter = supporter;
31         this.target = target;
32         this.ticket = ticket;
33     }
34
35     public boolean setDob(DayDate dob) throws GigiApiException {
36         if (dob.equals(target.getDoB())) {
37             return false;
38         }
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);
48         return true;
49     }
50
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) {
57                 certs[i].revoke();
58             }
59         }
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."));
65     }
66
67     public void revokeCertificate(Certificate cert) throws GigiApiException {
68
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);
81         }
82     }
83
84     private void writeSELog(String type) throws GigiApiException {
85         if (ticket == null) {
86             throw new GigiApiException("No ticket set!");
87         }
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);
93             prep.executeUpdate();
94         }
95     }
96
97     public int getId() {
98         return target.getId();
99     }
100
101     public Certificate[] getCertificates(boolean includeRevoked) {
102         return target.getCertificates(includeRevoked);
103     }
104
105     public String getTicket() {
106         return ticket;
107     }
108
109     public User getTargetUser() {
110         return target;
111     }
112
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);
126         }
127     }
128
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);
142         }
143     }
144
145     private static final MailTemplate supportNotification = new MailTemplate(SupportedUser.class.getResource("SupportNotificationMail.templ"));
146
147     private void sendSupportNotification(String subject, Outputable message) {
148         try {
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);
154
155             String supportemailaddress = ServerConstants.getSupportMailAddress();
156             supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, supportemailaddress);
157         } catch (IOException e) {
158             e.printStackTrace();
159         }
160     }
161
162     private static final MailTemplate supportUserNotification = new MailTemplate(SupportedUser.class.getResource("SupportUserNotificationMail.templ"));
163
164     private void sendSupportUserNotification(String subject, Outputable message) {
165         try {
166             HashMap<String, Object> vars = new HashMap<>();
167             vars.put("action", message);
168             vars.put("ticket", this.getTicket());
169             vars.put("subject", subject);
170
171             supportUserNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, target.getEmail());
172         } catch (IOException e) {
173             e.printStackTrace();
174         }
175     }
176
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);
184     }
185
186     private void sendBoardNotification(String subject, Outputable message) {
187         try {
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);
193
194             String boardemailaddress = ServerConstants.getBoardMailAddress();
195             supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, boardemailaddress);
196         } catch (IOException e) {
197             e.printStackTrace();
198         }
199     }
200 }