]> WPIA git - gigi.git/commitdiff
add: factor out country selection and type-restrict internal api.
authorFelix Dörre <felix@dogcraft.de>
Thu, 11 Aug 2016 10:36:54 +0000 (12:36 +0200)
committerFelix Dörre <felix@dogcraft.de>
Thu, 11 Aug 2016 21:54:00 +0000 (23:54 +0200)
Change-Id: I39fe3a9626408bb085278172538268ff9b5f2ce7

12 files changed:
src/org/cacert/gigi/dbObjects/CountryCode.java
src/org/cacert/gigi/dbObjects/Organisation.java
src/org/cacert/gigi/output/CountrySelector.java [new file with mode: 0644]
src/org/cacert/gigi/output/CountrySelector.templ [new file with mode: 0644]
src/org/cacert/gigi/pages/orga/CreateOrgForm.java
src/org/cacert/gigi/pages/orga/CreateOrgForm.templ
tests/org/cacert/gigi/TestOrga.java
tests/org/cacert/gigi/api/IssueCert.java
tests/org/cacert/gigi/pages/orga/TestOrgManagement.java
tests/org/cacert/gigi/testUtils/ConfiguredTest.java
tests/org/cacert/gigi/testUtils/OrgTest.java
tests/org/cacert/gigi/testUtils/RestrictedApiTest.java

index bf9375ce81afae29c4852b81f95cad1f1192e17b..47abc2776e818bcb5e375986ab626fddf8beb760 100644 (file)
@@ -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"));
+        }
+
+    }
 }
index 9581f815f170d1ad4801ccc702e540c0267fbe6f..bbb3214c84ad2408b690cdf9b41d2eab43c83863 100644 (file)
@@ -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 (file)
index 0000000..f35a8f2
--- /dev/null
@@ -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<String, Object> vars) {
+        vars.put("countryCode", new ArrayIterable<CountryCode>(all) {
+
+            @Override
+            public void apply(CountryCode t, Language l, Map<String, Object> 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 (file)
index 0000000..818940f
--- /dev/null
@@ -0,0 +1,6 @@
+<select name='<?=$name?>'>
+<option value='invalid'><? if($optional) { ?><?=_not specified?><? } else { ?><?=_please select...?><? } ?></option>
+<? foreach($countryCode) { ?>
+    <option value="<?=$cc?>" <?=$selected?> ><?=$cc?> - <?=$display?></option>
+<? } ?>
+</select>
index 194fe529ce7a997f7a08ce94389393a1e2f073bf..2d57a3c4af642c98a1e51df48b0ba1a4919cb4a4 100644 (file)
@@ -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<String, Object> 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<String, Object> 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);
         }
index 761bd78b0655fa0e6f4e05019b1110fef65727f3..7f34782ba86f325e56d76b896bad6ffd55312802 100644 (file)
   <tr>
     <td><?=_Country?>:</td>
     <td>
-      <select name="C">
-    <? foreach($countryCode) { ?>
-        <option value="<?=$cc?>" <?=$selected?> ><?=$cc?> - <?=$display?></option>
-    <? } ?>
-      </select>
+      <?=$C?>
       <?=_(2 letter !'<a href="http://www.iso.org/iso/home/standards/country_codes/iso-3166-1_decoding_table.htm">'ISO code!'</a>')?>
     </td>
   </tr>
index b1a23129ca432371f272c20a552aca56159c0487..3ea186540f30eade03708f22407554d72c6e7fd6 100644 (file)
@@ -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());
index 5211ec4ce5655e749caadbd4b6c9ad5065817c56..6410a6dec1a5a427a34fb8faeff505ef1fe1698a 100644 (file)
@@ -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);
index 7c7160320a2a7454af83d2f70b345e457513014f..c8f3d11972dcab9fd2ea6041bb6d9931fdb738a6 100644 (file)
@@ -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());
index fda87cf142377a806be0aa3109612e0dff29116a..359cda2574e46e24d6a4e1591c6c06809017dd7f 100644 (file)
@@ -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()));
     }
-
 }
index 8ae7e2c901e29a7935de5ff1a525b48a3d145f1b..4e2ea8bd1bb74bd3c990880afa52e5a0349cab61 100644 (file)
@@ -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;
     }
 }
index e0f2947a97aaf76fb936b48b660723f66d762f56..9e8586d56b49ef72309129101e0eaba26ad5743b 100644 (file)
@@ -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");