From 47c7ef9db6c7a688853f338495ba61e3d827b2d2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Fri, 6 Nov 2015 12:30:33 +0100 Subject: [PATCH] add: show support and user the account history and the trainings. --- src/org/cacert/gigi/Gigi.java | 8 ++- .../cacert/gigi/dbObjects/SupportedUser.java | 2 +- src/org/cacert/gigi/dbObjects/User.java | 29 +++++++++- .../gigi/pages/account/MyDetailsForm.templ | 2 +- .../pages/account/MyDetailsFormAssured.templ | 2 +- .../gigi/pages/account/UserHistory.java | 53 +++++++++++++++++++ .../gigi/pages/account/UserHistory.templ | 9 ++++ .../gigi/pages/account/UserTrainings.java | 52 ++++++++++++++++++ .../gigi/pages/account/UserTrainings.templ | 9 ++++ .../admin/support/SupportUserHistory.java | 39 -------------- 10 files changed, 160 insertions(+), 45 deletions(-) create mode 100644 src/org/cacert/gigi/pages/account/UserHistory.java create mode 100644 src/org/cacert/gigi/pages/account/UserHistory.templ create mode 100644 src/org/cacert/gigi/pages/account/UserTrainings.java create mode 100644 src/org/cacert/gigi/pages/account/UserTrainings.templ delete mode 100644 src/org/cacert/gigi/pages/admin/support/SupportUserHistory.java diff --git a/src/org/cacert/gigi/Gigi.java b/src/org/cacert/gigi/Gigi.java index b347a63f..00a10f4a 100644 --- a/src/org/cacert/gigi/Gigi.java +++ b/src/org/cacert/gigi/Gigi.java @@ -43,6 +43,8 @@ import org.cacert.gigi.pages.TestSecure; import org.cacert.gigi.pages.Verify; import org.cacert.gigi.pages.account.ChangePasswordPage; import org.cacert.gigi.pages.account.MyDetails; +import org.cacert.gigi.pages.account.UserHistory; +import org.cacert.gigi.pages.account.UserTrainings; import org.cacert.gigi.pages.account.certs.CertificateAdd; import org.cacert.gigi.pages.account.certs.Certificates; import org.cacert.gigi.pages.account.domain.DomainOverview; @@ -52,7 +54,6 @@ import org.cacert.gigi.pages.admin.support.FindDomainPage; import org.cacert.gigi.pages.admin.support.FindUserPage; import org.cacert.gigi.pages.admin.support.SupportEnterTicketPage; import org.cacert.gigi.pages.admin.support.SupportUserDetailsPage; -import org.cacert.gigi.pages.admin.support.SupportUserHistory; import org.cacert.gigi.pages.error.AccessDenied; import org.cacert.gigi.pages.error.PageNotFound; import org.cacert.gigi.pages.main.RegisterPage; @@ -142,7 +143,10 @@ public class Gigi extends HttpServlet { putPage(FindDomainPage.PATH, new FindDomainPage("Find Domain"), "System Admin"); putPage(SupportEnterTicketPage.PATH, new SupportEnterTicketPage(), "System Admin"); putPage(SupportUserDetailsPage.PATH + "*", new SupportUserDetailsPage("Support: User Details"), null); - putPage(SupportUserHistory.PATH, new SupportUserHistory(), null); + putPage(UserHistory.PATH, new UserHistory(false), "My Account"); + putPage(UserHistory.SUPPORT_PATH, new UserHistory(true), null); + putPage(UserTrainings.PATH, new UserTrainings(false), "My Account"); + putPage(UserTrainings.SUPPORT_PATH, new UserTrainings(true), null); if (testing) { try { Class manager = Class.forName("org.cacert.gigi.pages.Manager"); diff --git a/src/org/cacert/gigi/dbObjects/SupportedUser.java b/src/org/cacert/gigi/dbObjects/SupportedUser.java index 17d7c8cf..decf5527 100644 --- a/src/org/cacert/gigi/dbObjects/SupportedUser.java +++ b/src/org/cacert/gigi/dbObjects/SupportedUser.java @@ -54,7 +54,7 @@ public class SupportedUser { if (ticket == null) { throw new GigiApiException("No ticket set!"); } - GigiPreparedStatement prep = DatabaseConnection.getInstance().prepare("INSERT INTO adminLog SET uid=?, admin=?, type=?, information=?"); + GigiPreparedStatement prep = DatabaseConnection.getInstance().prepare("INSERT INTO `adminLog` SET uid=?, admin=?, type=?, information=?"); prep.setInt(1, target.getId()); prep.setInt(2, supporter.getId()); prep.setString(3, type); diff --git a/src/org/cacert/gigi/dbObjects/User.java b/src/org/cacert/gigi/dbObjects/User.java index 1b4b52ea..f1200233 100644 --- a/src/org/cacert/gigi/dbObjects/User.java +++ b/src/org/cacert/gigi/dbObjects/User.java @@ -15,6 +15,7 @@ import org.cacert.gigi.database.DatabaseConnection; import org.cacert.gigi.database.GigiPreparedStatement; import org.cacert.gigi.database.GigiResultSet; import org.cacert.gigi.localisation.Language; +import org.cacert.gigi.output.DateSelector; import org.cacert.gigi.util.Notary; import org.cacert.gigi.util.PasswordHash; import org.cacert.gigi.util.PasswordStrengthChecker; @@ -137,7 +138,7 @@ public class User extends CertificateOwner { } public boolean hasPassedCATS() { - GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?"); + GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=? AND `variant_id`=1"); query.setInt(1, getId()); try (GigiResultSet rs = query.executeQuery()) { if (rs.next()) { @@ -468,4 +469,30 @@ public class User extends CertificateOwner { return false; } + public String[] getAdminLog() { + GigiPreparedStatement prep = DatabaseConnection.getInstance().prepare("SELECT `when`, type, information FROM `adminLog` WHERE uid=? ORDER BY `when` ASC"); + prep.setInt(1, getId()); + GigiResultSet res = prep.executeQuery(); + List entries = new LinkedList(); + + while (res.next()) { + entries.add(res.getString(2) + " (" + res.getString(3) + ")"); + } + + return entries.toArray(new String[0]); + } + + public String[] getTrainings() { + GigiPreparedStatement prep = DatabaseConnection.getInstance().prepare("SELECT `pass_date`, `type_text` FROM `cats_passed` LEFT JOIN `cats_type` ON `cats_type`.`id`=`cats_passed`.`variant_id` WHERE `user_id`=? ORDER BY `pass_date` ASC"); + prep.setInt(1, getId()); + GigiResultSet res = prep.executeQuery(); + List entries = new LinkedList(); + + while (res.next()) { + + entries.add(DateSelector.getDateFormat().format(res.getTimestamp(1)) + " (" + res.getString(2) + ")"); + } + + return entries.toArray(new String[0]); + } } diff --git a/src/org/cacert/gigi/pages/account/MyDetailsForm.templ b/src/org/cacert/gigi/pages/account/MyDetailsForm.templ index ec290a8b..ea1e9fca 100644 --- a/src/org/cacert/gigi/pages/account/MyDetailsForm.templ +++ b/src/org/cacert/gigi/pages/account/MyDetailsForm.templ @@ -30,7 +30,7 @@ - + diff --git a/src/org/cacert/gigi/pages/account/MyDetailsFormAssured.templ b/src/org/cacert/gigi/pages/account/MyDetailsFormAssured.templ index 6d731071..a3ad85d5 100644 --- a/src/org/cacert/gigi/pages/account/MyDetailsFormAssured.templ +++ b/src/org/cacert/gigi/pages/account/MyDetailsFormAssured.templ @@ -30,7 +30,7 @@ - + diff --git a/src/org/cacert/gigi/pages/account/UserHistory.java b/src/org/cacert/gigi/pages/account/UserHistory.java new file mode 100644 index 00000000..53e81a23 --- /dev/null +++ b/src/org/cacert/gigi/pages/account/UserHistory.java @@ -0,0 +1,53 @@ +package org.cacert.gigi.pages.account; + +import java.io.IOException; +import java.util.HashMap; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.cacert.gigi.dbObjects.User; +import org.cacert.gigi.output.template.OutputableArrayIterable; +import org.cacert.gigi.pages.Page; +import org.cacert.gigi.util.AuthorizationContext; + +public class UserHistory extends Page { + + public static final String SUPPORT_PATH = "/support/user/*/history"; + + public static final String PATH = "/account/history"; + + private static final int intStart = SUPPORT_PATH.indexOf('*'); + + private boolean support; + + public UserHistory(boolean support) { + super(support ? "Support user history" : "History"); + this.support = support; + } + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + User u; + if (support) { + String info = req.getPathInfo(); + int id = Integer.parseInt(info.substring(intStart, info.length() - SUPPORT_PATH.length() + intStart + 1)); + u = User.getById(id); + if (u == null) { + resp.sendError(404); + return; + } + } else { + u = getUser(req); + } + String[] adminLog = u.getAdminLog(); + HashMap vars = new HashMap<>(); + vars.put("entries", new OutputableArrayIterable(adminLog, "entry")); + getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars); + } + + @Override + public boolean isPermitted(AuthorizationContext ac) { + return ac != null && ( !support || ac.canSupport()); + } +} diff --git a/src/org/cacert/gigi/pages/account/UserHistory.templ b/src/org/cacert/gigi/pages/account/UserHistory.templ new file mode 100644 index 00000000..238f9c41 --- /dev/null +++ b/src/org/cacert/gigi/pages/account/UserHistory.templ @@ -0,0 +1,9 @@ + + + + + + + +
+ diff --git a/src/org/cacert/gigi/pages/account/UserTrainings.java b/src/org/cacert/gigi/pages/account/UserTrainings.java new file mode 100644 index 00000000..8c117d5e --- /dev/null +++ b/src/org/cacert/gigi/pages/account/UserTrainings.java @@ -0,0 +1,52 @@ +package org.cacert.gigi.pages.account; + +import java.io.IOException; +import java.util.HashMap; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.cacert.gigi.dbObjects.User; +import org.cacert.gigi.output.template.OutputableArrayIterable; +import org.cacert.gigi.pages.Page; +import org.cacert.gigi.util.AuthorizationContext; + +public class UserTrainings extends Page { + + public static final String SUPPORT_PATH = "/support/user/*/trainings"; + + public static final String PATH = "/account/trainings"; + + private static final int intStart = SUPPORT_PATH.indexOf('*'); + + private boolean support; + + public UserTrainings(boolean support) { + super(support ? "Support User Trainings" : "Trainings"); + this.support = support; + } + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + User u; + if (support) { + String info = req.getPathInfo(); + int id = Integer.parseInt(info.substring(intStart, info.length() - SUPPORT_PATH.length() + intStart + 1)); + u = User.getById(id); + if (u == null) { + resp.sendError(404); + return; + } + } else { + u = getUser(req); + } + HashMap vars = new HashMap<>(); + vars.put("entries", new OutputableArrayIterable(u.getTrainings(), "entry")); + getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars); + } + + @Override + public boolean isPermitted(AuthorizationContext ac) { + return ac != null && ( !support || ac.canSupport()); + } +} diff --git a/src/org/cacert/gigi/pages/account/UserTrainings.templ b/src/org/cacert/gigi/pages/account/UserTrainings.templ new file mode 100644 index 00000000..b4eb0060 --- /dev/null +++ b/src/org/cacert/gigi/pages/account/UserTrainings.templ @@ -0,0 +1,9 @@ + + + + + + + +
+ diff --git a/src/org/cacert/gigi/pages/admin/support/SupportUserHistory.java b/src/org/cacert/gigi/pages/admin/support/SupportUserHistory.java deleted file mode 100644 index 02ad8b0a..00000000 --- a/src/org/cacert/gigi/pages/admin/support/SupportUserHistory.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.cacert.gigi.pages.admin.support; - -import java.io.IOException; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.cacert.gigi.dbObjects.Group; -import org.cacert.gigi.dbObjects.User; -import org.cacert.gigi.pages.Page; -import org.cacert.gigi.util.AuthorizationContext; - -public class SupportUserHistory extends Page { - - public static final String PATH = "/support/user/*/history"; - - private static final int intStart = PATH.indexOf('*'); - - public SupportUserHistory() { - super("Support user history"); - } - - @Override - public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { - String info = req.getPathInfo(); - int id = Integer.parseInt(info.substring(intStart, info.length() - PATH.length() + intStart + 1)); - User u = User.getById(id); - if (u == null) { - resp.sendError(404); - return; - } - // TODO get Admin log - } - - @Override - public boolean isPermitted(AuthorizationContext ac) { - return ac != null && ac.isInGroup(Group.SUPPORTER); - } -} -- 2.39.2