From e6b68845c026e91c89d1a3e1f81c27cb2bfb9109 Mon Sep 17 00:00:00 2001 From: Heng Sin Low Date: Wed, 2 Oct 2013 16:13:53 +0800 Subject: [PATCH] IDEMPIERE-1409 Validate format for AD_User.EMail. Peer review and commit patch from Carlos. --- .../oracle/201309302110_IDEMPIERE-1409.sql | 11 +++++++++++ .../postgresql/201309302110_IDEMPIERE-1409.sql | 8 ++++++++ .../src/org/compiere/model/MUser.java | 10 ++++++++++ .../src/org/compiere/util/EMail.java | 17 ++++++++++++++++- 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 migration/i1.0c-release/oracle/201309302110_IDEMPIERE-1409.sql create mode 100644 migration/i1.0c-release/postgresql/201309302110_IDEMPIERE-1409.sql diff --git a/migration/i1.0c-release/oracle/201309302110_IDEMPIERE-1409.sql b/migration/i1.0c-release/oracle/201309302110_IDEMPIERE-1409.sql new file mode 100644 index 0000000000..012d94e648 --- /dev/null +++ b/migration/i1.0c-release/oracle/201309302110_IDEMPIERE-1409.sql @@ -0,0 +1,11 @@ +SET SQLBLANKLINES ON +SET DEFINE OFF + +-- Sep 30, 2013 9:09:59 PM COT +-- IDEMPIERE-1409 Validate format for AD_User.EMail +INSERT INTO AD_Message (MsgType,MsgText,AD_Message_ID,EntityType,AD_Message_UU,Value,IsActive,Updated,CreatedBy,UpdatedBy,AD_Org_ID,Created,AD_Client_ID) VALUES ('E','EMail format is invalid: ',200235,'D','c2fb0472-795b-41ff-806c-1c1e0895e9b3','InvalidEMailFormat','Y',TO_DATE('2013-09-30 21:09:57','YYYY-MM-DD HH24:MI:SS'),100,100,0,TO_DATE('2013-09-30 21:09:57','YYYY-MM-DD HH24:MI:SS'),0) +; + +SELECT register_migration_script('201309302110_IDEMPIERE-1409.sql') FROM dual +; + diff --git a/migration/i1.0c-release/postgresql/201309302110_IDEMPIERE-1409.sql b/migration/i1.0c-release/postgresql/201309302110_IDEMPIERE-1409.sql new file mode 100644 index 0000000000..40f6e63f86 --- /dev/null +++ b/migration/i1.0c-release/postgresql/201309302110_IDEMPIERE-1409.sql @@ -0,0 +1,8 @@ +-- Sep 30, 2013 9:09:59 PM COT +-- IDEMPIERE-1409 Validate format for AD_User.EMail +INSERT INTO AD_Message (MsgType,MsgText,AD_Message_ID,EntityType,AD_Message_UU,Value,IsActive,Updated,CreatedBy,UpdatedBy,AD_Org_ID,Created,AD_Client_ID) VALUES ('E','EMail format is invalid: ',200235,'D','c2fb0472-795b-41ff-806c-1c1e0895e9b3','InvalidEMailFormat','Y',TO_TIMESTAMP('2013-09-30 21:09:57','YYYY-MM-DD HH24:MI:SS'),100,100,0,TO_TIMESTAMP('2013-09-30 21:09:57','YYYY-MM-DD HH24:MI:SS'),0) +; + +SELECT register_migration_script('201309302110_IDEMPIERE-1409.sql') FROM dual +; + diff --git a/org.adempiere.base/src/org/compiere/model/MUser.java b/org.adempiere.base/src/org/compiere/model/MUser.java index e91b7f9e44..e4fbf1fb01 100644 --- a/org.adempiere.base/src/org/compiere/model/MUser.java +++ b/org.adempiere.base/src/org/compiere/model/MUser.java @@ -37,6 +37,7 @@ import org.adempiere.exceptions.DBException; import org.compiere.util.CCache; import org.compiere.util.CLogger; import org.compiere.util.DB; +import org.compiere.util.EMail; import org.compiere.util.Env; import org.compiere.util.Msg; import org.compiere.util.Secure; @@ -896,6 +897,15 @@ public class MUser extends X_AD_User // New Address invalidates verification if (!newRecord && is_ValueChanged("EMail")) setEMailVerifyDate(null); + + // IDEMPIERE-1409 + if (getEMail() != null && (newRecord || is_ValueChanged("EMail"))) { + if (! EMail.validate(getEMail())) { + log.saveError("SaveError", Msg.getMsg(getCtx(), "InvalidEMailFormat") + Msg.getElement(getCtx(), COLUMNNAME_EMail) + " - [" + getEMail() + "]"); + return false; + } + } + if (newRecord || super.getValue() == null || is_ValueChanged("Value")) setValue(super.getValue()); diff --git a/org.adempiere.base/src/org/compiere/util/EMail.java b/org.adempiere.base/src/org/compiere/util/EMail.java index 6dddb76190..f81658d55e 100644 --- a/org.adempiere.base/src/org/compiere/util/EMail.java +++ b/org.adempiere.base/src/org/compiere/util/EMail.java @@ -26,6 +26,8 @@ import java.util.Collection; import java.util.Enumeration; import java.util.Properties; import java.util.logging.Level; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.activation.DataHandler; import javax.activation.DataSource; @@ -71,7 +73,8 @@ public final class EMail implements Serializable /** * */ - private static final long serialVersionUID = -1408649015285763245L; + private static final long serialVersionUID = -5857825644737211294L; + //use in server bean public final static String HTML_MAIL_MARKER = "ContentType=text/html;"; /** @@ -1092,6 +1095,18 @@ public final class EMail implements Serializable return sb.toString (); } // toString + /** + * Validate format of an email address + * IDEMPIERE-1409 - based on http://examples.javacodegeeks.com/core-java/util/regex/matcher/validate-email-address-with-java-regular-expression-example/ + * @return true if email has proper format + */ + private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; + private static Pattern pattern = Pattern.compile(EMAIL_PATTERN); + public static boolean validate(final String email) { + Matcher matcher = pattern.matcher(email); + return matcher.matches(); + } + /************************************************************************** * Test. * java -cp CTools.jar;CClient.jar org.compiere.util.EMail main info@adempiere.org jjanke@adempiere.org "My Subject" "My Message"