]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Organisation.java
fix: SQL change database call pattern
[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
11 public class Organisation extends CertificateOwner {
12
13     public class Affiliation {
14
15         private final User target;
16
17         private final boolean master;
18
19         private final String fixedOU;
20
21         public Affiliation(User target, boolean master, String fixedOU) {
22             this.target = target;
23             this.master = master;
24             this.fixedOU = fixedOU;
25         }
26
27         public User getTarget() {
28             return target;
29         }
30
31         public boolean isMaster() {
32             return master;
33         }
34
35         public String getFixedOU() {
36             return fixedOU;
37         }
38
39         public Organisation getOrganisation() {
40             return Organisation.this;
41         }
42     }
43
44     private String name;
45
46     private String state;
47
48     private String province;
49
50     private String city;
51
52     private String email;
53
54     public Organisation(String name, String state, String province, String city, String email, User creator) throws GigiApiException {
55         if ( !creator.isInGroup(Group.ORGASSURER)) {
56             throw new GigiApiException("Only org-assurers may create organisations.");
57         }
58         this.name = name;
59         this.state = state;
60         this.province = province;
61         this.city = city;
62         this.email = email;
63         int id = getId();
64         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, creator=?")) {
65             ps.setInt(1, id);
66             ps.setString(2, name);
67             ps.setString(3, state);
68             ps.setString(4, province);
69             ps.setString(5, city);
70             ps.setString(6, email);
71             ps.setInt(7, creator.getId());
72             synchronized (Organisation.class) {
73                 ps.execute();
74             }
75         }
76     }
77
78     protected Organisation(GigiResultSet rs) {
79         super(rs.getInt("id"));
80         name = rs.getString("name");
81         state = rs.getString("state");
82         province = rs.getString("province");
83         city = rs.getString("city");
84         email = rs.getString("contactEmail");
85     }
86
87     public String getName() {
88         return name;
89     }
90
91     public String getState() {
92         return state;
93     }
94
95     public String getProvince() {
96         return province;
97     }
98
99     public String getCity() {
100         return city;
101     }
102
103     public String getContactEmail() {
104         return email;
105     }
106
107     public static synchronized Organisation getById(int id) {
108         CertificateOwner co = CertificateOwner.getById(id);
109         if (co instanceof Organisation) {
110             return (Organisation) co;
111         }
112         return null;
113     }
114
115     public synchronized void addAdmin(User admin, User actor, boolean master) throws GigiApiException {
116         if ( !admin.canAssure()) {
117             throw new GigiApiException("Cannot add non-assurer.");
118         }
119         if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
120             throw new GigiApiException("Only org assurer or master-admin may add admins to an organisation.");
121         }
122         try (GigiPreparedStatement ps1 = new GigiPreparedStatement("SELECT 1 FROM `org_admin` WHERE `orgid`=? AND `memid`=? AND `deleted` IS NULL")) {
123             ps1.setInt(1, getId());
124             ps1.setInt(2, admin.getId());
125             GigiResultSet result = ps1.executeQuery();
126             if (result.next()) {
127                 return;
128             }
129         }
130         try (GigiPreparedStatement ps2 = new GigiPreparedStatement("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
139     public void removeAdmin(User admin, User actor) throws GigiApiException {
140         if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
141             throw new GigiApiException("Only org assurer or master-admin may delete admins from an organisation.");
142         }
143         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?")) {
144             ps.setInt(1, actor.getId());
145             ps.setInt(2, getId());
146             ps.setInt(3, admin.getId());
147             ps.execute();
148         }
149     }
150
151     public List<Affiliation> getAllAdmins() {
152         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `master` FROM `org_admin` WHERE `orgid`=? AND `deleted` IS NULL", true)) {
153             ps.setInt(1, getId());
154             GigiResultSet rs = ps.executeQuery();
155             rs.last();
156             ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
157             rs.beforeFirst();
158             while (rs.next()) {
159                 al.add(new Affiliation(User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
160             }
161             return al;
162         }
163     }
164
165     public static Organisation[] getOrganisations(int offset, int count) {
166         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)) {
167             ps.setInt(1, offset);
168             ps.setInt(2, count);
169             GigiResultSet res = ps.executeQuery();
170             res.last();
171             Organisation[] resu = new Organisation[res.getRow()];
172             res.beforeFirst();
173             int i = 0;
174             while (res.next()) {
175                 resu[i++] = getById(res.getInt(1));
176             }
177             return resu;
178         }
179     }
180
181     public void update(String o, String c, String st, String l, String mail) {
182         for (Certificate cert : getCertificates(false)) {
183             if (cert.getStatus() == CertificateStatus.ISSUED) {
184                 cert.revoke();
185             }
186         }
187         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `state`=?, `province`=?, `city`=?, `contactEmail`=?")) {
188             ps.setString(1, o);
189             ps.setString(2, c);
190             ps.setString(3, st);
191             ps.setString(4, l);
192             ps.setString(5, mail);
193             ps.execute();
194         }
195         email = mail;
196         name = o;
197         state = c;
198         province = st;
199         city = l;
200     }
201
202     public boolean isMaster(User u) {
203         for (Affiliation i : getAllAdmins()) {
204             if (i.isMaster() && i.getTarget() == u) {
205                 return true;
206             }
207         }
208         return false;
209     }
210
211     @Override
212     public boolean isValidEmail(String email) {
213         return isValidDomain(email.split("@", 2)[1]);
214     }
215 }