1 package org.cacert.gigi.dbObjects;
3 import java.io.IOException;
4 import java.io.ObjectInputStream;
5 import java.io.ObjectOutputStream;
6 import java.util.ArrayList;
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.database.GigiPreparedStatement;
11 import org.cacert.gigi.database.GigiResultSet;
12 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
13 import org.cacert.gigi.dbObjects.wrappers.DataContainer;
15 public class Organisation extends CertificateOwner {
17 private static final long serialVersionUID = -2386342985586320843L;
20 public static class Affiliation {
22 private final User target;
24 private final boolean master;
26 private final String fixedOU;
28 private Organisation o;
30 public Affiliation(Organisation o, User target, boolean master, String fixedOU) {
34 this.fixedOU = fixedOU;
37 public User getTarget() {
41 public boolean isMaster() {
45 public String getFixedOU() {
49 public Organisation getOrganisation() {
58 private String province;
64 private String optionalName;
66 private String postalAddress;
68 public Organisation(String name, CountryCode state, String province, String city, String email, String optionalName, String postalAddress, User creator) throws GigiApiException {
69 if ( !creator.isInGroup(Group.ORGASSURER)) {
70 throw new GigiApiException("Only Organisation RA Agents may create organisations.");
73 this.state = state.getCountryCode();
74 this.province = province;
77 this.optionalName = optionalName;
78 this.postalAddress = postalAddress;
80 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, optional_name=?, postal_address=?, creator=?")) {
82 ps.setString(2, name);
83 ps.setString(3, state.getCountryCode());
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 protected Organisation(GigiResultSet rs) {
97 super(rs.getInt("id"));
98 name = rs.getString("name");
99 state = rs.getString("state");
100 province = rs.getString("province");
101 city = rs.getString("city");
102 email = rs.getString("contactEmail");
103 optionalName = rs.getString("optional_name");
104 postalAddress = rs.getString("postal_address");
107 public String getName() {
111 public String getState() {
115 public String getProvince() {
119 public String getCity() {
123 public String getContactEmail() {
127 public String getOptionalName() {
131 public String getPostalAddress() {
132 return postalAddress;
135 public static synchronized Organisation getById(int id) {
136 CertificateOwner co = CertificateOwner.getById(id);
137 if (co instanceof Organisation) {
138 return (Organisation) co;
140 throw new IllegalArgumentException("Organisation not found.");
143 public synchronized void addAdmin(User admin, User actor, boolean master) throws GigiApiException {
144 if ( !admin.canAssure()) {
145 throw new GigiApiException("Cannot add person who is not RA Agent.");
147 if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
148 throw new GigiApiException("Only Organisation RA Agents or Organisation Administrators may add admins to an organisation.");
150 try (GigiPreparedStatement ps1 = new GigiPreparedStatement("SELECT 1 FROM `org_admin` WHERE `orgid`=? AND `memid`=? AND `deleted` IS NULL")) {
151 ps1.setInt(1, getId());
152 ps1.setInt(2, admin.getId());
153 GigiResultSet result = ps1.executeQuery();
158 try (GigiPreparedStatement ps2 = new GigiPreparedStatement("INSERT INTO `org_admin` SET `orgid`=?, `memid`=?, `creator`=?, `master`=?::`yesno`")) {
159 ps2.setInt(1, getId());
160 ps2.setInt(2, admin.getId());
161 ps2.setInt(3, actor.getId());
162 ps2.setString(4, master ? "y" : "n");
167 public void removeAdmin(User admin, User actor) throws GigiApiException {
168 if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
169 throw new GigiApiException("Only Organisation RA Agents or Organisation Administrators may delete admins from an organisation.");
171 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?")) {
172 ps.setInt(1, actor.getId());
173 ps.setInt(2, getId());
174 ps.setInt(3, admin.getId());
179 public List<Affiliation> getAllAdmins() {
180 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `master` FROM `org_admin` WHERE `orgid`=? AND `deleted` IS NULL", true)) {
181 ps.setInt(1, getId());
182 GigiResultSet rs = ps.executeQuery();
184 ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
187 al.add(new Affiliation(this, User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
193 public static Organisation[] getOrganisations(int offset, int count) {
194 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)) {
195 ps.setInt(1, offset);
197 GigiResultSet res = ps.executeQuery();
199 Organisation[] resu = new Organisation[res.getRow()];
203 resu[i++] = getById(res.getInt(1));
209 public void updateCertData(String o, CountryCode c, String st, String l) {
210 for (Certificate cert : getCertificates(false)) {
211 if (cert.getStatus() == CertificateStatus.ISSUED) {
215 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `state`=?, `province`=?, `city`=? WHERE `id`=?")) {
217 ps.setString(2, c.getCountryCode());
220 ps.setInt(5, getId());
224 state = c.getCountryCode();
229 public void updateOrgData(String mail, String o_name, String p_address) {
230 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `contactEmail`=?, `optional_name`=?, `postal_address`=? WHERE `id`=?")) {
231 ps.setString(1, mail);
232 ps.setString(2, o_name);
233 ps.setString(3, p_address);
234 ps.setInt(4, getId());
238 optionalName = o_name;
239 postalAddress = p_address;
242 public boolean isMaster(User u) {
243 for (Affiliation i : getAllAdmins()) {
244 if (i.isMaster() && i.getTarget() == u) {
252 public boolean isValidEmail(String email) {
253 return isValidDomain(email.split("@", 2)[1]);
256 public static final String SELF_ORG_NAME = "SomeCA";
258 public boolean isSelfOrganisation() {
259 return SELF_ORG_NAME.equals(getName());
262 private void writeObject(ObjectOutputStream oos) throws IOException {}
264 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {}