]> WPIA git - gigi.git/blobdiff - src/club/wpia/gigi/dbObjects/EmailAddress.java
fix: check if email is a valid pattern while account creation
[gigi.git] / src / club / wpia / gigi / dbObjects / EmailAddress.java
index 319e415b93e86a97d89da13ddcf5b18e8df12ec8..935877a6d7a594e89955b0c46458130f9a782863 100644 (file)
@@ -13,6 +13,7 @@ import club.wpia.gigi.email.MailProbe;
 import club.wpia.gigi.localisation.Language;
 import club.wpia.gigi.output.template.SprintfCommand;
 import club.wpia.gigi.util.RandomToken;
+import club.wpia.gigi.util.TimeConditions;
 
 public class EmailAddress implements IdCachable, Verifyable {
 
@@ -61,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);
@@ -110,7 +116,7 @@ public class EmailAddress implements IdCachable, Verifyable {
             stmt.setInt(2, owner.getId());
             stmt.setString(3, hash);
             if ( !stmt.executeMaybeUpdate()) {
-                throw new IllegalArgumentException("Given token could not be found to complete the verification process (Domain Ping).");
+                throw new IllegalArgumentException("Given token could not be found to complete the verification process (Email Ping).");
             }
         }
         // Verify user with that primary email
@@ -122,9 +128,10 @@ public class EmailAddress implements IdCachable, Verifyable {
     }
 
     public boolean isVerified() {
-        try (GigiPreparedStatement statmt = new GigiPreparedStatement("SELECT 1 FROM `emailPinglog` WHERE `email`=? AND `uid`=? AND `type`='active' AND `status`='success'")) {
+        try (GigiPreparedStatement statmt = new GigiPreparedStatement("SELECT 1 FROM `emailPinglog` WHERE `email`=? AND `uid`=? AND `type`='active' AND `status`='success' AND `when` > (now() - interval '1 months' * ?::INTEGER)")) {
             statmt.setString(1, address);
             statmt.setInt(2, owner.getId());
+            statmt.setInt(3, TimeConditions.getInstance().getEmailPingMonths());
             GigiResultSet e = statmt.executeQuery();
             return e.next();
         }
@@ -179,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();
+        }
+    }
 }