]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/pages/wot/AssurePage.java
upd: use a more strict pattern for handling forms
[gigi.git] / src / org / cacert / gigi / pages / wot / AssurePage.java
index 1ff8410c1b06eae6a5dc32989d06866cb1b1fc02..c29b2388cd83a13e7cabd63fe9b6142b08df0603 100644 (file)
 package org.cacert.gigi.pages.wot;
 
 import java.io.IOException;
-import java.io.InputStreamReader;
 import java.io.PrintWriter;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
 import java.util.HashMap;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
 
-import org.cacert.gigi.User;
-import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.GigiApiException;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
+import org.cacert.gigi.dbObjects.User;
 import org.cacert.gigi.output.DateSelector;
-import org.cacert.gigi.output.Template;
-import org.cacert.gigi.pages.LoginPage;
+import org.cacert.gigi.output.template.Form;
+import org.cacert.gigi.output.template.Template;
 import org.cacert.gigi.pages.Page;
+import org.cacert.gigi.util.AuthorizationContext;
+import org.cacert.gigi.util.HTMLEncoder;
 
 public class AssurePage extends Page {
-       public static final String PATH = "/wot/assure/*";
-       public static final String SESSION = "/wot/assure/FORM";
-       DateSelector ds = new DateSelector("day", "month", "year");
-       Template t;
-
-       public AssurePage() {
-               super("Assure someone");
-               t = new Template(new InputStreamReader(
-                               AssurePage.class.getResourceAsStream("AssureeSearch.templ")));
-       }
-
-       @Override
-       public void doGet(HttpServletRequest req, HttpServletResponse resp)
-                       throws IOException {
-
-               PrintWriter out = resp.getWriter();
-               String pi = req.getPathInfo().substring(PATH.length() - 2);
-               if (pi.length() > 1) {
-                       User myself = LoginPage.getUser(req);
-                       int mid = Integer.parseInt(pi.substring(1));
-                       if (mid == myself.getId()) {
-                               out.println("Cannot assure myself.");
-                               return;
-                       }
-
-                       HttpSession hs = req.getSession();
-                       AssuranceForm form = (AssuranceForm) hs.getAttribute(SESSION);
-                       if (form == null || form.assuree.getId() != mid) {
-                               form = new AssuranceForm(mid);
-                               hs.setAttribute(SESSION, form);
-                       }
-
-                       form.output(out, getLanguage(req), new HashMap<String, Object>());;
-               } else {
-                       HashMap<String, Object> vars = new HashMap<String, Object>();
-                       vars.put("DoB", ds);
-                       t.output(out, getLanguage(req), vars);
-               }
-       }
-       @Override
-       public void doPost(HttpServletRequest req, HttpServletResponse resp)
-                       throws IOException {
-               PrintWriter out = resp.getWriter();
-               String pi = req.getPathInfo().substring(PATH.length() - 2);
-               if (pi.length() > 1) {
-                       AssuranceForm form = (AssuranceForm) req.getSession().getAttribute(
-                                       SESSION);
-                       if (form == null) {
-                               out.println("No form found. This is an Error. Fill in the form again.");
-                       }
-                       form.submit(out, req);
-
-                       return;
-               }
-
-               System.out.println("searching for");
-               try {
-                       PreparedStatement ps = DatabaseConnection.getInstance().prepare(
-                                       "SELECT id FROM users WHERE email=? AND dob=?");
-                       ps.setString(1, req.getParameter("email"));
-                       String day = req.getParameter("year") + "-"
-                                       + req.getParameter("month") + "-" + req.getParameter("day");
-                       ps.setString(2, day);
-                       ResultSet rs = ps.executeQuery();
-                       int id = 0;
-                       if (rs.next()) {
-                               id = rs.getInt(1);
-                       }
-                       if (rs.next()) {
-                               out.println("Error, ambigous user. Please contact support@cacert.org");
-                       } else {
-                               resp.sendRedirect(PATH.substring(0, PATH.length() - 2) + "/"
-                                               + id);
-                       }
-
-                       rs.close();
-               } catch (SQLException e) {
-                       e.printStackTrace();
-               }
-       }
+
+    public static final String PATH = "/wot/assure";
+
+    DateSelector ds = new DateSelector("day", "month", "year");
+
+    private static final Template t = new Template(AssuranceForm.class.getResource("AssureeSearch.templ"));
+
+    public AssurePage() {
+        super("Verify someone");
+
+    }
+
+    @Override
+    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+
+        PrintWriter out = resp.getWriter();
+        HashMap<String, Object> vars = new HashMap<String, Object>();
+        vars.put("DoB", ds);
+        t.output(out, getLanguage(req), vars);
+    }
+
+    @Override
+    public boolean isPermitted(AuthorizationContext ac) {
+        return ac != null && ac.canAssure();
+    }
+
+    @Override
+    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+        PrintWriter out = resp.getWriter();
+        if (req.getParameter("search") == null) {
+            AssuranceForm form = Form.getForm(req, AssuranceForm.class);
+            if (form.submitProtected(out, req)) {
+                if (form.isWithPasswordReset()) {
+                    resp.getWriter().println(HTMLEncoder.encodeHTML(translate(req, "Password reset successful.")));
+                }
+                out.println(translate(req, "Verification complete."));
+                return;
+            }
+            return;
+        }
+
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `users`.`id`, `verified` FROM `users` INNER JOIN `certOwners` ON `certOwners`.`id`=`users`.`id` WHERE `email`=? AND `dob`=? AND `deleted` IS NULL")) {
+            ds.update(req);
+
+            ps.setString(1, req.getParameter("email"));
+            ps.setDate(2, ds.getDate().toSQLDate());
+            GigiResultSet rs = ps.executeQuery();
+            int id = 0;
+            if (rs.next()) {
+                id = rs.getInt(1);
+                boolean verified = rs.getBoolean(2);
+                if (rs.next()) {
+                    out.println("Error, ambigous user. Please contact support@cacert.org.");
+                } else {
+                    if ( !verified) {
+                        out.println(translate(req, "User is not yet verified. Please try again in 24 hours!"));
+                    } else if (getUser(req).getId() == id) {
+
+                    } else {
+                        User assuree = User.getById(id);
+                        try {
+                            new AssuranceForm(req, assuree).output(out, getLanguage(req), new HashMap<String, Object>());
+                        } catch (GigiApiException e) {
+                            e.format(out, Page.getLanguage(req));
+                        }
+                    }
+                }
+            } else {
+                throw new GigiApiException("I'm sorry, there was no email and date of birth matching" //
+                        + " what you entered in the system. Please double check your information.");
+            }
+
+        } catch (GigiApiException e) {
+            e.format(out, getLanguage(req));
+        }
+    }
 }