]> WPIA git - gigi.git/blob - src/club/wpia/gigi/dbObjects/Organisation.java
c9754565744b0d107baee2515f802e2e2cae9fbe
[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         super(validate(creator, country));
72         this.name = name;
73         this.country = country;
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=?, country=?, province=?, city=?, contactEmail=?, optional_name=?, postal_address=?, creator=?")) {
81             ps.setInt(1, id);
82             ps.setString(2, name);
83             ps.setString(3, country.getCode());
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     private static Void validate(User creator, Country country) throws GigiApiException {
97         if ( !creator.isInGroup(Group.ORG_AGENT)) {
98             throw new GigiApiException("Only Organisation RA Agents may create organisations.");
99         }
100         if (country == null) {
101             throw new GigiApiException("Got country code of illegal type.");
102         }
103         return null;
104     }
105
106     protected Organisation(GigiResultSet rs) throws GigiApiException {
107         super(rs.getInt("id"));
108         name = rs.getString("name");
109         country = Country.getCountryByCode(rs.getString("country"), CountryCodeType.CODE_2_CHARS);
110         province = rs.getString("province");
111         city = rs.getString("city");
112         email = rs.getString("contactEmail");
113         optionalName = rs.getString("optional_name");
114         postalAddress = rs.getString("postal_address");
115     }
116
117     public String getName() {
118         return name;
119     }
120
121     public Country getCountry() {
122         return country;
123     }
124
125     public String getProvince() {
126         return province;
127     }
128
129     public String getCity() {
130         return city;
131     }
132
133     public String getContactEmail() {
134         return email;
135     }
136
137     public String getOptionalName() {
138         return optionalName;
139     }
140
141     public String getPostalAddress() {
142         return postalAddress;
143     }
144
145     public static synchronized Organisation getById(int id) {
146         CertificateOwner co = CertificateOwner.getById(id);
147         if (co instanceof Organisation) {
148             return (Organisation) co;
149         }
150         throw new IllegalArgumentException("Organisation not found.");
151     }
152
153     public synchronized void addAdmin(User admin, User actor, boolean master) throws GigiApiException {
154         if (actor == admin) {
155             throw new GigiApiException("You may not add yourself as Organisation Admin. Ask another Organisation Agent to do so.");
156         }
157         if ( !admin.canVerify()) {
158             throw new GigiApiException("Cannot add person who is not RA Agent.");
159         }
160         if ( !actor.isInGroup(Group.ORG_AGENT) && !isMaster(actor)) {
161             throw new GigiApiException("Only Organisation RA Agents or Organisation Administrators may add admins to an organisation.");
162         }
163         try (GigiPreparedStatement ps1 = new GigiPreparedStatement("SELECT 1 FROM `org_admin` WHERE `orgid`=? AND `memid`=? AND `deleted` IS NULL")) {
164             ps1.setInt(1, getId());
165             ps1.setInt(2, admin.getId());
166             GigiResultSet result = ps1.executeQuery();
167             if (result.next()) {
168                 return;
169             }
170         }
171         try (GigiPreparedStatement ps2 = new GigiPreparedStatement("INSERT INTO `org_admin` SET `orgid`=?, `memid`=?, `creator`=?, `master`=?::`yesno`")) {
172             ps2.setInt(1, getId());
173             ps2.setInt(2, admin.getId());
174             ps2.setInt(3, actor.getId());
175             ps2.setString(4, master ? "y" : "n");
176             ps2.execute();
177         }
178     }
179
180     public void removeAdmin(User admin, User actor) throws GigiApiException {
181         if ( !actor.isInGroup(Group.ORG_AGENT) && !isMaster(actor)) {
182             throw new GigiApiException("Only Organisation RA Agents or Organisation Administrators may delete admins from an organisation.");
183         }
184         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?")) {
185             ps.setInt(1, actor.getId());
186             ps.setInt(2, getId());
187             ps.setInt(3, admin.getId());
188             ps.execute();
189         }
190     }
191
192     public List<Affiliation> getAllAdmins() {
193         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `master` FROM `org_admin` WHERE `orgid`=? AND `deleted` IS NULL", true)) {
194             ps.setInt(1, getId());
195             GigiResultSet rs = ps.executeQuery();
196             rs.last();
197             ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
198             rs.beforeFirst();
199             while (rs.next()) {
200                 al.add(new Affiliation(this, User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
201             }
202             return al;
203         }
204     }
205
206     public static Organisation[] getOrganisations(int offset, int count) {
207         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)) {
208             ps.setInt(1, offset);
209             ps.setInt(2, count);
210             GigiResultSet res = ps.executeQuery();
211             res.last();
212             Organisation[] resu = new Organisation[res.getRow()];
213             res.beforeFirst();
214             int i = 0;
215             while (res.next()) {
216                 resu[i++] = getById(res.getInt(1));
217             }
218             return resu;
219         }
220     }
221
222     public void updateCertData(String o, Country c, String st, String l) throws GigiApiException {
223         if (c == null) {
224             throw new GigiApiException("Got country code of illegal type.");
225         }
226         for (Certificate cert : getCertificates(false)) {
227             if (cert.getStatus() == CertificateStatus.ISSUED) {
228                 cert.revoke(RevocationType.USER);
229             }
230         }
231         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `country`=?, `province`=?, `city`=? WHERE `id`=?")) {
232             ps.setString(1, o);
233             ps.setString(2, c.getCode());
234             ps.setString(3, st);
235             ps.setString(4, l);
236             ps.setInt(5, getId());
237             ps.executeUpdate();
238         }
239         name = o;
240         country = c;
241         province = st;
242         city = l;
243     }
244
245     public void updateOrgData(String mail, String o_name, String p_address) {
246         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `contactEmail`=?, `optional_name`=?, `postal_address`=? WHERE `id`=?")) {
247             ps.setString(1, mail);
248             ps.setString(2, o_name);
249             ps.setString(3, p_address);
250             ps.setInt(4, getId());
251             ps.executeUpdate();
252         }
253         email = mail;
254         optionalName = o_name;
255         postalAddress = p_address;
256     }
257
258     public boolean isMaster(User u) {
259         for (Affiliation i : getAllAdmins()) {
260             if (i.isMaster() && i.getTarget() == u) {
261                 return true;
262             }
263         }
264         return false;
265     }
266
267     @Override
268     public boolean isValidEmail(String email) {
269         return isValidDomain(email.split("@", 2)[1]);
270     }
271
272     public static final String SELF_ORG_NAME = "SomeCA";
273
274     public boolean isSelfOrganisation() {
275         return SELF_ORG_NAME.equals(getName());
276     }
277
278     private void writeObject(ObjectOutputStream oos) throws IOException {}
279
280     private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {}
281
282 }