]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Organisation.java
Merge "Update notes about password security"
[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     private String optionalName;
60
61     private String postalAddress;
62
63     public Organisation(String name, String state, String province, String city, String email, String optionalName, String postalAddress, User creator) throws GigiApiException {
64         if ( !creator.isInGroup(Group.ORGASSURER)) {
65             throw new GigiApiException("Only org-assurers may create organisations.");
66         }
67         this.name = name;
68         this.state = state;
69         this.province = province;
70         this.city = city;
71         this.email = email;
72         this.optionalName = optionalName;
73         this.postalAddress = postalAddress;
74         int id = getId();
75         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, optional_name=?, postal_address=?, creator=?")) {
76             ps.setInt(1, id);
77             ps.setString(2, name);
78             ps.setString(3, state);
79             ps.setString(4, province);
80             ps.setString(5, city);
81             ps.setString(6, email);
82             ps.setString(7, optionalName);
83             ps.setString(8, postalAddress);
84             ps.setInt(9, creator.getId());
85             synchronized (Organisation.class) {
86                 ps.execute();
87             }
88         }
89     }
90
91     protected Organisation(GigiResultSet rs) {
92         super(rs.getInt("id"));
93         name = rs.getString("name");
94         state = rs.getString("state");
95         province = rs.getString("province");
96         city = rs.getString("city");
97         email = rs.getString("contactEmail");
98         optionalName = rs.getString("optional_name");
99         postalAddress = rs.getString("postal_address");
100     }
101
102     public String getName() {
103         return name;
104     }
105
106     public String getState() {
107         return state;
108     }
109
110     public String getProvince() {
111         return province;
112     }
113
114     public String getCity() {
115         return city;
116     }
117
118     public String getContactEmail() {
119         return email;
120     }
121
122     public String getOptionalName() {
123         return optionalName;
124     }
125
126     public String getPostalAddress() {
127         return postalAddress;
128     }
129
130     public static synchronized Organisation getById(int id) {
131         CertificateOwner co = CertificateOwner.getById(id);
132         if (co instanceof Organisation) {
133             return (Organisation) co;
134         }
135         throw new IllegalArgumentException("Organisation not found.");
136     }
137
138     public synchronized void addAdmin(User admin, User actor, boolean master) throws GigiApiException {
139         if ( !admin.canAssure()) {
140             throw new GigiApiException("Cannot add non-assurer.");
141         }
142         if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
143             throw new GigiApiException("Only org assurer or master-admin may add admins to an organisation.");
144         }
145         try (GigiPreparedStatement ps1 = new GigiPreparedStatement("SELECT 1 FROM `org_admin` WHERE `orgid`=? AND `memid`=? AND `deleted` IS NULL")) {
146             ps1.setInt(1, getId());
147             ps1.setInt(2, admin.getId());
148             GigiResultSet result = ps1.executeQuery();
149             if (result.next()) {
150                 return;
151             }
152         }
153         try (GigiPreparedStatement ps2 = new GigiPreparedStatement("INSERT INTO `org_admin` SET `orgid`=?, `memid`=?, `creator`=?, `master`=?::`yesno`")) {
154             ps2.setInt(1, getId());
155             ps2.setInt(2, admin.getId());
156             ps2.setInt(3, actor.getId());
157             ps2.setString(4, master ? "y" : "n");
158             ps2.execute();
159         }
160     }
161
162     public void removeAdmin(User admin, User actor) throws GigiApiException {
163         if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
164             throw new GigiApiException("Only org assurer or master-admin may delete admins from an organisation.");
165         }
166         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?")) {
167             ps.setInt(1, actor.getId());
168             ps.setInt(2, getId());
169             ps.setInt(3, admin.getId());
170             ps.execute();
171         }
172     }
173
174     public List<Affiliation> getAllAdmins() {
175         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `master` FROM `org_admin` WHERE `orgid`=? AND `deleted` IS NULL", true)) {
176             ps.setInt(1, getId());
177             GigiResultSet rs = ps.executeQuery();
178             rs.last();
179             ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
180             rs.beforeFirst();
181             while (rs.next()) {
182                 al.add(new Affiliation(this, User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
183             }
184             return al;
185         }
186     }
187
188     public static Organisation[] getOrganisations(int offset, int count) {
189         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)) {
190             ps.setInt(1, offset);
191             ps.setInt(2, count);
192             GigiResultSet res = ps.executeQuery();
193             res.last();
194             Organisation[] resu = new Organisation[res.getRow()];
195             res.beforeFirst();
196             int i = 0;
197             while (res.next()) {
198                 resu[i++] = getById(res.getInt(1));
199             }
200             return resu;
201         }
202     }
203
204     public void updateCertData(String o, String c, String st, String l) {
205         for (Certificate cert : getCertificates(false)) {
206             if (cert.getStatus() == CertificateStatus.ISSUED) {
207                 cert.revoke();
208             }
209         }
210         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `state`=?, `province`=?, `city`=? WHERE `id`=?")) {
211             ps.setString(1, o);
212             ps.setString(2, c);
213             ps.setString(3, st);
214             ps.setString(4, l);
215             ps.setInt(5, getId());
216             ps.executeUpdate();
217         }
218         name = o;
219         state = c;
220         province = st;
221         city = l;
222     }
223
224     public void updateOrgData(String mail, String o_name, String p_address) {
225         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `contactEmail`=?, `optional_name`=?, `postal_address`=? WHERE `id`=?")) {
226             ps.setString(1, mail);
227             ps.setString(2, o_name);
228             ps.setString(3, p_address);
229             ps.setInt(4, getId());
230             ps.executeUpdate();
231         }
232         email = mail;
233         optionalName = o_name;
234         postalAddress = p_address;
235     }
236
237     public boolean isMaster(User u) {
238         for (Affiliation i : getAllAdmins()) {
239             if (i.isMaster() && i.getTarget() == u) {
240                 return true;
241             }
242         }
243         return false;
244     }
245
246     @Override
247     public boolean isValidEmail(String email) {
248         return isValidDomain(email.split("@", 2)[1]);
249     }
250
251     public static final String SELF_ORG_NAME = "SomeCA";
252
253     public boolean isSelfOrganisation() {
254         return SELF_ORG_NAME.equals(getName());
255     }
256 }