]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/orga/CreateOrgForm.java
add: check for valid entries in organisation form
[gigi.git] / src / org / cacert / gigi / pages / orga / CreateOrgForm.java
1 package org.cacert.gigi.pages.orga;
2
3 import java.io.PrintWriter;
4 import java.util.Map;
5
6 import javax.servlet.http.HttpServletRequest;
7
8 import org.cacert.gigi.GigiApiException;
9 import org.cacert.gigi.dbObjects.Organisation;
10 import org.cacert.gigi.email.EmailProvider;
11 import org.cacert.gigi.localisation.Language;
12 import org.cacert.gigi.output.template.Form;
13 import org.cacert.gigi.output.template.SprintfCommand;
14 import org.cacert.gigi.output.template.Template;
15 import org.cacert.gigi.pages.LoginPage;
16
17 public class CreateOrgForm extends Form {
18
19     private final static Template t = new Template(CreateOrgForm.class.getResource("CreateOrgForm.templ"));
20
21     private Organisation result;
22
23     private String o = "";
24
25     private String c = "";
26
27     private String st = "";
28
29     private String l = "";
30
31     private String email = "";
32
33     private String optionalName = "";
34
35     private String postalAddress = "";
36
37     private boolean isEdit = false;
38
39     public CreateOrgForm(HttpServletRequest hsr) {
40         super(hsr);
41     }
42
43     public CreateOrgForm(HttpServletRequest hsr, Organisation t) {
44         super(hsr);
45         isEdit = true;
46         result = t;
47         o = t.getName();
48         c = t.getState();
49         st = t.getProvince();
50         l = t.getCity();
51         email = t.getContactEmail();
52         optionalName = t.getOptionalName();
53         postalAddress = t.getPostalAddress();
54     }
55
56     @Override
57     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
58         String action = req.getParameter("action");
59         if (action == null) {
60             return false;
61         }
62
63         if (action.equals("new")) {
64             checkCertData(req);
65             checkOrganisationData(req);
66             Organisation ne = new Organisation(o, c, st, l, email, optionalName, postalAddress, LoginPage.getUser(req));
67             result = ne;
68             return true;
69         } else if (action.equals("updateOrganisationData")) {
70             checkOrganisationData(req);
71             result.updateOrgData(email, optionalName, postalAddress);
72             return true;
73         } else if (action.equals("updateCertificateData")) {
74             checkCertData(req);
75             result.updateCertData(o, c, st, l);
76             return true;
77         }
78
79         return false;
80     }
81
82     private void checkOrganisationData(HttpServletRequest req) throws GigiApiException {
83         email = extractParam(req, "contact");
84         optionalName = extractParam(req, "optionalName");
85         postalAddress = extractParam(req, "postalAddress");
86         if ( !EmailProvider.MAIL.matcher(email).matches()) {
87             throw new GigiApiException("Contact email is not a valid email address");
88         }
89     }
90
91     private void checkCertData(HttpServletRequest req) throws GigiApiException {
92         o = extractParam(req, "O");
93         c = extractParam(req, "C");
94         st = extractParam(req, "ST");
95         l = extractParam(req, "L");
96
97         if (o.length() > 64 || o.length() < 1) {
98             throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Organisation name", 64));
99         }
100         if (c.length() != 2) {
101             throw new GigiApiException(SprintfCommand.createSimple("{0} not given or not exactly {1} characters long", "Country code", 2));
102         }
103
104         if (st.length() > 128 || st.length() < 1) {
105             throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "State/county", 128));
106         }
107
108         if (l.length() > 128 || l.length() < 1) {
109             throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Town/suburb", 128));
110         }
111     }
112
113     private String extractParam(HttpServletRequest req, String name) {
114         String parameter = req.getParameter(name);
115         if (parameter == null) {
116             return "";
117         }
118         return parameter.trim();
119     }
120
121     public Organisation getResult() {
122         return result;
123     }
124
125     @Override
126     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
127         vars.put("O", o);
128         vars.put("C", c);
129         vars.put("ST", st);
130         vars.put("L", this.l);
131         vars.put("email", email);
132         vars.put("optionalName", optionalName);
133         vars.put("postalAddress", postalAddress);
134         if (isEdit) {
135             vars.put("edit", true);
136         }
137         t.output(out, l, vars);
138     }
139 }