]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/SupportedUser.java
add: defense-in-depth mechanism to prevent unauthorized adding of groups
[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 org.cacert.gigi.GigiApiException;
8 import org.cacert.gigi.database.GigiPreparedStatement;
9 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
10 import org.cacert.gigi.localisation.Language;
11 import org.cacert.gigi.output.template.MailTemplate;
12 import org.cacert.gigi.output.template.Outputable;
13 import org.cacert.gigi.util.DayDate;
14 import org.cacert.gigi.util.ServerConstants;
15
16 public class SupportedUser {
17
18     private User target;
19
20     private User supporter;
21
22     private String ticket;
23
24     public SupportedUser(User target, User supporter, String ticket) {
25         this.supporter = supporter;
26         this.target = target;
27         this.ticket = ticket;
28     }
29
30     public boolean setDob(DayDate dob) throws GigiApiException {
31         if (dob.equals(target.getDoB())) {
32             return false;
33         }
34         writeSELog("SE dob change");
35         target.setDoBAsSupport(dob);
36         return true;
37     }
38
39     public void revokeAllCertificates() throws GigiApiException {
40         writeSELog("SE Revoke certificates");
41         Certificate[] certs = target.getCertificates(false);
42         // TODO Check for open jobs!
43         for (int i = 0; i < certs.length; i++) {
44             if (certs[i].getStatus() == CertificateStatus.ISSUED) {
45                 certs[i].revoke();
46             }
47         }
48     }
49
50     public void revokeCertificate(Certificate cert) throws GigiApiException {
51
52         // TODO Check for open jobs!
53         if (cert.getStatus() == CertificateStatus.ISSUED) {
54             writeSELog("SE Revoke certificate");
55             cert.revoke().waitFor(60000);
56         }
57     }
58
59     private void writeSELog(String type) throws GigiApiException {
60         if (ticket == null) {
61             throw new GigiApiException("No ticket set!");
62         }
63         try (GigiPreparedStatement prep = new GigiPreparedStatement("INSERT INTO `adminLog` SET uid=?, admin=?, type=?, information=?")) {
64             prep.setInt(1, target.getId());
65             prep.setInt(2, supporter.getId());
66             prep.setString(3, type);
67             prep.setString(4, ticket);
68             prep.executeUpdate();
69         }
70     }
71
72     public int getId() {
73         return target.getId();
74     }
75
76     public Certificate[] getCertificates(boolean includeRevoked) {
77         return target.getCertificates(includeRevoked);
78     }
79
80     public String getTicket() {
81         return ticket;
82     }
83
84     public User getTargetUser() {
85         return target;
86     }
87
88     public void grant(Group toMod) throws GigiApiException {
89         target.grantGroup(supporter, toMod);
90     }
91
92     public void revoke(Group toMod) {
93         target.revokeGroup(supporter, toMod);
94     }
95
96     private static final MailTemplate supportNotification = new MailTemplate(SupportedUser.class.getResource("SupportNotificationMail.templ"));
97
98     public void sendSupportNotification(String subject, Outputable message) {
99         try {
100             HashMap<String, Object> vars = new HashMap<>();
101             vars.put("supporter", supporter.getPreferredName().toString());
102             vars.put("action", message);
103             vars.put("ticket", this.getTicket());
104             vars.put("subject", subject);
105
106             String supportemailaddress = ServerConstants.getSupportMailAddress();
107             supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, supportemailaddress);
108         } catch (IOException e) {
109             e.printStackTrace();
110         }
111     }
112 }