]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Organisation.java
upd: allow signing of OCSP-Certs for internal use
[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.GigiPreparedStatement;
8 import org.cacert.gigi.database.GigiResultSet;
9 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
10 import org.cacert.gigi.dbObjects.wrappers.DataContainer;
11
12 public class Organisation extends CertificateOwner {
13
14     @DataContainer
15     public static class Affiliation {
16
17         private final User target;
18
19         private final boolean master;
20
21         private final String fixedOU;
22
23         private Organisation o;
24
25         public Affiliation(Organisation o, User target, boolean master, String fixedOU) {
26             this.o = o;
27             this.target = target;
28             this.master = master;
29             this.fixedOU = fixedOU;
30         }
31
32         public User getTarget() {
33             return target;
34         }
35
36         public boolean isMaster() {
37             return master;
38         }
39
40         public String getFixedOU() {
41             return fixedOU;
42         }
43
44         public Organisation getOrganisation() {
45             return o;
46         }
47     }
48
49     private String name;
50
51     private String state;
52
53     private String province;
54
55     private String city;
56
57     private String email;
58
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.");
62         }
63         this.name = name;
64         this.state = state;
65         this.province = province;
66         this.city = city;
67         this.email = email;
68         int id = getId();
69         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, creator=?")) {
70             ps.setInt(1, id);
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) {
78                 ps.execute();
79             }
80         }
81     }
82
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");
90     }
91
92     public String getName() {
93         return name;
94     }
95
96     public String getState() {
97         return state;
98     }
99
100     public String getProvince() {
101         return province;
102     }
103
104     public String getCity() {
105         return city;
106     }
107
108     public String getContactEmail() {
109         return email;
110     }
111
112     public static synchronized Organisation getById(int id) {
113         CertificateOwner co = CertificateOwner.getById(id);
114         if (co instanceof Organisation) {
115             return (Organisation) co;
116         }
117         throw new IllegalArgumentException("Organisation not found.");
118     }
119
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.");
123         }
124         if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
125             throw new GigiApiException("Only org assurer or master-admin may add admins to an organisation.");
126         }
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();
131             if (result.next()) {
132                 return;
133             }
134         }
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");
140             ps2.execute();
141         }
142     }
143
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.");
147         }
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());
152             ps.execute();
153         }
154     }
155
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();
160             rs.last();
161             ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
162             rs.beforeFirst();
163             while (rs.next()) {
164                 al.add(new Affiliation(this, User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
165             }
166             return al;
167         }
168     }
169
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);
173             ps.setInt(2, count);
174             GigiResultSet res = ps.executeQuery();
175             res.last();
176             Organisation[] resu = new Organisation[res.getRow()];
177             res.beforeFirst();
178             int i = 0;
179             while (res.next()) {
180                 resu[i++] = getById(res.getInt(1));
181             }
182             return resu;
183         }
184     }
185
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) {
189                 cert.revoke();
190             }
191         }
192         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `state`=?, `province`=?, `city`=?, `contactEmail`=?")) {
193             ps.setString(1, o);
194             ps.setString(2, c);
195             ps.setString(3, st);
196             ps.setString(4, l);
197             ps.setString(5, mail);
198             ps.execute();
199         }
200         email = mail;
201         name = o;
202         state = c;
203         province = st;
204         city = l;
205     }
206
207     public boolean isMaster(User u) {
208         for (Affiliation i : getAllAdmins()) {
209             if (i.isMaster() && i.getTarget() == u) {
210                 return true;
211             }
212         }
213         return false;
214     }
215
216     @Override
217     public boolean isValidEmail(String email) {
218         return isValidDomain(email.split("@", 2)[1]);
219     }
220
221     public boolean isSelfOrganisation() {
222         return "CAcert".equals(getName());
223     }
224 }