upd: change the find user routine to search for all email addresses
authorINOPIAE <m.maengel@inopiae.de>
Sat, 30 Jul 2016 20:55:06 +0000 (22:55 +0200)
committerINOPIAE <m.maengel@inopiae.de>
Tue, 2 Aug 2016 17:49:00 +0000 (19:49 +0200)
instead of just the primary one

fixes issue #99

Change-Id: I3ed6e9007357d2091a832aabbd3c97249af77ff2

src/org/cacert/gigi/dbObjects/EmailAddress.java
src/org/cacert/gigi/pages/admin/support/FindUserForm.java
src/org/cacert/gigi/pages/admin/support/FindUserPage.java

index 964d1b6..a320816 100644 (file)
@@ -2,6 +2,7 @@ package org.cacert.gigi.dbObjects;
 
 import java.io.IOException;
 import java.util.Date;
+import java.util.LinkedList;
 import java.util.Locale;
 
 import org.cacert.gigi.GigiApiException;
@@ -149,4 +150,20 @@ public class EmailAddress implements IdCachable, Verifyable {
         }
         return em;
     }
+
+    public User getOwner() {
+        return owner;
+    }
+
+    public static EmailAddress[] findByAllEmail(String mail) {
+        LinkedList<EmailAddress> results = new LinkedList<EmailAddress>();
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `emails`.`id` FROM `emails` INNER JOIN `users` ON `users`.`id` = `emails`.`memid` INNER JOIN `certOwners` ON `certOwners`.`id` = `users`.`id` WHERE `emails`.`email` LIKE ? AND `emails`.`deleted` IS NULL AND `certOwners`.`deleted` IS NULL ORDER BY `users`.`id`, `emails`.`email` LIMIT 100")) {
+            ps.setString(1, mail);
+            GigiResultSet rs = ps.executeQuery();
+            while (rs.next()) {
+                results.add(EmailAddress.getById(rs.getInt(1)));
+            }
+            return results.toArray(new EmailAddress[results.size()]);
+        }
+    }
 }
index 3a7bf07..025e28a 100644 (file)
@@ -6,7 +6,7 @@ import java.util.Map;
 import javax.servlet.http.HttpServletRequest;
 
 import org.cacert.gigi.GigiApiException;
-import org.cacert.gigi.dbObjects.User;
+import org.cacert.gigi.dbObjects.EmailAddress;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.output.template.Form;
 import org.cacert.gigi.output.template.SprintfCommand;
@@ -14,7 +14,7 @@ import org.cacert.gigi.output.template.Template;
 
 public class FindUserForm extends Form {
 
-    private User users[];
+    private EmailAddress emails[];
 
     private static final Template t = new Template(FindDomainForm.class.getResource("FindUserForm.templ"));
 
@@ -24,11 +24,11 @@ public class FindUserForm extends Form {
 
     @Override
     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
-        User[] users = User.findByEmail(req.getParameter("email"));
-        if (users.length == 0) {
+        EmailAddress[] emails = EmailAddress.findByAllEmail(req.getParameter("email"));
+        if (emails.length == 0) {
             throw new GigiApiException(SprintfCommand.createSimple("No users found matching {0}", req.getParameter("email")));
         }
-        this.users = users;
+        this.emails = emails;
         return true;
     }
 
@@ -37,8 +37,8 @@ public class FindUserForm extends Form {
         t.output(out, l, vars);
     }
 
-    public User[] getUsers() {
-        return users;
+    public EmailAddress[] getEmails() {
+        return emails;
     }
 
 }
index 059298f..02e2889 100644 (file)
@@ -8,7 +8,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.cacert.gigi.GigiApiException;
-import org.cacert.gigi.dbObjects.User;
+import org.cacert.gigi.dbObjects.EmailAddress;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.output.template.Form;
 import org.cacert.gigi.output.template.IterableDataset;
@@ -35,9 +35,9 @@ public class FindUserPage extends Page {
         FindUserForm form = Form.getForm(req, FindUserForm.class);
         try {
             form.submit(resp.getWriter(), req);
-            final User[] users = form.getUsers();
-            if (users.length == 1) {
-                resp.sendRedirect(SupportUserDetailsPage.PATH + users[0].getId());
+            final EmailAddress[] emails = form.getEmails();
+            if (emails.length == 1) {
+                resp.sendRedirect(SupportUserDetailsPage.PATH + emails[0].getOwner().getId());
             } else {
                 HashMap<String, Object> vars = new HashMap<String, Object>();
                 vars.put("first", false);
@@ -47,11 +47,11 @@ public class FindUserPage extends Page {
 
                     @Override
                     public boolean next(Language l, Map<String, Object> vars) {
-                        if (i == users.length) {
+                        if (i == emails.length) {
                             return false;
                         }
-                        vars.put("usrid", users[i].getId());
-                        vars.put("usermail", users[i].getEmail());
+                        vars.put("usrid", emails[i].getOwner().getId());
+                        vars.put("usermail", emails[i].getAddress());
                         i++;
                         return true;
                     }