]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/pages/orga/CreateOrgForm.java
chg: Be more liberal in what email addresses are accepted.
[gigi.git] / src / org / cacert / gigi / pages / orga / CreateOrgForm.java
index 27f7dbcfef1cab0c2303934c9b81bd3df4ecdbb8..194fe529ce7a997f7a08ce94389393a1e2f073bf 100644 (file)
@@ -6,9 +6,14 @@ 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.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;
 
@@ -26,35 +31,100 @@ public class CreateOrgForm extends Form {
 
     private String l = "";
 
+    private String email = "";
+
+    private String optionalName = "";
+
+    private String postalAddress = "";
+
     private boolean isEdit = false;
 
+    private CountryCode[] countryCode;
+
     public CreateOrgForm(HttpServletRequest hsr) {
         super(hsr);
+        try {
+            countryCode = CountryCode.getCountryCodes(CountryCodeType.CODE_2_CHARS);
+        } catch (GigiApiException e) {
+            throw new Error(e); // should not happen
+        }
     }
 
     public CreateOrgForm(HttpServletRequest hsr, Organisation t) {
-        super(hsr);
+        this(hsr);
         isEdit = true;
         result = t;
         o = t.getName();
         c = t.getState();
         st = t.getProvince();
         l = t.getCity();
+        email = t.getContactEmail();
+        optionalName = t.getOptionalName();
+        postalAddress = t.getPostalAddress();
     }
 
     @Override
     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
-        o = req.getParameter("O");
-        c = req.getParameter("C");
-        st = req.getParameter("ST");
-        l = req.getParameter("L");
-        if (result != null) {
-            result.update(o, c, st, l);
+        String action = req.getParameter("action");
+        if (action == null) {
+            return false;
+        }
+
+        if (action.equals("new")) {
+            checkCertData(req);
+            checkOrganisationData(req);
+            Organisation ne = new Organisation(o, c, st, l, email, optionalName, postalAddress, LoginPage.getUser(req));
+            result = ne;
+            return true;
+        } else if (action.equals("updateOrganisationData")) {
+            checkOrganisationData(req);
+            result.updateOrgData(email, optionalName, postalAddress);
+            return true;
+        } else if (action.equals("updateCertificateData")) {
+            checkCertData(req);
+            result.updateCertData(o, c, st, l);
             return true;
         }
-        Organisation ne = new Organisation(o, c, st, l, LoginPage.getUser(req));
-        result = ne;
-        return true;
+
+        return false;
+    }
+
+    private void checkOrganisationData(HttpServletRequest req) throws GigiApiException {
+        email = extractParam(req, "contact");
+        optionalName = extractParam(req, "optionalName");
+        postalAddress = extractParam(req, "postalAddress");
+        if ( !EmailProvider.isValidMailAddress(email)) {
+            throw new GigiApiException("Contact email is not a valid email address");
+        }
+    }
+
+    private void checkCertData(HttpServletRequest req) throws GigiApiException {
+        o = extractParam(req, "O");
+        c = extractParam(req, "C").toUpperCase();
+        st = extractParam(req, "ST");
+        l = extractParam(req, "L");
+
+        if (o.length() > 64 || o.length() < 1) {
+            throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Organisation name", 64));
+        }
+
+        CountryCode.checkCountryCode(c, CountryCodeType.CODE_2_CHARS);
+
+        if (st.length() > 128 || st.length() < 1) {
+            throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "State/county", 128));
+        }
+
+        if (l.length() > 128 || l.length() < 1) {
+            throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Town/suburb", 128));
+        }
+    }
+
+    private String extractParam(HttpServletRequest req, String name) {
+        String parameter = req.getParameter(name);
+        if (parameter == null) {
+            return "";
+        }
+        return parameter.trim();
     }
 
     public Organisation getResult() {
@@ -67,6 +137,31 @@ public class CreateOrgForm extends Form {
         vars.put("C", c);
         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);
         if (isEdit) {
             vars.put("edit", true);
         }