add: check that new email address is not linked to organisation domain
[gigi.git] / src / club / wpia / gigi / dbObjects / EmailAddress.java
index f8629d9..935877a 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();
+        }
+    }
 }