From: Felix Dörre Date: Thu, 11 Aug 2016 10:36:54 +0000 (+0200) Subject: add: factor out country selection and type-restrict internal api. X-Git-Url: https://code.wpia.club/?p=gigi.git;a=commitdiff_plain;h=bead06ac89a5fbe282dab187c5d1babbb7dcdf66 add: factor out country selection and type-restrict internal api. Change-Id: I39fe3a9626408bb085278172538268ff9b5f2ce7 --- diff --git a/src/org/cacert/gigi/dbObjects/CountryCode.java b/src/org/cacert/gigi/dbObjects/CountryCode.java index bf9375ce..47abc277 100644 --- a/src/org/cacert/gigi/dbObjects/CountryCode.java +++ b/src/org/cacert/gigi/dbObjects/CountryCode.java @@ -8,18 +8,27 @@ import org.cacert.gigi.output.template.SprintfCommand; public class CountryCode { public enum CountryCodeType { - CODE_2_CHARS(2, "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2", "SELECT 1 FROM `countryIsoCode` WHERE `code2`=?"),// - CODE_3_CHARS(3, "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` ORDER BY code3", "SELECT 1 FROM `countryIsoCode` WHERE `code3`=?"); + CODE_2_CHARS(2, // + "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2",// + "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` WHERE `code2`=?",// + "SELECT 1 FROM `countryIsoCode` WHERE `code2`=?"),// + CODE_3_CHARS(3,// + "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` ORDER BY code3", // + "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` WHERE `code3`=?",// + "SELECT 1 FROM `countryIsoCode` WHERE `code3`=?"); private final String listQuery; + private final String getQuery; + private final String validationQuery; private final int len; - private CountryCodeType(int len, String listQuery, String validationQuery) { + private CountryCodeType(int len, String listQuery, String getQuery, String validationQuery) { this.len = len; this.listQuery = listQuery; + this.getQuery = getQuery; this.validationQuery = validationQuery; } @@ -27,6 +36,10 @@ public class CountryCode { return len; } + public String getGetQuery() { + return getQuery; + } + public String getListQuery() { return listQuery; } @@ -93,4 +106,20 @@ public class CountryCode { } } + + public static CountryCode getCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException { + if (countrycode.length() != cType.getLen()) { + throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen()))); + } + try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getGetQuery())) { + ps.setString(1, countrycode.toUpperCase()); + GigiResultSet rs = ps.executeQuery(); + + if ( !rs.next()) { + throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase())); + } + return new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode")); + } + + } } diff --git a/src/org/cacert/gigi/dbObjects/Organisation.java b/src/org/cacert/gigi/dbObjects/Organisation.java index 9581f815..bbb3214c 100644 --- a/src/org/cacert/gigi/dbObjects/Organisation.java +++ b/src/org/cacert/gigi/dbObjects/Organisation.java @@ -65,12 +65,12 @@ public class Organisation extends CertificateOwner { private String postalAddress; - public Organisation(String name, String state, String province, String city, String email, String optionalName, String postalAddress, User creator) throws GigiApiException { + public Organisation(String name, CountryCode state, String province, String city, String email, String optionalName, String postalAddress, User creator) throws GigiApiException { if ( !creator.isInGroup(Group.ORGASSURER)) { throw new GigiApiException("Only Organisation RA Agents may create organisations."); } this.name = name; - this.state = state; + this.state = state.getCountryCode(); this.province = province; this.city = city; this.email = email; @@ -80,7 +80,7 @@ public class Organisation extends CertificateOwner { try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, optional_name=?, postal_address=?, creator=?")) { ps.setInt(1, id); ps.setString(2, name); - ps.setString(3, state); + ps.setString(3, state.getCountryCode()); ps.setString(4, province); ps.setString(5, city); ps.setString(6, email); @@ -206,7 +206,7 @@ public class Organisation extends CertificateOwner { } } - public void updateCertData(String o, String c, String st, String l) { + public void updateCertData(String o, CountryCode c, String st, String l) { for (Certificate cert : getCertificates(false)) { if (cert.getStatus() == CertificateStatus.ISSUED) { cert.revoke(); @@ -214,14 +214,14 @@ public class Organisation extends CertificateOwner { } try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `state`=?, `province`=?, `city`=? WHERE `id`=?")) { ps.setString(1, o); - ps.setString(2, c); + ps.setString(2, c.getCountryCode()); ps.setString(3, st); ps.setString(4, l); ps.setInt(5, getId()); ps.executeUpdate(); } name = o; - state = c; + state = c.getCountryCode(); province = st; city = l; } diff --git a/src/org/cacert/gigi/output/CountrySelector.java b/src/org/cacert/gigi/output/CountrySelector.java new file mode 100644 index 00000000..f35a8f29 --- /dev/null +++ b/src/org/cacert/gigi/output/CountrySelector.java @@ -0,0 +1,76 @@ +package org.cacert.gigi.output; + +import java.io.PrintWriter; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.cacert.gigi.GigiApiException; +import org.cacert.gigi.dbObjects.CountryCode; +import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType; +import org.cacert.gigi.localisation.Language; +import org.cacert.gigi.output.template.Outputable; +import org.cacert.gigi.output.template.Template; + +public class CountrySelector implements Outputable { + + private static final Template t = new Template(CountrySelector.class.getResource("CountrySelector.templ")); + + private CountryCode[] all = CountryCode.getCountryCodes(CountryCodeType.CODE_2_CHARS); + + private String name; + + private CountryCode selected; + + private boolean optional; + + public CountrySelector(String name, boolean optional) throws GigiApiException { + this.name = name; + this.optional = optional; + } + + public CountrySelector(String name, boolean optional, String state) throws GigiApiException { + this(name, optional); + selected = CountryCode.getCountryCode(state, CountryCodeType.CODE_2_CHARS); + + } + + public void update(HttpServletRequest r) throws GigiApiException { + String vS = r.getParameter(name); + selected = null; + if (vS == null || vS.equals("invalid")) { + if (optional) { + return; + } else { + throw new GigiApiException("Country code required."); + } + } + selected = CountryCode.getCountryCode(vS, CountryCodeType.CODE_2_CHARS); + + } + + @Override + public void output(PrintWriter out, Language l, Map vars) { + vars.put("countryCode", new ArrayIterable(all) { + + @Override + public void apply(CountryCode t, Language l, Map vars) { + vars.put("cc", t.getCountryCode()); + vars.put("display", t.getCountry()); + if (selected != null && t.getCountryCode().equals(selected.getCountryCode())) { + vars.put("selected", "selected"); + } else { + vars.put("selected", ""); + } + } + }); + vars.put("optional", optional); + vars.put("name", name); + t.output(out, l, vars); + } + + public CountryCode getCountry() { + return selected; + } + +} diff --git a/src/org/cacert/gigi/output/CountrySelector.templ b/src/org/cacert/gigi/output/CountrySelector.templ new file mode 100644 index 00000000..818940f4 --- /dev/null +++ b/src/org/cacert/gigi/output/CountrySelector.templ @@ -0,0 +1,6 @@ + diff --git a/src/org/cacert/gigi/pages/orga/CreateOrgForm.java b/src/org/cacert/gigi/pages/orga/CreateOrgForm.java index 194fe529..2d57a3c4 100644 --- a/src/org/cacert/gigi/pages/orga/CreateOrgForm.java +++ b/src/org/cacert/gigi/pages/orga/CreateOrgForm.java @@ -6,13 +6,11 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.cacert.gigi.GigiApiException; -import org.cacert.gigi.dbObjects.CountryCode; -import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType; import org.cacert.gigi.dbObjects.Organisation; import org.cacert.gigi.email.EmailProvider; import org.cacert.gigi.localisation.Language; +import org.cacert.gigi.output.CountrySelector; import org.cacert.gigi.output.template.Form; -import org.cacert.gigi.output.template.IterableDataset; import org.cacert.gigi.output.template.SprintfCommand; import org.cacert.gigi.output.template.Template; import org.cacert.gigi.pages.LoginPage; @@ -25,8 +23,6 @@ public class CreateOrgForm extends Form { private String o = ""; - private String c = ""; - private String st = ""; private String l = ""; @@ -39,12 +35,12 @@ public class CreateOrgForm extends Form { private boolean isEdit = false; - private CountryCode[] countryCode; + private CountrySelector cs; public CreateOrgForm(HttpServletRequest hsr) { super(hsr); try { - countryCode = CountryCode.getCountryCodes(CountryCodeType.CODE_2_CHARS); + cs = new CountrySelector("C", false); } catch (GigiApiException e) { throw new Error(e); // should not happen } @@ -55,7 +51,11 @@ public class CreateOrgForm extends Form { isEdit = true; result = t; o = t.getName(); - c = t.getState(); + try { + cs = new CountrySelector("C", false, t.getState()); + } catch (GigiApiException e) { + throw new Error(e); + } st = t.getProvince(); l = t.getCity(); email = t.getContactEmail(); @@ -73,7 +73,7 @@ public class CreateOrgForm extends Form { if (action.equals("new")) { checkCertData(req); checkOrganisationData(req); - Organisation ne = new Organisation(o, c, st, l, email, optionalName, postalAddress, LoginPage.getUser(req)); + Organisation ne = new Organisation(o, cs.getCountry(), st, l, email, optionalName, postalAddress, LoginPage.getUser(req)); result = ne; return true; } else if (action.equals("updateOrganisationData")) { @@ -82,7 +82,7 @@ public class CreateOrgForm extends Form { return true; } else if (action.equals("updateCertificateData")) { checkCertData(req); - result.updateCertData(o, c, st, l); + result.updateCertData(o, cs.getCountry(), st, l); return true; } @@ -100,7 +100,6 @@ public class CreateOrgForm extends Form { private void checkCertData(HttpServletRequest req) throws GigiApiException { o = extractParam(req, "O"); - c = extractParam(req, "C").toUpperCase(); st = extractParam(req, "ST"); l = extractParam(req, "L"); @@ -108,7 +107,7 @@ public class CreateOrgForm extends Form { throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Organisation name", 64)); } - CountryCode.checkCountryCode(c, CountryCodeType.CODE_2_CHARS); + cs.update(req); if (st.length() > 128 || st.length() < 1) { throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "State/county", 128)); @@ -134,34 +133,13 @@ public class CreateOrgForm extends Form { @Override protected void outputContent(PrintWriter out, Language l, Map vars) { vars.put("O", o); - vars.put("C", c); + vars.put("C", cs); vars.put("ST", st); vars.put("L", this.l); vars.put("email", email); vars.put("optionalName", optionalName); vars.put("postalAddress", postalAddress); - vars.put("countryCode", new IterableDataset() { - - int i = 0; - - @Override - public boolean next(Language l, Map vars) { - if (i >= countryCode.length) { - return false; - } - CountryCode t = countryCode[i++]; - vars.put("id", t.getId()); - vars.put("cc", t.getCountryCode()); - vars.put("display", t.getCountry()); - if (t.getCountryCode().equals(c)) { - vars.put("selected", "selected"); - } else { - vars.put("selected", ""); - } - return true; - } - }); - // vars.put("countryCode", countryCode); + vars.put("countryCode", cs); if (isEdit) { vars.put("edit", true); } diff --git a/src/org/cacert/gigi/pages/orga/CreateOrgForm.templ b/src/org/cacert/gigi/pages/orga/CreateOrgForm.templ index 761bd78b..7f34782b 100644 --- a/src/org/cacert/gigi/pages/orga/CreateOrgForm.templ +++ b/src/org/cacert/gigi/pages/orga/CreateOrgForm.templ @@ -27,11 +27,7 @@ : - + 'ISO code!'')?> diff --git a/tests/org/cacert/gigi/TestOrga.java b/tests/org/cacert/gigi/TestOrga.java index b1a23129..3ea18654 100644 --- a/tests/org/cacert/gigi/TestOrga.java +++ b/tests/org/cacert/gigi/TestOrga.java @@ -4,6 +4,8 @@ import static org.junit.Assert.*; import java.io.IOException; +import org.cacert.gigi.dbObjects.CountryCode; +import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType; import org.cacert.gigi.dbObjects.Group; import org.cacert.gigi.dbObjects.Organisation; import org.cacert.gigi.dbObjects.User; @@ -22,7 +24,7 @@ public class TestOrga extends BusinessTest { u3.grantGroup(u1, Group.ORGASSURER); User u4 = User.getById(createAssuranceUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD)); u4.grantGroup(u1, Group.ORGASSURER); - Organisation o1 = new Organisation("name", "ST", "prov", "city", "email", "optional name", "postal address", u1); + Organisation o1 = new Organisation("name", CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS), "prov", "city", "email", "optional name", "postal address", u1); assertEquals(0, o1.getAllAdmins().size()); o1.addAdmin(u2, u1, false); assertEquals(1, o1.getAllAdmins().size()); diff --git a/tests/org/cacert/gigi/api/IssueCert.java b/tests/org/cacert/gigi/api/IssueCert.java index 5211ec4c..6410a6de 100644 --- a/tests/org/cacert/gigi/api/IssueCert.java +++ b/tests/org/cacert/gigi/api/IssueCert.java @@ -18,7 +18,9 @@ import java.security.cert.X509Certificate; import org.cacert.gigi.dbObjects.Certificate; import org.cacert.gigi.dbObjects.Certificate.CSRType; import org.cacert.gigi.dbObjects.Certificate.CertificateStatus; +import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType; import org.cacert.gigi.dbObjects.CertificateProfile; +import org.cacert.gigi.dbObjects.CountryCode; import org.cacert.gigi.dbObjects.Digest; import org.cacert.gigi.dbObjects.Domain; import org.cacert.gigi.dbObjects.Group; @@ -87,7 +89,7 @@ public class IssueCert extends ClientTest { makeAssurer(id); u.grantGroup(u, Group.ORGASSURER); - Organisation o1 = new Organisation("name", "st", "pr", "st", "test@mail", "", "", u); + Organisation o1 = new Organisation("name", CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS), "pr", "st", "test@mail", "", "", u); o1.addAdmin(u, u, false); String testdom = createUniqueName() + "-example.com"; Domain d2 = new Domain(u, o1, testdom); diff --git a/tests/org/cacert/gigi/pages/orga/TestOrgManagement.java b/tests/org/cacert/gigi/pages/orga/TestOrgManagement.java index 7c716032..c8f3d119 100644 --- a/tests/org/cacert/gigi/pages/orga/TestOrgManagement.java +++ b/tests/org/cacert/gigi/pages/orga/TestOrgManagement.java @@ -13,6 +13,8 @@ import java.sql.SQLException; import java.util.List; import org.cacert.gigi.GigiApiException; +import org.cacert.gigi.dbObjects.CountryCode; +import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType; import org.cacert.gigi.dbObjects.Organisation; import org.cacert.gigi.dbObjects.Organisation.Affiliation; import org.cacert.gigi.dbObjects.User; @@ -145,7 +147,7 @@ public class TestOrgManagement extends OrgTest { @Test public void testUpdateOrgCertData() throws IOException, GigiApiException { Organisation o1 = createUniqueOrg(); - o1.updateCertData("name", "DE", DIFFICULT_CHARS, "Köln"); + o1.updateCertData("name", CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS), DIFFICULT_CHARS, "Köln"); assertEquals("name", o1.getName()); assertEquals("DE", o1.getState()); assertEquals(DIFFICULT_CHARS, o1.getProvince()); diff --git a/tests/org/cacert/gigi/testUtils/ConfiguredTest.java b/tests/org/cacert/gigi/testUtils/ConfiguredTest.java index fda87cf1..359cda25 100644 --- a/tests/org/cacert/gigi/testUtils/ConfiguredTest.java +++ b/tests/org/cacert/gigi/testUtils/ConfiguredTest.java @@ -255,5 +255,4 @@ public abstract class ConfiguredTest { c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION + 1); return sdf.format(new Date(c.getTimeInMillis())); } - } diff --git a/tests/org/cacert/gigi/testUtils/OrgTest.java b/tests/org/cacert/gigi/testUtils/OrgTest.java index 8ae7e2c9..4e2ea8bd 100644 --- a/tests/org/cacert/gigi/testUtils/OrgTest.java +++ b/tests/org/cacert/gigi/testUtils/OrgTest.java @@ -3,6 +3,8 @@ package org.cacert.gigi.testUtils; import java.io.IOException; import org.cacert.gigi.GigiApiException; +import org.cacert.gigi.dbObjects.CountryCode; +import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType; import org.cacert.gigi.dbObjects.Group; import org.cacert.gigi.dbObjects.Organisation; @@ -16,7 +18,7 @@ public class OrgTest extends ClientTest { } public Organisation createUniqueOrg() throws GigiApiException { - Organisation o1 = new Organisation(createUniqueName(), "st", "pr", "city", "test@example.com", "", "", u); + Organisation o1 = new Organisation(createUniqueName(), CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS), "pr", "city", "test@example.com", "", "", u); return o1; } } diff --git a/tests/org/cacert/gigi/testUtils/RestrictedApiTest.java b/tests/org/cacert/gigi/testUtils/RestrictedApiTest.java index e0f2947a..9e8586d5 100644 --- a/tests/org/cacert/gigi/testUtils/RestrictedApiTest.java +++ b/tests/org/cacert/gigi/testUtils/RestrictedApiTest.java @@ -16,6 +16,8 @@ import org.cacert.gigi.dbObjects.Certificate; import org.cacert.gigi.dbObjects.Certificate.CSRType; import org.cacert.gigi.dbObjects.Certificate.SANType; import org.cacert.gigi.dbObjects.CertificateProfile; +import org.cacert.gigi.dbObjects.CountryCode; +import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType; import org.cacert.gigi.dbObjects.Digest; import org.cacert.gigi.dbObjects.Group; import org.cacert.gigi.dbObjects.Organisation; @@ -40,7 +42,7 @@ public class RestrictedApiTest extends ClientTest { grant(u.getEmail(), Group.ORGASSURER); clearCaches(); u = User.getById(u.getId()); - Organisation o = new Organisation(Organisation.SELF_ORG_NAME, "NA", "NA", "NA", "contact@cacert.org", "", "", u); + Organisation o = new Organisation(Organisation.SELF_ORG_NAME, CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS), "NA", "NA", "contact@cacert.org", "", "", u); assertTrue(o.isSelfOrganisation()); KeyPair kp = generateKeypair(); String key1 = generatePEMCSR(kp, "EMAIL=cats@cacert.org");