]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/CertificateOwner.java
Merge remote-tracking branch 'origin/libs/scrypt/local'
[gigi.git] / src / org / cacert / gigi / dbObjects / CertificateOwner.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.util.LinkedList;
4
5 import org.cacert.gigi.database.DatabaseConnection;
6 import org.cacert.gigi.database.GigiPreparedStatement;
7 import org.cacert.gigi.database.GigiResultSet;
8
9 public abstract class CertificateOwner implements IdCachable {
10
11     private static ObjectCache<CertificateOwner> myCache = new ObjectCache<>();
12
13     private int id;
14
15     public CertificateOwner(int id) {
16         this.id = id;
17     }
18
19     public CertificateOwner() {}
20
21     public int getId() {
22         return id;
23     }
24
25     public static synchronized CertificateOwner getById(int id) {
26         CertificateOwner u = myCache.get(id);
27         if (u == null) {
28             GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("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");
29             ps.setInt(1, id);
30             try (GigiResultSet rs = ps.executeQuery()) {
31                 if ( !rs.next()) {
32                     return null;
33                 }
34                 if (rs.getString("uid") != null) {
35                     myCache.put(u = new User(rs));
36                 } else if (rs.getString("oid") != null) {
37                     myCache.put(u = new Organisation(rs));
38                 } else {
39                     System.err.print("Malformed cert owner: " + id);
40                 }
41             }
42         }
43         return u;
44     }
45
46     protected int insert() {
47         synchronized (User.class) {
48             if (id != 0) {
49                 throw new Error("refusing to insert");
50             }
51             GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO certOwners() VALUES()");
52             ps.execute();
53             id = ps.lastInsertId();
54             myCache.put(this);
55         }
56
57         return id;
58     }
59
60     public EmailAddress[] getEmails() {
61         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted is NULL");
62         ps.setInt(1, getId());
63
64         try (GigiResultSet rs = ps.executeQuery()) {
65             LinkedList<EmailAddress> data = new LinkedList<EmailAddress>();
66
67             while (rs.next()) {
68                 data.add(EmailAddress.getById(rs.getInt(1)));
69             }
70
71             return data.toArray(new EmailAddress[0]);
72         }
73     }
74
75     public Domain[] getDomains() {
76         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM domains WHERE memid=? AND deleted IS NULL");
77         ps.setInt(1, getId());
78
79         try (GigiResultSet rs = ps.executeQuery()) {
80             LinkedList<Domain> data = new LinkedList<Domain>();
81
82             while (rs.next()) {
83                 data.add(Domain.getById(rs.getInt(1)));
84             }
85
86             return data.toArray(new Domain[0]);
87         }
88     }
89
90     public Certificate[] getCertificates(boolean includeRevoked) {
91         GigiPreparedStatement ps;
92         if (includeRevoked) {
93             ps = DatabaseConnection.getInstance().prepare("SELECT serial FROM certs WHERE memid=?");
94         } else {
95             ps = DatabaseConnection.getInstance().prepare("SELECT serial FROM certs WHERE memid=? AND revoked IS NULL");
96         }
97         ps.setInt(1, getId());
98
99         try (GigiResultSet rs = ps.executeQuery()) {
100             LinkedList<Certificate> data = new LinkedList<Certificate>();
101
102             while (rs.next()) {
103                 data.add(Certificate.getBySerial(rs.getString(1)));
104             }
105
106             return data.toArray(new Certificate[0]);
107         }
108     }
109
110     public boolean isValidDomain(String domainname) {
111         for (Domain d : getDomains()) {
112             String sfx = d.getSuffix();
113             if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
114                 return true;
115             }
116         }
117
118         return false;
119     }
120
121     public boolean isValidEmail(String email) {
122         for (EmailAddress em : getEmails()) {
123             if (em.getAddress().equals(email)) {
124                 return true;
125             }
126         }
127
128         return false;
129     }
130
131     public void delete() {
132         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE certOwners SET deleted=NOW() WHERE id=?");
133         ps.setInt(1, getId());
134         ps.execute();
135         myCache.remove(this);
136     }
137
138 }