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