Skip to content

Commit

Permalink
Merge pull request #2564 from SCADA-LTS/fix/#623_Fixed_email_validati…
Browse files Browse the repository at this point in the history
…on_in_users_details

Fix/#623 fixed email validation in users details
  • Loading branch information
Limraj authored Jun 15, 2023
2 parents d1e03c2 + 632ee89 commit 7ff86fa
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,6 @@ test {
includeTestsMatching "com.serotonin.mango.view.CopyViewTest"
includeTestsMatching "org.scada_lts.dao.IsEventDetectorXidUniqueTest"
includeTestsMatching "com.serotonin.mango.view.export.CsvWriterTest"
includeTestsMatching "com.serotonin.mango.util.EmailValidatorTest"
}
}
18 changes: 18 additions & 0 deletions src/com/serotonin/mango/util/EmailValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.serotonin.mango.util;

import org.scada_lts.serorepl.utils.StringUtils;

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class EmailValidator {
private static final String EMAIL_PATTERN =
"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";

public static boolean isValidEmail(String email) {
if(StringUtils.isEmpty(email))
return false;
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email.toLowerCase());
return matcher.matches();
}
}
3 changes: 2 additions & 1 deletion src/com/serotonin/mango/vo/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.serotonin.mango.db.dao.DataPointDao;
import com.serotonin.mango.db.dao.DataSourceDao;
import com.serotonin.mango.rt.dataImage.SetPointSource;
import com.serotonin.mango.util.EmailValidator;
import com.serotonin.mango.util.LocalizableJsonException;
import com.serotonin.mango.vo.dataSource.DataSourceVO;
import com.serotonin.mango.vo.permission.DataPointAccess;
Expand Down Expand Up @@ -510,7 +511,7 @@ public void validate(DwrResponseI18n response) {
if (StringUtils.isEmpty(username))
response.addMessage("username", new LocalizableMessage(
"validate.required"));
if (StringUtils.isEmpty(email))
if (StringUtils.isEmpty(email) || !EmailValidator.isValidEmail(email))
response.addMessage("email", new LocalizableMessage(
"validate.required"));
if (id == Common.NEW_ID && StringUtils.isEmpty(password))
Expand Down
65 changes: 65 additions & 0 deletions test/com/serotonin/mango/util/EmailValidatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.serotonin.mango.util;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class EmailValidatorTest {

@Parameterized.Parameters(name= "{index}: email: {0}, valid: {1}")
public static Object[][] data() {
return new Object[][] {
new Object[] {"", false},
new Object[] {"\t", false},
new Object[] {" ", false},
new Object[] {null, false},
new Object[] {"Email@com.com", true},
new Object[] {"email@Com.co", true},
new Object[] {"email@com.c", true},
new Object[] {"email@com.", false},
new Object[] {"email@co.com", true},
new Object[] {"email@c.com", true},
new Object[] {"email@.com", false},
new Object[] {"emailcom.com", false},
new Object[] {"email@@.cm", false},
new Object[] {"e@mail@com.com", false},
new Object[] {"@email@com.com", false},
new Object[] {"emai@l@com.com", false},
new Object[] {"email@com.c@om", false},
new Object[] {"email@com.@com", false},
new Object[] {"email@com@.com", false},
new Object[] {"email@com.com@", false},
new Object[] {"email@dewa.c", true},
new Object[] {"email@@@a.om", false},
new Object[] {"email@ac.cm", true},
new Object[] {"ertyu@ac.cm", true},
new Object[] {"e@ac.cm", true},
new Object[] {"@ac.cm", false},
new Object[] {"ac.cm", false},
new Object[] {"ac.@cm", false},
new Object[] {"test2@test", false},
new Object[] {"Email@com.com", true},
new Object[] {"Email@Com.com", true},
new Object[] {"Email@com.Com", true},
new Object[] {"eMAil@com.com", true},
};
}

private final String email;
private final boolean isvalid;

public EmailValidatorTest(String email, boolean isvalid) {
this.email = email;
this.isvalid = isvalid;
}

@Test
public void when_isValidEmail() {
//when:
boolean result = EmailValidator.isValidEmail(email);
//then:
Assert.assertEquals(isvalid, result);
}
}

0 comments on commit 7ff86fa

Please sign in to comment.