]> WPIA git - gigi.git/blob - src/club/wpia/gigi/dbObjects/CertificateOwner.java
72e5a81672762a74510cfbe7743dda61f19d2231
[gigi.git] / src / club / wpia / gigi / dbObjects / CertificateOwner.java
1 package club.wpia.gigi.dbObjects;
2
3 import java.io.IOException;
4 import java.io.ObjectInputStream;
5 import java.io.ObjectOutputStream;
6 import java.io.ObjectStreamException;
7 import java.io.Serializable;
8 import java.math.BigInteger;
9 import java.util.LinkedList;
10 import java.util.List;
11
12 import club.wpia.gigi.GigiApiException;
13 import club.wpia.gigi.database.GigiPreparedStatement;
14 import club.wpia.gigi.database.GigiResultSet;
15
16 public abstract class CertificateOwner implements IdCachable, Serializable {
17
18     private static final long serialVersionUID = -672580485730247314L;
19
20     private static final ObjectCache<CertificateOwner> myCache = new ObjectCache<>();
21
22     private int id;
23
24     protected CertificateOwner(int id) {
25         this.id = id;
26     }
27
28     /**
29      * This constructor has a dummy parameter to allow callers to do checks
30      * before invoking the super constructor.
31      * 
32      * @param dummy
33      *            a parameter that is not used to allow callers to do checks
34      *            before super constructor invocation.
35      */
36     protected CertificateOwner(Void dummy) {
37         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `certOwners` DEFAULT VALUES")) {
38             ps.execute();
39             id = ps.lastInsertId();
40         }
41         myCache.put(this);
42     }
43
44     public int getId() {
45         return id;
46     }
47
48     public static synchronized CertificateOwner getById(int id) {
49         CertificateOwner cached = myCache.get(id);
50         if (cached != null) {
51             return cached;
52         }
53
54         try (GigiPreparedStatement psU = new GigiPreparedStatement("SELECT *, `users`.`id` AS uid FROM `certOwners` INNER JOIN `users` ON `users`.`id`=`certOwners`.`id` WHERE `certOwners`.`id`=? AND `deleted` is null")) {
55             psU.setInt(1, id);
56             GigiResultSet rsU = psU.executeQuery();
57             if (rsU.next()) {
58                 return myCache.put(new User(rsU));
59             }
60         } catch (GigiApiException e) {
61             throw new Error(e);
62         }
63
64         try (GigiPreparedStatement psO = new GigiPreparedStatement("SELECT *, `organisations`.`id` AS oid FROM `certOwners` INNER JOIN `organisations` ON `organisations`.`id`=`certOwners`.`id` WHERE `certOwners`.`id`=? AND `deleted` is null")) {
65             psO.setInt(1, id);
66             GigiResultSet rsO = psO.executeQuery();
67             if (rsO.next()) {
68                 return myCache.put(new Organisation(rsO));
69             }
70         } catch (GigiApiException e) {
71             throw new Error(e);
72         }
73
74         System.err.println("Malformed cert owner: " + id);
75         return null;
76     }
77
78     public Domain[] getDomains() {
79         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `domains` WHERE `memid`=? AND `deleted` IS NULL")) {
80             ps.setInt(1, getId());
81
82             try (GigiResultSet rs = ps.executeQuery()) {
83                 LinkedList<Domain> data = new LinkedList<Domain>();
84
85                 while (rs.next()) {
86                     data.add(Domain.getById(rs.getInt(1)));
87                 }
88
89                 return data.toArray(new Domain[0]);
90             }
91         }
92     }
93
94     public Certificate[] getCertificates(boolean includeRevoked) {
95         try (GigiPreparedStatement ps = new GigiPreparedStatement(includeRevoked ? "SELECT id FROM certs WHERE memid=? ORDER BY id DESC" : "SELECT id FROM certs WHERE memid=? AND `revoked` IS NULL ORDER BY id DESC")) {
96             ps.setInt(1, getId());
97
98             GigiResultSet rs = ps.executeQuery();
99             LinkedList<Certificate> data = new LinkedList<Certificate>();
100
101             while (rs.next()) {
102                 data.add(Certificate.getById(rs.getInt(1)));
103             }
104
105             return data.toArray(new Certificate[0]);
106         }
107     }
108
109     public boolean isValidDomain(String domainname) {
110         for (Domain d : getDomains()) {
111             String sfx = d.getSuffix();
112             if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
113                 return d.isVerified();
114             }
115         }
116
117         return false;
118     }
119
120     public abstract boolean isValidEmail(String email);
121
122     public void delete() {
123         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `certOwners` SET `deleted`=NOW() WHERE `id`=?")) {
124             ps.setInt(1, getId());
125             ps.execute();
126         }
127         myCache.remove(this);
128     }
129
130     public String[] getAdminLog() {
131         try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `when`, type, information FROM `adminLog` WHERE uid=? ORDER BY `when` ASC")) {
132             prep.setInt(1, getId());
133             GigiResultSet res = prep.executeQuery();
134             List<String> entries = new LinkedList<String>();
135
136             while (res.next()) {
137                 entries.add(res.getString(2) + " (" + res.getString(3) + ")");
138             }
139             return entries.toArray(new String[0]);
140         }
141     }
142
143     public static CertificateOwner getByEnabledSerial(BigInteger serial) {
144         try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `memid` FROM `certs` INNER JOIN `logincerts` ON `logincerts`.`id`=`certs`.`id` WHERE serial=? AND `revoked` is NULL")) {
145             prep.setString(1, serial.toString(16));
146             GigiResultSet res = prep.executeQuery();
147             if (res.next()) {
148                 return getById(res.getInt(1));
149             }
150             return null;
151         }
152     }
153
154     private void writeObject(ObjectOutputStream oos) throws IOException {
155         oos.writeLong(getId());
156     }
157
158     private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
159         id = (int) ois.readLong();
160     }
161
162     protected Object readResolve() throws ObjectStreamException {
163         /**
164          * Returning the Object by looking up its ID in the cache.
165          *
166          * @see http://www.javalobby.org/java/forums/t17491.html
167          * @see http://www.jguru.com/faq/view.jsp?EID=44039
168          * @see http://thecodersbreakfast.net/
169          *      ?post/2011/05/12/Serialization-and-magic-methods
170          */
171         CertificateOwner co = getById(this.getId());
172
173         if (null == co) {
174             throw new Error("Unknown Certificate Owner");
175         }
176
177         return co;
178     }
179
180 }