]> WPIA git - gigi.git/commitdiff
Merge "add: implement password change log"
authorLucas Werkmeister <mail@lucaswerkmeister.de>
Sat, 14 Sep 2019 12:34:17 +0000 (14:34 +0200)
committerGerrit Code Review <gigi-system@dogcraft.de>
Sat, 14 Sep 2019 12:34:17 +0000 (14:34 +0200)
src/club/wpia/gigi/dbObjects/EmailAddress.java
tests/club/wpia/gigi/dbObjects/TestEmailAddress.java [new file with mode: 0644]

index f8629d946bc85bc396947d2c360a2314704babde..935877a6d7a594e89955b0c46458130f9a782863 100644 (file)
@@ -62,6 +62,11 @@ public class EmailAddress implements IdCachable, Verifyable {
                         throw new GigiApiException("The email address is already known to the system.");
                     }
                 }
+
+                if (isOrgMailAddress()) {
+                    throw new GigiApiException("The entered email address belongs to a registered organisation. Please contact the organisation to issue certificates for this email address.");
+                }
+
                 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `emails` SET memid=?, email=?")) {
                     ps.setInt(1, owner.getId());
                     ps.setString(2, address);
@@ -181,4 +186,14 @@ public class EmailAddress implements IdCachable, Verifyable {
             return results.toArray(new EmailAddress[results.size()]);
         }
     }
+
+    public boolean isOrgMailAddress() {
+        String[] parts = address.split("@");
+
+        try (GigiPreparedStatement statmt = new GigiPreparedStatement("SELECT 1 FROM `domains` AS d, `organisations` AS o WHERE d.`domain` = ? AND d.`deleted` IS NULL AND d.`memid` = o.`id`")) {
+            statmt.setString(1, parts[parts.length - 1]);
+            GigiResultSet e = statmt.executeQuery();
+            return e.next();
+        }
+    }
 }
diff --git a/tests/club/wpia/gigi/dbObjects/TestEmailAddress.java b/tests/club/wpia/gigi/dbObjects/TestEmailAddress.java
new file mode 100644 (file)
index 0000000..d6ed1d9
--- /dev/null
@@ -0,0 +1,60 @@
+package club.wpia.gigi.dbObjects;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.util.Locale;
+
+import org.junit.Test;
+
+import club.wpia.gigi.GigiApiException;
+import club.wpia.gigi.testUtils.OrgTest;
+
+public class TestEmailAddress extends OrgTest {
+
+    public TestEmailAddress() throws IOException, GigiApiException {
+
+    }
+
+    @Test
+    public void testAddEmail() {
+        String email = createUniqueName() + "@email.com";
+        assertNull(addEmailAddress(email));
+
+        // add known email address
+        assertEquals("The email address is already known to the system.", addEmailAddress(email));
+
+        // add invalid email address
+        email = createUniqueName();
+        assertEquals("Invalid email.", addEmailAddress(email));
+
+        // add invalid email address
+        email = createUniqueName() + "@email";
+        assertEquals("Invalid email.", addEmailAddress(email));
+
+        // add email address with organisation domain
+        String dom = createUniqueName() + ".de";
+        try {
+            Organisation o1 = createUniqueOrg();
+            new Domain(u, o1, dom);
+        } catch (GigiApiException e) {
+            // nothing to do
+        }
+        email = createUniqueName() + "@" + dom;
+        assertEquals("The entered email address belongs to a registered organisation. Please contact the organisation to issue certificates for this email address.", addEmailAddress(email));
+
+    }
+
+    private String addEmailAddress(String email) {
+        try {
+            EmailAddress addr = new EmailAddress(u, email, Locale.ENGLISH);
+            getMailReceiver().receive(addr.getAddress());
+        } catch (IllegalArgumentException e) {
+            return e.getMessage();
+        } catch (GigiApiException e) {
+            return e.getMessage();
+        }
+        return null;
+    }
+
+}