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