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