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