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