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 if ( !creator.isInGroup(Group.ORG_AGENT)) {
72 throw new GigiApiException("Only Organisation RA Agents may create organisations.");
74 if (country == null) {
75 throw new GigiApiException("Got country code of illegal type.");
78 this.country = country;
79 this.province = province;
82 this.optionalName = optionalName;
83 this.postalAddress = postalAddress;
85 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO organisations SET id=?, name=?, country=?, province=?, city=?, contactEmail=?, optional_name=?, postal_address=?, creator=?")) {
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) {
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");
112 public String getName() {
116 public Country getCountry() {
120 public String getProvince() {
124 public String getCity() {
128 public String getContactEmail() {
132 public String getOptionalName() {
136 public String getPostalAddress() {
137 return postalAddress;
140 public static synchronized Organisation getById(int id) {
141 CertificateOwner co = CertificateOwner.getById(id);
142 if (co instanceof Organisation) {
143 return (Organisation) co;
145 throw new IllegalArgumentException("Organisation not found.");
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.");
152 if ( !admin.canVerify()) {
153 throw new GigiApiException("Cannot add person who is not RA Agent.");
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.");
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();
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");
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.");
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());
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();
192 ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
195 al.add(new Affiliation(this, User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
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);
205 GigiResultSet res = ps.executeQuery();
207 Organisation[] resu = new Organisation[res.getRow()];
211 resu[i++] = getById(res.getInt(1));
217 public void updateCertData(String o, Country c, String st, String l) throws GigiApiException {
219 throw new GigiApiException("Got country code of illegal type.");
221 for (Certificate cert : getCertificates(false)) {
222 if (cert.getStatus() == CertificateStatus.ISSUED) {
223 cert.revoke(RevocationType.USER);
226 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `country`=?, `province`=?, `city`=? WHERE `id`=?")) {
228 ps.setString(2, c.getCode());
231 ps.setInt(5, getId());
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());
249 optionalName = o_name;
250 postalAddress = p_address;
253 public boolean isMaster(User u) {
254 for (Affiliation i : getAllAdmins()) {
255 if (i.isMaster() && i.getTarget() == u) {
263 public boolean isValidEmail(String email) {
264 return isValidDomain(email.split("@", 2)[1]);
267 public static final String SELF_ORG_NAME = "SomeCA";
269 public boolean isSelfOrganisation() {
270 return SELF_ORG_NAME.equals(getName());
273 private void writeObject(ObjectOutputStream oos) throws IOException {}
275 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {}