From efa3fa46bca73b26c5ef7142cf6d436fb2e5468b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Sun, 10 Dec 2017 00:18:03 +0100 Subject: [PATCH] fix: ensure that Users and Organisations only are inserted completely Change-Id: I2c9fc5140ad46020c55325622fb102a0d1a073db --- .../wpia/gigi/dbObjects/CertificateOwner.java | 10 +++++++++- src/club/wpia/gigi/dbObjects/Organisation.java | 17 +++++++++++------ src/club/wpia/gigi/dbObjects/User.java | 13 +++++++++---- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/club/wpia/gigi/dbObjects/CertificateOwner.java b/src/club/wpia/gigi/dbObjects/CertificateOwner.java index f608b2fc..007d98d3 100644 --- a/src/club/wpia/gigi/dbObjects/CertificateOwner.java +++ b/src/club/wpia/gigi/dbObjects/CertificateOwner.java @@ -24,7 +24,15 @@ public abstract class CertificateOwner implements IdCachable, Serializable { this.id = id; } - protected CertificateOwner() { + /** + * This constructor has a dummy parameter to allow callers to do checks + * before invoking the super constructor. + * + * @param dummy + * a parameter that is not used to allow callers to do checks + * before super constructor invocation. + */ + protected CertificateOwner(Void dummy) { try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `certOwners` DEFAULT VALUES")) { ps.execute(); id = ps.lastInsertId(); diff --git a/src/club/wpia/gigi/dbObjects/Organisation.java b/src/club/wpia/gigi/dbObjects/Organisation.java index 4ee25d0e..c9754565 100644 --- a/src/club/wpia/gigi/dbObjects/Organisation.java +++ b/src/club/wpia/gigi/dbObjects/Organisation.java @@ -68,12 +68,7 @@ public class Organisation extends CertificateOwner { private String postalAddress; public Organisation(String name, Country country, String province, String city, String email, String optionalName, String postalAddress, User creator) throws GigiApiException { - if ( !creator.isInGroup(Group.ORG_AGENT)) { - throw new GigiApiException("Only Organisation RA Agents may create organisations."); - } - if (country == null) { - throw new GigiApiException("Got country code of illegal type."); - } + super(validate(creator, country)); this.name = name; this.country = country; this.province = province; @@ -98,6 +93,16 @@ public class Organisation extends CertificateOwner { } } + private static Void validate(User creator, Country country) throws GigiApiException { + if ( !creator.isInGroup(Group.ORG_AGENT)) { + throw new GigiApiException("Only Organisation RA Agents may create organisations."); + } + if (country == null) { + throw new GigiApiException("Got country code of illegal type."); + } + return null; + } + protected Organisation(GigiResultSet rs) throws GigiApiException { super(rs.getInt("id")); name = rs.getString("name"); diff --git a/src/club/wpia/gigi/dbObjects/User.java b/src/club/wpia/gigi/dbObjects/User.java index cdd00d6f..3c2cd6b0 100644 --- a/src/club/wpia/gigi/dbObjects/User.java +++ b/src/club/wpia/gigi/dbObjects/User.java @@ -105,10 +105,7 @@ public class User extends CertificateOwner { } public User(String email, String password, DayDate dob, Locale locale, Country residenceCountry, NamePart... preferred) throws GigiApiException { - // Avoid storing information that obviously won't get through - if ( !EmailProvider.isValidMailAddress(email)) { - throw new IllegalArgumentException("Invalid email."); - } + super(validate(email)); this.email = email; this.dob = dob; @@ -128,6 +125,14 @@ public class User extends CertificateOwner { new EmailAddress(this, email, locale); } + private static Void validate(String email) { + // Avoid storing information that obviously won't get through + if ( !EmailProvider.isValidMailAddress(email)) { + throw new IllegalArgumentException("Invalid email."); + } + return null; + } + public Name[] getNames() { try (GigiPreparedStatement gps = new GigiPreparedStatement("SELECT `id` FROM `names` WHERE `uid`=? AND `deleted` IS NULL", true)) { gps.setInt(1, getId()); -- 2.39.2