1 package org.cacert.gigi.dbObjects;
3 import java.util.ArrayList;
6 import org.cacert.gigi.GigiApiException;
7 import org.cacert.gigi.database.DatabaseConnection;
8 import org.cacert.gigi.database.GigiPreparedStatement;
9 import org.cacert.gigi.database.GigiResultSet;
10 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
12 public class Organisation extends CertificateOwner {
14 public class Affiliation {
16 private final User target;
18 private final boolean master;
20 private final String fixedOU;
22 public Affiliation(User target, boolean master, String fixedOU) {
25 this.fixedOU = fixedOU;
28 public User getTarget() {
32 public boolean isMaster() {
36 public String getFixedOU() {
40 public Organisation getOrganisation() {
41 return Organisation.this;
49 private String province;
55 public Organisation(String name, String state, String province, String city, String email, User creator) throws GigiApiException {
56 if ( !creator.isInGroup(Group.ORGASSURER)) {
57 throw new GigiApiException("Only org-assurers may create organisations.");
61 this.province = province;
64 int id = super.insert();
65 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, creator=?");
67 ps.setString(2, name);
68 ps.setString(3, state);
69 ps.setString(4, province);
70 ps.setString(5, city);
71 ps.setString(6, email);
72 ps.setInt(7, creator.getId());
73 synchronized (Organisation.class) {
79 protected Organisation(GigiResultSet rs) {
80 super(rs.getInt("id"));
81 name = rs.getString("name");
82 state = rs.getString("state");
83 province = rs.getString("province");
84 city = rs.getString("city");
85 email = rs.getString("contactEmail");
88 public String getName() {
92 public String getState() {
96 public String getProvince() {
100 public String getCity() {
104 public String getContactEmail() {
108 public static synchronized Organisation getById(int id) {
109 CertificateOwner co = CertificateOwner.getById(id);
110 if (co instanceof Organisation) {
111 return (Organisation) co;
116 public synchronized void addAdmin(User admin, User actor, boolean master) throws GigiApiException {
117 if ( !admin.canAssure()) {
118 throw new GigiApiException("Cannot add non-assurer.");
120 if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
121 throw new GigiApiException("Only org assurer or master-admin may add admins to an organisation.");
123 GigiPreparedStatement ps1 = DatabaseConnection.getInstance().prepare("SELECT 1 FROM org_admin WHERE orgid=? AND memid=? AND deleted is null");
124 ps1.setInt(1, getId());
125 ps1.setInt(2, admin.getId());
126 GigiResultSet result = ps1.executeQuery();
130 GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("INSERT INTO org_admin SET orgid=?, memid=?, creator=?, master=?");
131 ps2.setInt(1, getId());
132 ps2.setInt(2, admin.getId());
133 ps2.setInt(3, actor.getId());
134 ps2.setString(4, master ? "y" : "n");
138 public void removeAdmin(User admin, User actor) throws GigiApiException {
139 if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
140 throw new GigiApiException("Only org assurer or master-admin may delete admins from an organisation.");
142 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?");
143 ps.setInt(1, actor.getId());
144 ps.setInt(2, getId());
145 ps.setInt(3, admin.getId());
149 public List<Affiliation> getAllAdmins() {
150 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, master FROM org_admin WHERE orgid=? AND deleted is null");
151 ps.setInt(1, getId());
152 GigiResultSet rs = ps.executeQuery();
154 ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
157 al.add(new Affiliation(User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
162 public static Organisation[] getOrganisations(int offset, int count) {
163 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT certOwners.id FROM organisations inner join certOwners on certOwners.id=organisations.id where certOwners.deleted is null LIMIT ?,?");
164 ps.setInt(1, offset);
166 GigiResultSet res = ps.executeQuery();
168 Organisation[] resu = new Organisation[res.getRow()];
172 resu[i++] = getById(res.getInt(1));
177 public void update(String o, String c, String st, String l, String mail) {
178 for (Certificate cert : getCertificates(false)) {
179 if (cert.getStatus() == CertificateStatus.ISSUED) {
183 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE organisations SET name=?, state=?, province=?, city=?, contactEmail=?");
188 ps.setString(5, mail);
197 public boolean isMaster(User u) {
198 for (Affiliation i : getAllAdmins()) {
199 if (i.isMaster() && i.getTarget() == u) {
207 public boolean isValidEmail(String email) {
208 return isValidDomain(email.split("@", 2)[1]);