IDEMPIERE-1409 Validate format for AD_User.EMail. Peer review and commit patch from Carlos.

This commit is contained in:
Heng Sin Low 2013-10-02 16:13:53 +08:00
parent 4a94fcd0f5
commit e6b68845c0
4 changed files with 45 additions and 1 deletions

View File

@ -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
;

View File

@ -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
;

View File

@ -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());

View File

@ -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"