]> WPIA git - gigi.git/blob - src/club/wpia/gigi/dbObjects/Organisation.java
add: Org Agents may not make themselves administrators of organisations.
[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.Country.CountryCodeType;
14 import club.wpia.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.ORG_AGENT)) {
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 (actor == admin) {
149             throw new GigiApiException("You may not add yourself as Organisation Admin. Ask another Organisation Agent to do so.");
150         }
151         if ( !admin.canVerify()) {
152             throw new GigiApiException("Cannot add person who is not RA Agent.");
153         }
154         if ( !actor.isInGroup(Group.ORG_AGENT) && !isMaster(actor)) {
155             throw new GigiApiException("Only Organisation RA Agents or Organisation Administrators may add admins to an organisation.");
156         }
157         try (GigiPreparedStatement ps1 = new GigiPreparedStatement("SELECT 1 FROM `org_admin` WHERE `orgid`=? AND `memid`=? AND `deleted` IS NULL")) {
158             ps1.setInt(1, getId());
159             ps1.setInt(2, admin.getId());
160             GigiResultSet result = ps1.executeQuery();
161             if (result.next()) {
162                 return;
163             }
164         }
165         try (GigiPreparedStatement ps2 = new GigiPreparedStatement("INSERT INTO `org_admin` SET `orgid`=?, `memid`=?, `creator`=?, `master`=?::`yesno`")) {
166             ps2.setInt(1, getId());
167             ps2.setInt(2, admin.getId());
168             ps2.setInt(3, actor.getId());
169             ps2.setString(4, master ? "y" : "n");
170             ps2.execute();
171         }
172     }
173
174     public void removeAdmin(User admin, User actor) throws GigiApiException {
175         if ( !actor.isInGroup(Group.ORG_AGENT) && !isMaster(actor)) {
176             throw new GigiApiException("Only Organisation RA Agents or Organisation Administrators may delete admins from an organisation.");
177         }
178         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?")) {
179             ps.setInt(1, actor.getId());
180             ps.setInt(2, getId());
181             ps.setInt(3, admin.getId());
182             ps.execute();
183         }
184     }
185
186     public List<Affiliation> getAllAdmins() {
187         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `master` FROM `org_admin` WHERE `orgid`=? AND `deleted` IS NULL", true)) {
188             ps.setInt(1, getId());
189             GigiResultSet rs = ps.executeQuery();
190             rs.last();
191             ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
192             rs.beforeFirst();
193             while (rs.next()) {
194                 al.add(new Affiliation(this, User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
195             }
196             return al;
197         }
198     }
199
200     public static Organisation[] getOrganisations(int offset, int count) {
201         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)) {
202             ps.setInt(1, offset);
203             ps.setInt(2, count);
204             GigiResultSet res = ps.executeQuery();
205             res.last();
206             Organisation[] resu = new Organisation[res.getRow()];
207             res.beforeFirst();
208             int i = 0;
209             while (res.next()) {
210                 resu[i++] = getById(res.getInt(1));
211             }
212             return resu;
213         }
214     }
215
216     public void updateCertData(String o, Country c, String st, String l) throws GigiApiException {
217         if (c == null) {
218             throw new GigiApiException("Got country code of illegal type.");
219         }
220         for (Certificate cert : getCertificates(false)) {
221             if (cert.getStatus() == CertificateStatus.ISSUED) {
222                 cert.revoke();
223             }
224         }
225         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `country`=?, `province`=?, `city`=? WHERE `id`=?")) {
226             ps.setString(1, o);
227             ps.setString(2, c.getCode());
228             ps.setString(3, st);
229             ps.setString(4, l);
230             ps.setInt(5, getId());
231             ps.executeUpdate();
232         }
233         name = o;
234         country = c;
235         province = st;
236         city = l;
237     }
238
239     public void updateOrgData(String mail, String o_name, String p_address) {
240         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `contactEmail`=?, `optional_name`=?, `postal_address`=? WHERE `id`=?")) {
241             ps.setString(1, mail);
242             ps.setString(2, o_name);
243             ps.setString(3, p_address);
244             ps.setInt(4, getId());
245             ps.executeUpdate();
246         }
247         email = mail;
248         optionalName = o_name;
249         postalAddress = p_address;
250     }
251
252     public boolean isMaster(User u) {
253         for (Affiliation i : getAllAdmins()) {
254             if (i.isMaster() && i.getTarget() == u) {
255                 return true;
256             }
257         }
258         return false;
259     }
260
261     @Override
262     public boolean isValidEmail(String email) {
263         return isValidDomain(email.split("@", 2)[1]);
264     }
265
266     public static final String SELF_ORG_NAME = "SomeCA";
267
268     public boolean isSelfOrganisation() {
269         return SELF_ORG_NAME.equals(getName());
270     }
271
272     private void writeObject(ObjectOutputStream oos) throws IOException {}
273
274     private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {}
275
276 }