import java.util.ArrayList;
import java.util.List;
+import org.cacert.gigi.GigiApiException;
import org.cacert.gigi.database.DatabaseConnection;
import org.cacert.gigi.database.GigiPreparedStatement;
import org.cacert.gigi.database.GigiResultSet;
private String email;
- public Organisation(String name, String state, String province, String city, String email, User creator) {
+ public Organisation(String name, String state, String province, String city, String email, User creator) throws GigiApiException {
+ if ( !creator.isInGroup(Group.ORGASSURER)) {
+ throw new GigiApiException("Only org-assurers may create organisations.");
+ }
this.name = name;
this.state = state;
this.province = province;
this.city = city;
this.email = email;
- int id = super.insert();
+ int id = getId();
GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, creator=?");
ps.setInt(1, id);
ps.setString(2, name);
return null;
}
- public synchronized void addAdmin(User admin, User actor, boolean master) {
- GigiPreparedStatement ps1 = DatabaseConnection.getInstance().prepare("SELECT 1 FROM org_admin WHERE orgid=? AND memid=? AND deleted is null");
+ public synchronized void addAdmin(User admin, User actor, boolean master) throws GigiApiException {
+ if ( !admin.canAssure()) {
+ throw new GigiApiException("Cannot add non-assurer.");
+ }
+ if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
+ throw new GigiApiException("Only org assurer or master-admin may add admins to an organisation.");
+ }
+ GigiPreparedStatement ps1 = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `org_admin` WHERE `orgid`=? AND `memid`=? AND `deleted` IS NULL");
ps1.setInt(1, getId());
ps1.setInt(2, admin.getId());
GigiResultSet result = ps1.executeQuery();
if (result.next()) {
return;
}
- GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("INSERT INTO org_admin SET orgid=?, memid=?, creator=?, master=?");
+ GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("INSERT INTO `org_admin` SET `orgid`=?, `memid`=?, `creator`=?, `master`=?::`yesno`");
ps2.setInt(1, getId());
ps2.setInt(2, admin.getId());
ps2.setInt(3, actor.getId());
ps2.execute();
}
- public void removeAdmin(User admin, User actor) {
+ public void removeAdmin(User admin, User actor) throws GigiApiException {
+ if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
+ throw new GigiApiException("Only org assurer or master-admin may delete admins from an organisation.");
+ }
GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?");
ps.setInt(1, actor.getId());
ps.setInt(2, getId());
}
public List<Affiliation> getAllAdmins() {
- GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, master FROM org_admin WHERE orgid=? AND deleted is null");
+ GigiPreparedStatement ps = DatabaseConnection.getInstance().prepareScrollable("SELECT `memid`, `master` FROM `org_admin` WHERE `orgid`=? AND `deleted` IS NULL");
ps.setInt(1, getId());
GigiResultSet rs = ps.executeQuery();
rs.last();
}
public static Organisation[] getOrganisations(int offset, int count) {
- GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT certOwners.id FROM organisations inner join certOwners on certOwners.id=organisations.id where certOwners.deleted is null LIMIT ?,?");
+ 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 ?");
ps.setInt(1, offset);
ps.setInt(2, count);
GigiResultSet res = ps.executeQuery();
cert.revoke();
}
}
- GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE organisations SET name=?, state=?, province=?, city=?, contactEmail=?");
+ GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `organisations` SET `name`=?, `state`=?, `province`=?, `city`=?, `contactEmail`=?");
ps.setString(1, o);
ps.setString(2, c);
ps.setString(3, st);
province = st;
city = l;
}
+
+ public boolean isMaster(User u) {
+ for (Affiliation i : getAllAdmins()) {
+ if (i.isMaster() && i.getTarget() == u) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public boolean isValidEmail(String email) {
+ return isValidDomain(email.split("@", 2)[1]);
+ }
}