1 package org.cacert.gigi.dbObjects;
3 import java.util.ArrayList;
6 import org.cacert.gigi.GigiApiException;
7 import org.cacert.gigi.database.GigiPreparedStatement;
8 import org.cacert.gigi.database.GigiResultSet;
9 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
10 import org.cacert.gigi.dbObjects.wrappers.DataContainer;
12 public class Organisation extends CertificateOwner {
15 public static class Affiliation {
17 private final User target;
19 private final boolean master;
21 private final String fixedOU;
23 private Organisation o;
25 public Affiliation(Organisation o, User target, boolean master, String fixedOU) {
29 this.fixedOU = fixedOU;
32 public User getTarget() {
36 public boolean isMaster() {
40 public String getFixedOU() {
44 public Organisation getOrganisation() {
53 private String province;
59 public Organisation(String name, String state, String province, String city, String email, User creator) throws GigiApiException {
60 if ( !creator.isInGroup(Group.ORGASSURER)) {
61 throw new GigiApiException("Only org-assurers may create organisations.");
65 this.province = province;
69 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, creator=?")) {
71 ps.setString(2, name);
72 ps.setString(3, state);
73 ps.setString(4, province);
74 ps.setString(5, city);
75 ps.setString(6, email);
76 ps.setInt(7, creator.getId());
77 synchronized (Organisation.class) {
83 protected Organisation(GigiResultSet rs) {
84 super(rs.getInt("id"));
85 name = rs.getString("name");
86 state = rs.getString("state");
87 province = rs.getString("province");
88 city = rs.getString("city");
89 email = rs.getString("contactEmail");
92 public String getName() {
96 public String getState() {
100 public String getProvince() {
104 public String getCity() {
108 public String getContactEmail() {
112 public static synchronized Organisation getById(int id) {
113 CertificateOwner co = CertificateOwner.getById(id);
114 if (co instanceof Organisation) {
115 return (Organisation) co;
117 throw new IllegalArgumentException("Organisation not found.");
120 public synchronized void addAdmin(User admin, User actor, boolean master) throws GigiApiException {
121 if ( !admin.canAssure()) {
122 throw new GigiApiException("Cannot add non-assurer.");
124 if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
125 throw new GigiApiException("Only org assurer or master-admin may add admins to an organisation.");
127 try (GigiPreparedStatement ps1 = new GigiPreparedStatement("SELECT 1 FROM `org_admin` WHERE `orgid`=? AND `memid`=? AND `deleted` IS NULL")) {
128 ps1.setInt(1, getId());
129 ps1.setInt(2, admin.getId());
130 GigiResultSet result = ps1.executeQuery();
135 try (GigiPreparedStatement ps2 = new GigiPreparedStatement("INSERT INTO `org_admin` SET `orgid`=?, `memid`=?, `creator`=?, `master`=?::`yesno`")) {
136 ps2.setInt(1, getId());
137 ps2.setInt(2, admin.getId());
138 ps2.setInt(3, actor.getId());
139 ps2.setString(4, master ? "y" : "n");
144 public void removeAdmin(User admin, User actor) throws GigiApiException {
145 if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
146 throw new GigiApiException("Only org assurer or master-admin may delete admins from an organisation.");
148 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?")) {
149 ps.setInt(1, actor.getId());
150 ps.setInt(2, getId());
151 ps.setInt(3, admin.getId());
156 public List<Affiliation> getAllAdmins() {
157 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `master` FROM `org_admin` WHERE `orgid`=? AND `deleted` IS NULL", true)) {
158 ps.setInt(1, getId());
159 GigiResultSet rs = ps.executeQuery();
161 ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
164 al.add(new Affiliation(this, User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
170 public static Organisation[] getOrganisations(int offset, int count) {
171 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)) {
172 ps.setInt(1, offset);
174 GigiResultSet res = ps.executeQuery();
176 Organisation[] resu = new Organisation[res.getRow()];
180 resu[i++] = getById(res.getInt(1));
186 public void update(String o, String c, String st, String l, String mail) {
187 for (Certificate cert : getCertificates(false)) {
188 if (cert.getStatus() == CertificateStatus.ISSUED) {
192 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `state`=?, `province`=?, `city`=?, `contactEmail`=?")) {
197 ps.setString(5, mail);
207 public boolean isMaster(User u) {
208 for (Affiliation i : getAllAdmins()) {
209 if (i.isMaster() && i.getTarget() == u) {
217 public boolean isValidEmail(String email) {
218 return isValidDomain(email.split("@", 2)[1]);
221 public static final String SELF_ORG_NAME = "CAcert";
223 public boolean isSelfOrganisation() {
224 return SELF_ORG_NAME.equals(getName());