From d1080ab12183cad2bab5d1f94bafe67960fbf4c8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Thu, 7 Apr 2016 13:32:37 +0200 Subject: [PATCH] general cleanup --- src/org/cacert/gigi/Gigi.java | 1 + src/org/cacert/gigi/dbObjects/Assurance.java | 19 +++-- .../cacert/gigi/dbObjects/Organisation.java | 13 +++- src/org/cacert/gigi/dbObjects/User.java | 10 ++- .../cacert/gigi/pages/wot/AssuranceForm.java | 40 +++++++++- .../cacert/gigi/pages/wot/AssuranceForm.templ | 4 + src/org/cacert/gigi/pages/wot/AssurePage.java | 28 +++---- src/org/cacert/gigi/util/Notary.java | 78 ++++++++++++++++--- tests/org/cacert/gigi/util/TestNotary.java | 11 +-- .../org/cacert/gigi/pages/Manager.java | 3 +- 10 files changed, 159 insertions(+), 48 deletions(-) diff --git a/src/org/cacert/gigi/Gigi.java b/src/org/cacert/gigi/Gigi.java index c83cb640..7c720bd7 100644 --- a/src/org/cacert/gigi/Gigi.java +++ b/src/org/cacert/gigi/Gigi.java @@ -186,6 +186,7 @@ public final class Gigi extends HttpServlet { about.addItem(new SimpleMenuItem("//wiki.cacert.org/Board", "CAcert Board")); about.addItem(new SimpleMenuItem("//lists.cacert.org/wws", "Mailing Lists")); about.addItem(new SimpleMenuItem("//blog.CAcert.org/feed", "RSS News Feed")); + about.addItem(new SimpleMenuItem("//wiki.cacert.org/Impress", "Impress")); Menu languages = new Menu("Translations"); for (Locale l : Language.getSupportedLocales()) { diff --git a/src/org/cacert/gigi/dbObjects/Assurance.java b/src/org/cacert/gigi/dbObjects/Assurance.java index cb0bcc49..8f172e51 100644 --- a/src/org/cacert/gigi/dbObjects/Assurance.java +++ b/src/org/cacert/gigi/dbObjects/Assurance.java @@ -1,6 +1,5 @@ package org.cacert.gigi.dbObjects; -import org.cacert.gigi.database.GigiResultSet; import org.cacert.gigi.dbObjects.wrappers.DataContainer; @DataContainer @@ -34,15 +33,15 @@ public class Assurance { private String date; - public Assurance(GigiResultSet res) { - super(); - this.id = res.getInt("id"); - this.from = User.getById(res.getInt("from")); - this.to = User.getById(res.getInt("to")); - this.location = res.getString("location"); - this.method = res.getString("method"); - this.points = res.getInt("points"); - this.date = res.getString("date"); + public Assurance(int id, User from, User to, String location, String method, int points, String date) { + this.id = id; + this.from = from; + this.to = to; + this.location = location; + this.method = method; + this.points = points; + this.date = date; + } public User getFrom() { diff --git a/src/org/cacert/gigi/dbObjects/Organisation.java b/src/org/cacert/gigi/dbObjects/Organisation.java index ae99b115..fa6ff1be 100644 --- a/src/org/cacert/gigi/dbObjects/Organisation.java +++ b/src/org/cacert/gigi/dbObjects/Organisation.java @@ -7,10 +7,12 @@ import org.cacert.gigi.GigiApiException; import org.cacert.gigi.database.GigiPreparedStatement; import org.cacert.gigi.database.GigiResultSet; import org.cacert.gigi.dbObjects.Certificate.CertificateStatus; +import org.cacert.gigi.dbObjects.wrappers.DataContainer; public class Organisation extends CertificateOwner { - public class Affiliation { + @DataContainer + public static class Affiliation { private final User target; @@ -18,7 +20,10 @@ public class Organisation extends CertificateOwner { private final String fixedOU; - public Affiliation(User target, boolean master, String fixedOU) { + private Organisation o; + + public Affiliation(Organisation o, User target, boolean master, String fixedOU) { + this.o = o; this.target = target; this.master = master; this.fixedOU = fixedOU; @@ -37,7 +42,7 @@ public class Organisation extends CertificateOwner { } public Organisation getOrganisation() { - return Organisation.this; + return o; } } @@ -156,7 +161,7 @@ public class Organisation extends CertificateOwner { ArrayList al = new ArrayList<>(rs.getRow()); rs.beforeFirst(); while (rs.next()) { - al.add(new Affiliation(User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null)); + al.add(new Affiliation(this, User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null)); } return al; } diff --git a/src/org/cacert/gigi/dbObjects/User.java b/src/org/cacert/gigi/dbObjects/User.java index e6f06921..bf12dd77 100644 --- a/src/org/cacert/gigi/dbObjects/User.java +++ b/src/org/cacert/gigi/dbObjects/User.java @@ -154,7 +154,7 @@ public class User extends CertificateOwner { } public int getAssurancePoints() { - try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT sum(points) FROM `notary` where `to`=? AND `deleted` is NULL")) { + try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT sum(points) FROM `notary` where `to`=? AND `deleted` is NULL AND (`expire` IS NULL OR `expire` > CURRENT_TIMESTAMP)")) { query.setInt(1, getId()); GigiResultSet rs = query.executeQuery(); @@ -278,7 +278,7 @@ public class User extends CertificateOwner { List assurances = new LinkedList(); while (res.next()) { - assurances.add(new Assurance(res)); + assurances.add(assuranceByRes(res)); } this.receivedAssurances = assurances.toArray(new Assurance[0]); @@ -297,7 +297,7 @@ public class User extends CertificateOwner { List assurances = new LinkedList(); while (res.next()) { - assurances.add(new Assurance(res)); + assurances.add(assuranceByRes(res)); } this.madeAssurances = assurances.toArray(new Assurance[0]); @@ -542,4 +542,8 @@ public class User extends CertificateOwner { ps.executeUpdate(); } } + + private Assurance assuranceByRes(GigiResultSet res) { + return new Assurance(res.getInt("id"), User.getById(res.getInt("from")), User.getById(res.getInt("to")), res.getString("location"), res.getString("method"), res.getInt("points"), res.getString("date")); + } } diff --git a/src/org/cacert/gigi/pages/wot/AssuranceForm.java b/src/org/cacert/gigi/pages/wot/AssuranceForm.java index 6e37ecc8..919128ff 100644 --- a/src/org/cacert/gigi/pages/wot/AssuranceForm.java +++ b/src/org/cacert/gigi/pages/wot/AssuranceForm.java @@ -6,16 +6,20 @@ import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.cacert.gigi.GigiApiException; +import org.cacert.gigi.dbObjects.Assurance.AssuranceType; import org.cacert.gigi.dbObjects.Name; import org.cacert.gigi.dbObjects.User; import org.cacert.gigi.email.Sendmail; 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.Template; import org.cacert.gigi.pages.Page; import org.cacert.gigi.pages.PasswordResetPage; @@ -39,6 +43,8 @@ public class AssuranceForm extends Form { private User assurer; + private AssuranceType type = AssuranceType.FACE_TO_FACE; + private static final Template templ; static { templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ")); @@ -68,6 +74,30 @@ public class AssuranceForm extends Form { res.put("location", location); res.put("date", date); res.put("aword", aword); + final LinkedList ats = new LinkedList<>(); + for (AssuranceType at : AssuranceType.values()) { + try { + Notary.may(assurer, assuree, at); + ats.add(at); + } catch (GigiApiException e) { + } + } + res.put("ats", new IterableDataset() { + + Iterator t = ats.iterator(); + + @Override + public boolean next(Language l, Map vars) { + if ( !t.hasNext()) { + return false; + } + AssuranceType t1 = t.next(); + vars.put("type", t1.getDescription()); + vars.put("id", t1.toString()); + vars.put("sel", t1 == type ? " selected" : ""); + return true; + } + }); templ.output(out, l, res); } @@ -91,6 +121,14 @@ public class AssuranceForm extends Form { } else { aword = null; } + String val = req.getParameter("assuranceType"); + if (val != null) { + try { + type = AssuranceType.valueOf(val); + } catch (IllegalArgumentException e) { + outputError(out, req, "Assurance Type wrong."); + } + } int pointsI = 0; String points = req.getParameter("points"); @@ -108,7 +146,7 @@ public class AssuranceForm extends Form { return false; } try { - Notary.assure(assurer, assuree, assureeName, dob, pointsI, location, req.getParameter("date")); + Notary.assure(assurer, assuree, assureeName, dob, pointsI, location, req.getParameter("date"), type); if (aword != null && !aword.equals("")) { String systemToken = RandomToken.generateToken(32); int id = assuree.generatePasswordResetTicket(Page.getUser(req), systemToken, aword); diff --git a/src/org/cacert/gigi/pages/wot/AssuranceForm.templ b/src/org/cacert/gigi/pages/wot/AssuranceForm.templ index a69f3e3a..9b1aa2e5 100644 --- a/src/org/cacert/gigi/pages/wot/AssuranceForm.templ +++ b/src/org/cacert/gigi/pages/wot/AssuranceForm.templ @@ -50,6 +50,10 @@
(Max. ) + + + + checked> diff --git a/src/org/cacert/gigi/pages/wot/AssurePage.java b/src/org/cacert/gigi/pages/wot/AssurePage.java index 39a5aa90..94c582f2 100644 --- a/src/org/cacert/gigi/pages/wot/AssurePage.java +++ b/src/org/cacert/gigi/pages/wot/AssurePage.java @@ -49,17 +49,6 @@ public class AssurePage extends Page { return ac != null && ac.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(); @@ -68,7 +57,12 @@ public class AssurePage extends Page { if (form.submit(out, req)) { out.println(translate(req, "Assurance complete.")); } else { - outputForm(req, resp.getWriter(), form); + try { + Notary.checkAssuranceIsPossible(LoginPage.getUser(req), form.getAssuree()); + form.output(out, getLanguage(req), new HashMap()); + } catch (GigiApiException e) { + e.format(out, Page.getLanguage(req)); + } } return; @@ -92,8 +86,14 @@ public class AssurePage extends Page { } else if (getUser(req).getId() == id) { } else { - AssuranceForm form = new AssuranceForm(req, User.getById(id)); - outputForm(req, out, form); + User assuree = User.getById(id); + User myself = LoginPage.getUser(req); + try { + Notary.checkAssuranceIsPossible(myself, assuree); + new AssuranceForm(req, assuree).output(out, getLanguage(req), new HashMap()); + } catch (GigiApiException e) { + e.format(out, Page.getLanguage(req)); + } } } } else { diff --git a/src/org/cacert/gigi/util/Notary.java b/src/org/cacert/gigi/util/Notary.java index f229bdd4..2f3a3aec 100644 --- a/src/org/cacert/gigi/util/Notary.java +++ b/src/org/cacert/gigi/util/Notary.java @@ -8,6 +8,7 @@ import java.util.GregorianCalendar; import org.cacert.gigi.GigiApiException; import org.cacert.gigi.database.GigiPreparedStatement; import org.cacert.gigi.database.GigiResultSet; +import org.cacert.gigi.dbObjects.Assurance.AssuranceType; import org.cacert.gigi.dbObjects.Group; import org.cacert.gigi.dbObjects.Name; import org.cacert.gigi.dbObjects.User; @@ -71,14 +72,9 @@ public class Notary { * @throws GigiApiException * if the assurance fails (for various reasons) */ - public synchronized static void assure(User assurer, User assuree, Name assureeName, Date dob, int awarded, String location, String date) throws GigiApiException { + public synchronized static void assure(User assurer, User assuree, Name assureeName, Date dob, int awarded, String location, String date, AssuranceType type) throws GigiApiException { + may(assurer, assuree, AssuranceType.FACE_TO_FACE); GigiApiException gae = new GigiApiException(); - if (assuree.isInGroup(ASSUREE_BLOCKED)) { - gae.mergeInto(new GigiApiException("The assuree is blocked.")); - } - if (assurer.isInGroup(ASSURER_BLOCKED)) { - gae.mergeInto(new GigiApiException("The assurer is blocked.")); - } if ( !gae.isEmpty()) { throw gae; } @@ -113,13 +109,37 @@ public class Notary { if ( !assuree.getName().equals(assureeName) || !assuree.getDoB().equals(dob)) { gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details.")); } - if (awarded > assurer.getMaxAssurePoints() || awarded < 0) { + if (awarded < 0) { gae.mergeInto(new GigiApiException("The points you are trying to award are out of range.")); + } else { + if (type == AssuranceType.NUCLEUS) { + if (awarded > 50) { + gae.mergeInto(new GigiApiException("The points you are trying to award are out of range.")); + } + } else { + if (awarded > assurer.getMaxAssurePoints()) { + gae.mergeInto(new GigiApiException("The points you are trying to award are out of range.")); + } + } } + if ( !gae.isEmpty()) { throw gae; } + if (type == AssuranceType.FACE_TO_FACE) { + assureF2F(assurer, assuree, awarded, location, date); + } else if (type == AssuranceType.TTP_ASSISTED) { + assureTTP(assurer, assuree, awarded, location, date); + } else { + throw new GigiApiException("Unknown Assurance type: " + type); + } + assurer.invalidateMadeAssurances(); + assuree.invalidateReceivedAssurances(); + } + + private static void assureF2F(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException { + may(assurer, assuree, AssuranceType.FACE_TO_FACE); try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) { ps.setInt(1, assurer.getId()); ps.setInt(2, assuree.getId()); @@ -128,7 +148,45 @@ public class Notary { ps.setString(5, date); ps.execute(); } - assurer.invalidateMadeAssurances(); - assuree.invalidateReceivedAssurances(); + } + + private static void assureTTP(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException { + may(assurer, assuree, AssuranceType.TTP_ASSISTED); + try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='TTP-Assisted'")) { + ps.setInt(1, assurer.getId()); + ps.setInt(2, assuree.getId()); + ps.setInt(3, awarded); + ps.setString(4, location); + ps.setString(5, date); + ps.execute(); + assuree.revokeGroup(assurer, Group.TTP_APPLICANT); + } + } + + public static void may(User assurer, User assuree, AssuranceType t) throws GigiApiException { + if (assuree.isInGroup(ASSUREE_BLOCKED)) { + throw new GigiApiException("The assuree is blocked."); + } + if (assurer.isInGroup(ASSURER_BLOCKED)) { + throw new GigiApiException("The assurer is blocked."); + } + + if (t == AssuranceType.NUCLEUS) { + if ( !assurer.isInGroup(Group.NUCLEUS_ASSURER)) { + throw new GigiApiException("Assurer needs to be Nucleus Assurer."); + } + return; + } else if (t == AssuranceType.TTP_ASSISTED) { + if ( !assurer.isInGroup(Group.TTP_ASSURER)) { + throw new GigiApiException("Assurer needs to be TTP Assurer."); + } + if ( !assuree.isInGroup(Group.TTP_APPLICANT)) { + throw new GigiApiException("Assuree needs to be TTP Applicant."); + } + return; + } else if (t == AssuranceType.FACE_TO_FACE) { + return; + } + throw new GigiApiException("Assurance type not possible."); } } diff --git a/tests/org/cacert/gigi/util/TestNotary.java b/tests/org/cacert/gigi/util/TestNotary.java index 740c12c6..6fd69d7c 100644 --- a/tests/org/cacert/gigi/util/TestNotary.java +++ b/tests/org/cacert/gigi/util/TestNotary.java @@ -7,6 +7,7 @@ import java.util.Date; import org.cacert.gigi.GigiApiException; import org.cacert.gigi.database.GigiPreparedStatement; +import org.cacert.gigi.dbObjects.Assurance.AssuranceType; import org.cacert.gigi.dbObjects.User; import org.cacert.gigi.output.DateSelector; import org.cacert.gigi.testUtils.ManagedTest; @@ -27,7 +28,7 @@ public class TestNotary extends ManagedTest { }; try { - Notary.assure(assurer, users[0], users[0].getName(), users[0].getDoB(), -1, "test-notary", "2014-01-01"); + Notary.assure(assurer, users[0], users[0].getName(), users[0].getDoB(), -1, "test-notary", "2014-01-01", AssuranceType.FACE_TO_FACE); fail("This shouldn't have passed"); } catch (GigiApiException e) { // expected @@ -36,7 +37,7 @@ public class TestNotary extends ManagedTest { assertEquals(result[i], assurer.getMaxAssurePoints()); assuranceFail(assurer, users[i], result[i] + 1, "test-notary", "2014-01-01"); - Notary.assure(assurer, users[i], users[i].getName(), users[i].getDoB(), result[i], "test-notary", "2014-01-01"); + Notary.assure(assurer, users[i], users[i].getName(), users[i].getDoB(), result[i], "test-notary", "2014-01-01", AssuranceType.FACE_TO_FACE); assuranceFail(assurer, users[i], result[i], "test-notary", "2014-01-01"); } @@ -48,7 +49,7 @@ public class TestNotary extends ManagedTest { private void assuranceFail(User assurer, User user, int i, String location, String date) throws SQLException { try { - Notary.assure(assurer, user, user.getName(), user.getDoB(), i, location, date); + Notary.assure(assurer, user, user.getName(), user.getDoB(), i, location, date, AssuranceType.FACE_TO_FACE); fail("This shouldn't have passed"); } catch (GigiApiException e) { // expected @@ -71,7 +72,7 @@ public class TestNotary extends ManagedTest { for (int i = 0; i < users.length; i++) { assuranceFail(assurer, users[i], -1, "test-notary", "2014-01-01"); assuranceFail(assurer, users[i], 11, "test-notary", "2014-01-01"); - Notary.assure(assurer, users[i], users[i].getName(), users[i].getDoB(), 10, "test-notary", "2014-01-01"); + Notary.assure(assurer, users[i], users[i].getName(), users[i].getDoB(), 10, "test-notary", "2014-01-01", AssuranceType.FACE_TO_FACE); assuranceFail(assurer, users[i], 10, "test-notary", "2014-01-01"); } } @@ -106,7 +107,7 @@ public class TestNotary extends ManagedTest { assuranceFail(assuree, assuranceUser, 10, "notary-junit-test", "2014-01-01"); // valid - Notary.assure(assuranceUser, assuree, assuree.getName(), assuree.getDoB(), 10, "notary-junit-test", "2014-01-01"); + Notary.assure(assuranceUser, assuree, assuree.getName(), assuree.getDoB(), 10, "notary-junit-test", "2014-01-01", AssuranceType.FACE_TO_FACE); // assure double assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "2014-01-01"); diff --git a/util-testing/org/cacert/gigi/pages/Manager.java b/util-testing/org/cacert/gigi/pages/Manager.java index 711f48e2..ebfd73b9 100644 --- a/util-testing/org/cacert/gigi/pages/Manager.java +++ b/util-testing/org/cacert/gigi/pages/Manager.java @@ -26,6 +26,7 @@ import org.cacert.gigi.Gigi; import org.cacert.gigi.GigiApiException; import org.cacert.gigi.crypto.SPKAC; import org.cacert.gigi.database.GigiPreparedStatement; +import org.cacert.gigi.dbObjects.Assurance.AssuranceType; import org.cacert.gigi.dbObjects.Certificate; import org.cacert.gigi.dbObjects.Certificate.CertificateStatus; import org.cacert.gigi.dbObjects.CertificateOwner; @@ -259,7 +260,7 @@ public class Manager extends Page { } try { for (int i = 0; i < getAssurers().length; i++) { - Notary.assure(getAssurers()[i], byEmail, byEmail.getName(), byEmail.getDoB(), 10, "Testmanager Assure up code", "2014-11-06"); + Notary.assure(getAssurers()[i], byEmail, byEmail.getName(), byEmail.getDoB(), 10, "Testmanager Assure up code", "2014-11-06", AssuranceType.FACE_TO_FACE); } } catch (GigiApiException e) { throw new Error(e); -- 2.39.2