]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/SupportedUser.java
Enforce Date-of-births to be day-only.
[gigi.git] / src / org / cacert / gigi / dbObjects / SupportedUser.java
1 package org.cacert.gigi.dbObjects;
2
3 import org.cacert.gigi.GigiApiException;
4 import org.cacert.gigi.database.GigiPreparedStatement;
5 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
6 import org.cacert.gigi.util.DayDate;
7
8 public class SupportedUser {
9
10     private User target;
11
12     private User supporter;
13
14     private String ticket;
15
16     public SupportedUser(User target, User supporter, String ticket) {
17         this.supporter = supporter;
18         this.target = target;
19         this.ticket = ticket;
20     }
21
22     public boolean setName(Name newName) throws GigiApiException {
23         if (newName.equals(target.getName())) {
24             return false;
25         }
26         writeSELog("SE Name change");
27         target.setName(newName);
28         return true;
29     }
30
31     public boolean setDob(DayDate dob) throws GigiApiException {
32         if (dob.equals(target.getDoB())) {
33             return false;
34         }
35         writeSELog("SE dob change");
36         target.setDoB(dob);
37         return true;
38     }
39
40     public void revokeAllCertificates() throws GigiApiException {
41         writeSELog("SE Revoke certificates");
42         Certificate[] certs = target.getCertificates(false);
43         // TODO Check for open jobs!
44         for (int i = 0; i < certs.length; i++) {
45             if (certs[i].getStatus() == CertificateStatus.ISSUED) {
46                 certs[i].revoke();
47             }
48         }
49     }
50
51     private void writeSELog(String type) throws GigiApiException {
52         if (ticket == null) {
53             throw new GigiApiException("No ticket set!");
54         }
55         try (GigiPreparedStatement prep = new GigiPreparedStatement("INSERT INTO `adminLog` SET uid=?, admin=?, type=?, information=?")) {
56             prep.setInt(1, target.getId());
57             prep.setInt(2, supporter.getId());
58             prep.setString(3, type);
59             prep.setString(4, ticket);
60             prep.executeUpdate();
61         }
62     }
63
64     public int getId() {
65         return target.getId();
66     }
67
68     public Certificate[] getCertificates(boolean includeRevoked) {
69         return target.getCertificates(includeRevoked);
70     }
71
72     public String getTicket() {
73         return ticket;
74     }
75
76     public User getTargetUser() {
77         return target;
78     }
79
80     public void submitSupportAction() throws GigiApiException {
81         target.rawUpdateUserData();
82     }
83
84     public void grant(Group toMod) {
85         target.grantGroup(supporter, toMod);
86     }
87
88     public void revoke(Group toMod) {
89         target.revokeGroup(supporter, toMod);
90     }
91
92 }