X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fpages%2Fwot%2FAssurePage.java;h=af00c0e8b7acb0319d4f2be12b8eaf68fba09be2;hb=4d9b0e8bdf122324cec9f3dd387c02d14c30877e;hp=5fae0f8cd042374e86ff5aabe3a9fd86fc0c90c3;hpb=41277f3156ec60c230d670b7f0b18b5646019522;p=gigi.git diff --git a/src/org/cacert/gigi/pages/wot/AssurePage.java b/src/org/cacert/gigi/pages/wot/AssurePage.java index 5fae0f8c..af00c0e8 100644 --- a/src/org/cacert/gigi/pages/wot/AssurePage.java +++ b/src/org/cacert/gigi/pages/wot/AssurePage.java @@ -1,85 +1,117 @@ 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.sql.Date; +import java.util.Calendar; import java.util.HashMap; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.cacert.gigi.User; +import org.cacert.gigi.GigiApiException; import org.cacert.gigi.database.DatabaseConnection; +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.output.template.Form; +import org.cacert.gigi.output.template.Template; import org.cacert.gigi.pages.LoginPage; import org.cacert.gigi.pages.Page; +import org.cacert.gigi.util.AuthorizationContext; +import org.cacert.gigi.util.Notary; public class AssurePage extends Page { - public static final String PATH = "/wot/assure/*"; - 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) { - out.println("I am a Placeholder for the Assurance form # "); - out.println(pi.substring(1)); - User myself = LoginPage.getUser(req); - int mid = Integer.parseInt(pi.substring(1)); - if (mid == myself.getId()) { - out.println("Cannot assure myself."); - return; - } - - new AssuranceForm(mid).output(out, getLanguage(req), - new HashMap());; - } else { - HashMap vars = new HashMap(); - vars.put("DoB", ds); - t.output(out, getLanguage(req), vars); - } - } - @Override - public void doPost(HttpServletRequest req, HttpServletResponse resp) - throws IOException { - PrintWriter out = resp.getWriter(); - 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"); + + Template t; + + public AssurePage() { + super("Assure someone"); + t = new Template(AssuranceForm.class.getResource("AssureeSearch.templ")); + + } + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + + PrintWriter out = resp.getWriter(); + String pi = req.getPathInfo().substring(PATH.length()); + HashMap vars = new HashMap(); + vars.put("DoB", ds); + t.output(out, getLanguage(req), vars); + } + + @Override + public boolean isPermitted(AuthorizationContext ac) { + return ac != null && ac.getActor().canAssure(); + } + + private void outputForm(HttpServletRequest req, PrintWriter out, AssuranceForm form) { + User myself = LoginPage.getUser(req); + try { + Notary.checkAssuranceIsPossible(myself, form.getAssuree()); + } catch (GigiApiException e) { + e.format(out, Page.getLanguage(req)); + } + + form.output(out, getLanguage(req), new HashMap()); + } + + @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.submit(out, req)) { + out.println(translate(req, "Assurance complete.")); + } else { + outputForm(req, resp.getWriter(), form); + } + + return; + } + + GigiResultSet rs = null; + try { + GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `users`.`id`, `verified` FROM `users` INNER JOIN `certOwners` ON `certOwners`.`id`=`users`.`id` WHERE `email`=? AND `dob`=? AND `deleted` IS NULL"); + ps.setString(1, req.getParameter("email")); + Calendar c = Calendar.getInstance(); + c.set(Integer.parseInt(req.getParameter("year")), Integer.parseInt(req.getParameter("month")) - 1, Integer.parseInt(req.getParameter("day"))); + ps.setDate(2, new Date(c.getTimeInMillis())); + 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 { + AssuranceForm form = new AssuranceForm(req, User.getById(id)); + outputForm(req, out, form); + } + } + } else { + out.print("
"); + + out.println(translate(req, "I'm sorry, there was no email and date of birth matching" + " what you entered in the system. Please double check" + " your information.")); + out.print("
"); + } + + rs.close(); + } finally { + if (rs != null) { + rs.close(); + } + } + } }