1 package club.wpia.gigi.dbObjects;
3 import java.io.IOException;
4 import java.io.ObjectInputStream;
5 import java.io.ObjectOutputStream;
6 import java.util.ArrayList;
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;
17 public class Organisation extends CertificateOwner {
19 private static final long serialVersionUID = -2386342985586320843L;
22 public static class Affiliation {
24 private final User target;
26 private final boolean master;
28 private final String fixedOU;
30 private Organisation o;
32 public Affiliation(Organisation o, User target, boolean master, String fixedOU) {
36 this.fixedOU = fixedOU;
39 public User getTarget() {
43 public boolean isMaster() {
47 public String getFixedOU() {
51 public Organisation getOrganisation() {
58 private Country country;
60 private String province;
66 private String optionalName;
68 private String postalAddress;
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));
73 this.country = country;
74 this.province = province;
77 this.optionalName = optionalName;
78 this.postalAddress = postalAddress;
80 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO organisations SET id=?, name=?, country=?, province=?, city=?, contactEmail=?, optional_name=?, postal_address=?, creator=?")) {
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) {
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.");
100 if (country == null) {
101 throw new GigiApiException("Got country code of illegal type.");
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");
117 public String getName() {
121 public Country getCountry() {
125 public String getProvince() {
129 public String getCity() {
133 public String getContactEmail() {
137 public String getOptionalName() {
141 public String getPostalAddress() {
142 return postalAddress;
145 public static synchronized Organisation getById(int id) {
146 CertificateOwner co = CertificateOwner.getById(id);
147 if (co instanceof Organisation) {
148 return (Organisation) co;
150 throw new IllegalArgumentException("Organisation not found.");
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.");
157 if ( !admin.canVerify()) {
158 throw new GigiApiException("Cannot add person who is not RA Agent.");
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.");
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();
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");
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.");
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());
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();
197 ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
200 al.add(new Affiliation(this, User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
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);
210 GigiResultSet res = ps.executeQuery();
212 Organisation[] resu = new Organisation[res.getRow()];
216 resu[i++] = getById(res.getInt(1));
222 public void updateCertData(String o, Country c, String st, String l) throws GigiApiException {
224 throw new GigiApiException("Got country code of illegal type.");
226 for (Certificate cert : getCertificates(false)) {
227 if (cert.getStatus() == CertificateStatus.ISSUED) {
228 cert.revoke(RevocationType.USER);
231 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `country`=?, `province`=?, `city`=? WHERE `id`=?")) {
233 ps.setString(2, c.getCode());
236 ps.setInt(5, getId());
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());
254 optionalName = o_name;
255 postalAddress = p_address;
258 public boolean isMaster(User u) {
259 for (Affiliation i : getAllAdmins()) {
260 if (i.isMaster() && i.getTarget() == u) {
268 public boolean isValidEmail(String email) {
269 return isValidDomain(email.split("@", 2)[1]);
272 public static final String SELF_ORG_NAME = "SomeCA";
274 public boolean isSelfOrganisation() {
275 return SELF_ORG_NAME.equals(getName());
278 private void writeObject(ObjectOutputStream oos) throws IOException {}
280 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {}