]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Organisation.java
b51d2838e4e045eb40296ed5c2dd301266a41f2e
[gigi.git] / src / org / cacert / gigi / dbObjects / Organisation.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
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;
11
12 public class Organisation extends CertificateOwner {
13
14     public class Affiliation {
15
16         private final User target;
17
18         private final boolean master;
19
20         private final String fixedOU;
21
22         public Affiliation(User target, boolean master, String fixedOU) {
23             this.target = target;
24             this.master = master;
25             this.fixedOU = fixedOU;
26         }
27
28         public User getTarget() {
29             return target;
30         }
31
32         public boolean isMaster() {
33             return master;
34         }
35
36         public String getFixedOU() {
37             return fixedOU;
38         }
39
40         public Organisation getOrganisation() {
41             return Organisation.this;
42         }
43     }
44
45     private String name;
46
47     private String state;
48
49     private String province;
50
51     private String city;
52
53     private String email;
54
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.");
58         }
59         this.name = name;
60         this.state = state;
61         this.province = province;
62         this.city = city;
63         this.email = email;
64         int id = getId();
65         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, creator=?");
66         ps.setInt(1, id);
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) {
74             ps.execute();
75         }
76
77     }
78
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");
86     }
87
88     public String getName() {
89         return name;
90     }
91
92     public String getState() {
93         return state;
94     }
95
96     public String getProvince() {
97         return province;
98     }
99
100     public String getCity() {
101         return city;
102     }
103
104     public String getContactEmail() {
105         return email;
106     }
107
108     public static synchronized Organisation getById(int id) {
109         CertificateOwner co = CertificateOwner.getById(id);
110         if (co instanceof Organisation) {
111             return (Organisation) co;
112         }
113         return null;
114     }
115
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.");
119         }
120         if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
121             throw new GigiApiException("Only org assurer or master-admin may add admins to an organisation.");
122         }
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();
127         if (result.next()) {
128             return;
129         }
130         GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("INSERT INTO `org_admin` SET `orgid`=?, `memid`=?, `creator`=?, `master`=?::`yesno`");
131         ps2.setInt(1, getId());
132         ps2.setInt(2, admin.getId());
133         ps2.setInt(3, actor.getId());
134         ps2.setString(4, master ? "y" : "n");
135         ps2.execute();
136     }
137
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.");
141         }
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());
146         ps.execute();
147     }
148
149     public List<Affiliation> getAllAdmins() {
150         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepareScrollable("SELECT `memid`, `master` FROM `org_admin` WHERE `orgid`=? AND `deleted` IS NULL");
151         ps.setInt(1, getId());
152         GigiResultSet rs = ps.executeQuery();
153         rs.last();
154         ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
155         rs.beforeFirst();
156         while (rs.next()) {
157             al.add(new Affiliation(User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
158         }
159         return al;
160     }
161
162     public static Organisation[] getOrganisations(int offset, int count) {
163         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepareScrollable("SELECT `certOwners`.`id` FROM `organisations` INNER JOIN `certOwners` ON `certOwners`.`id`=`organisations`.`id` WHERE `certOwners`.`deleted` IS NULL OFFSET ? LIMIT ?");
164         ps.setInt(1, offset);
165         ps.setInt(2, count);
166         GigiResultSet res = ps.executeQuery();
167         res.last();
168         Organisation[] resu = new Organisation[res.getRow()];
169         res.beforeFirst();
170         int i = 0;
171         while (res.next()) {
172             resu[i++] = getById(res.getInt(1));
173         }
174         return resu;
175     }
176
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) {
180                 cert.revoke();
181             }
182         }
183         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `organisations` SET `name`=?, `state`=?, `province`=?, `city`=?, `contactEmail`=?");
184         ps.setString(1, o);
185         ps.setString(2, c);
186         ps.setString(3, st);
187         ps.setString(4, l);
188         ps.setString(5, mail);
189         ps.execute();
190         email = mail;
191         name = o;
192         state = c;
193         province = st;
194         city = l;
195     }
196
197     public boolean isMaster(User u) {
198         for (Affiliation i : getAllAdmins()) {
199             if (i.isMaster() && i.getTarget() == u) {
200                 return true;
201             }
202         }
203         return false;
204     }
205
206     @Override
207     public boolean isValidEmail(String email) {
208         return isValidDomain(email.split("@", 2)[1]);
209     }
210 }