]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Organisation.java
bbb3214c84ad2408b690cdf9b41d2eab43c83863
[gigi.git] / src / org / cacert / gigi / dbObjects / Organisation.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.util.ArrayList;
7 import java.util.List;
8
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.database.GigiPreparedStatement;
11 import org.cacert.gigi.database.GigiResultSet;
12 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
13 import org.cacert.gigi.dbObjects.wrappers.DataContainer;
14
15 public class Organisation extends CertificateOwner {
16
17     private static final long serialVersionUID = -2386342985586320843L;
18
19     @DataContainer
20     public static class Affiliation {
21
22         private final User target;
23
24         private final boolean master;
25
26         private final String fixedOU;
27
28         private Organisation o;
29
30         public Affiliation(Organisation o, User target, boolean master, String fixedOU) {
31             this.o = o;
32             this.target = target;
33             this.master = master;
34             this.fixedOU = fixedOU;
35         }
36
37         public User getTarget() {
38             return target;
39         }
40
41         public boolean isMaster() {
42             return master;
43         }
44
45         public String getFixedOU() {
46             return fixedOU;
47         }
48
49         public Organisation getOrganisation() {
50             return o;
51         }
52     }
53
54     private String name;
55
56     private String state;
57
58     private String province;
59
60     private String city;
61
62     private String email;
63
64     private String optionalName;
65
66     private String postalAddress;
67
68     public Organisation(String name, CountryCode state, String province, String city, String email, String optionalName, String postalAddress, User creator) throws GigiApiException {
69         if ( !creator.isInGroup(Group.ORGASSURER)) {
70             throw new GigiApiException("Only Organisation RA Agents may create organisations.");
71         }
72         this.name = name;
73         this.state = state.getCountryCode();
74         this.province = province;
75         this.city = city;
76         this.email = email;
77         this.optionalName = optionalName;
78         this.postalAddress = postalAddress;
79         int id = getId();
80         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, optional_name=?, postal_address=?, creator=?")) {
81             ps.setInt(1, id);
82             ps.setString(2, name);
83             ps.setString(3, state.getCountryCode());
84             ps.setString(4, province);
85             ps.setString(5, city);
86             ps.setString(6, email);
87             ps.setString(7, optionalName);
88             ps.setString(8, postalAddress);
89             ps.setInt(9, creator.getId());
90             synchronized (Organisation.class) {
91                 ps.execute();
92             }
93         }
94     }
95
96     protected Organisation(GigiResultSet rs) {
97         super(rs.getInt("id"));
98         name = rs.getString("name");
99         state = rs.getString("state");
100         province = rs.getString("province");
101         city = rs.getString("city");
102         email = rs.getString("contactEmail");
103         optionalName = rs.getString("optional_name");
104         postalAddress = rs.getString("postal_address");
105     }
106
107     public String getName() {
108         return name;
109     }
110
111     public String getState() {
112         return state;
113     }
114
115     public String getProvince() {
116         return province;
117     }
118
119     public String getCity() {
120         return city;
121     }
122
123     public String getContactEmail() {
124         return email;
125     }
126
127     public String getOptionalName() {
128         return optionalName;
129     }
130
131     public String getPostalAddress() {
132         return postalAddress;
133     }
134
135     public static synchronized Organisation getById(int id) {
136         CertificateOwner co = CertificateOwner.getById(id);
137         if (co instanceof Organisation) {
138             return (Organisation) co;
139         }
140         throw new IllegalArgumentException("Organisation not found.");
141     }
142
143     public synchronized void addAdmin(User admin, User actor, boolean master) throws GigiApiException {
144         if ( !admin.canAssure()) {
145             throw new GigiApiException("Cannot add person who is not RA Agent.");
146         }
147         if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
148             throw new GigiApiException("Only Organisation RA Agents or Organisation Administrators may add admins to an organisation.");
149         }
150         try (GigiPreparedStatement ps1 = new GigiPreparedStatement("SELECT 1 FROM `org_admin` WHERE `orgid`=? AND `memid`=? AND `deleted` IS NULL")) {
151             ps1.setInt(1, getId());
152             ps1.setInt(2, admin.getId());
153             GigiResultSet result = ps1.executeQuery();
154             if (result.next()) {
155                 return;
156             }
157         }
158         try (GigiPreparedStatement ps2 = new GigiPreparedStatement("INSERT INTO `org_admin` SET `orgid`=?, `memid`=?, `creator`=?, `master`=?::`yesno`")) {
159             ps2.setInt(1, getId());
160             ps2.setInt(2, admin.getId());
161             ps2.setInt(3, actor.getId());
162             ps2.setString(4, master ? "y" : "n");
163             ps2.execute();
164         }
165     }
166
167     public void removeAdmin(User admin, User actor) throws GigiApiException {
168         if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
169             throw new GigiApiException("Only Organisation RA Agents or Organisation Administrators may delete admins from an organisation.");
170         }
171         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?")) {
172             ps.setInt(1, actor.getId());
173             ps.setInt(2, getId());
174             ps.setInt(3, admin.getId());
175             ps.execute();
176         }
177     }
178
179     public List<Affiliation> getAllAdmins() {
180         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `master` FROM `org_admin` WHERE `orgid`=? AND `deleted` IS NULL", true)) {
181             ps.setInt(1, getId());
182             GigiResultSet rs = ps.executeQuery();
183             rs.last();
184             ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
185             rs.beforeFirst();
186             while (rs.next()) {
187                 al.add(new Affiliation(this, User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
188             }
189             return al;
190         }
191     }
192
193     public static Organisation[] getOrganisations(int offset, int count) {
194         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `certOwners`.`id` FROM `organisations` INNER JOIN `certOwners` ON `certOwners`.`id`=`organisations`.`id` WHERE `certOwners`.`deleted` IS NULL OFFSET ? LIMIT ?", true)) {
195             ps.setInt(1, offset);
196             ps.setInt(2, count);
197             GigiResultSet res = ps.executeQuery();
198             res.last();
199             Organisation[] resu = new Organisation[res.getRow()];
200             res.beforeFirst();
201             int i = 0;
202             while (res.next()) {
203                 resu[i++] = getById(res.getInt(1));
204             }
205             return resu;
206         }
207     }
208
209     public void updateCertData(String o, CountryCode c, String st, String l) {
210         for (Certificate cert : getCertificates(false)) {
211             if (cert.getStatus() == CertificateStatus.ISSUED) {
212                 cert.revoke();
213             }
214         }
215         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `state`=?, `province`=?, `city`=? WHERE `id`=?")) {
216             ps.setString(1, o);
217             ps.setString(2, c.getCountryCode());
218             ps.setString(3, st);
219             ps.setString(4, l);
220             ps.setInt(5, getId());
221             ps.executeUpdate();
222         }
223         name = o;
224         state = c.getCountryCode();
225         province = st;
226         city = l;
227     }
228
229     public void updateOrgData(String mail, String o_name, String p_address) {
230         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `contactEmail`=?, `optional_name`=?, `postal_address`=? WHERE `id`=?")) {
231             ps.setString(1, mail);
232             ps.setString(2, o_name);
233             ps.setString(3, p_address);
234             ps.setInt(4, getId());
235             ps.executeUpdate();
236         }
237         email = mail;
238         optionalName = o_name;
239         postalAddress = p_address;
240     }
241
242     public boolean isMaster(User u) {
243         for (Affiliation i : getAllAdmins()) {
244             if (i.isMaster() && i.getTarget() == u) {
245                 return true;
246             }
247         }
248         return false;
249     }
250
251     @Override
252     public boolean isValidEmail(String email) {
253         return isValidDomain(email.split("@", 2)[1]);
254     }
255
256     public static final String SELF_ORG_NAME = "SomeCA";
257
258     public boolean isSelfOrganisation() {
259         return SELF_ORG_NAME.equals(getName());
260     }
261
262     private void writeObject(ObjectOutputStream oos) throws IOException {}
263
264     private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {}
265
266 }