]> WPIA git - gigi.git/commitdiff
UPD: check for duplicate Email in the Email class itself.
authorFelix Dörre <felix@dogcraft.de>
Tue, 4 Nov 2014 13:10:10 +0000 (14:10 +0100)
committerJanis Streib <janis@dogcraft.de>
Wed, 31 Dec 2014 01:35:58 +0000 (02:35 +0100)
src/org/cacert/gigi/dbObjects/EmailAddress.java
src/org/cacert/gigi/pages/account/mail/MailAddForm.java
src/org/cacert/gigi/pages/main/Signup.java
tests/org/cacert/gigi/TestObjectCache.java

index f398087d16bf067213afc0b2507557de10cfc658..b945f9eecc23028575b8d4368738ecd5d652c42e 100644 (file)
@@ -45,16 +45,22 @@ public class EmailAddress implements IdCachable {
         this.hash = RandomToken.generateToken(16);
     }
 
-    public void insert(Language l) {
+    public void insert(Language l) throws GigiApiException {
         if (id != 0) {
             throw new IllegalStateException("already inserted.");
         }
         try {
+            GigiPreparedStatement psCheck = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `emails` WHERE email=? AND deleted is NULL");
             GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `emails` SET memid=?, hash=?, email=?");
             ps.setInt(1, owner.getId());
             ps.setString(2, hash);
             ps.setString(3, address);
+            psCheck.setString(1, address);
             synchronized (EmailAddress.class) {
+                GigiResultSet res = psCheck.executeQuery();
+                if (res.next()) {
+                    throw new GigiApiException("The email is currently valid");
+                }
                 ps.execute();
                 id = ps.lastInsertId();
                 myCache.put(this);
index 29a3041db561a0ec7ca6a3f33c9e2c22ea20a56b..d4d46eae8602ec14db93b73b6607010b469fe410 100644 (file)
@@ -5,6 +5,7 @@ import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 
+import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.dbObjects.EmailAddress;
 import org.cacert.gigi.dbObjects.User;
 import org.cacert.gigi.localisation.Language;
@@ -38,6 +39,9 @@ public class MailAddForm extends Form {
         } catch (IllegalArgumentException e) {
             out.println("<div class='formError'>Error: Invalid address!</div>");
             return false;
+        } catch (GigiApiException e) {
+            e.format(out, Page.getLanguage(req));
+            return false;
         }
         return true;
     }
index 07cd608400a2fb83c298ab2486cdbdfadc18be69..d442ebda36b95b2ad0b1e860a331f71d6e905094 100644 (file)
@@ -164,11 +164,14 @@ public class Signup extends Form {
             run(req, pw1);
         } catch (SQLException e) {
             e.printStackTrace();
+        } catch (GigiApiException e) {
+            outputError(out, req, e.getMessage());
+            return false;
         }
         return true;
     }
 
-    private void run(HttpServletRequest req, String password) throws SQLException {
+    private void run(HttpServletRequest req, String password) throws SQLException, GigiApiException {
         try {
             DatabaseConnection.getInstance().beginTransaction();
             buildup.setPreferredLocale(Page.getLanguage(req).getLocale());
index c63da92219ec39c67ad15a4b3cdd206bdc53b44a..dd9c04a5c315e7ca33494268c691a2331d6e171b 100644 (file)
@@ -50,7 +50,7 @@ public class TestObjectCache extends ManagedTest {
     }
 
     @Test
-    public void testEmailCache() {
+    public void testEmailCache() throws GigiApiException {
         EmailAddress em = new EmailAddress(User.getById(uid), createUniqueName() + "@example.org");
         em.insert(Language.getInstance(Locale.ENGLISH));