소스 검색

fix regression that prevented email items to be sent with an error about invalid from address

Jason Rivard 8 년 전
부모
커밋
05eff6061b

+ 17 - 4
src/main/java/password/pwm/config/FormConfiguration.java

@@ -22,6 +22,7 @@
 
 package password.pwm.config;
 
+import org.apache.commons.validator.routines.EmailValidator;
 import password.pwm.AppProperty;
 import password.pwm.PwmConstants;
 import password.pwm.error.*;
@@ -314,10 +315,22 @@ public class FormConfiguration implements Serializable {
         return returnList;
     }
 
+    /**
+     * Return false if an invalid email address is issued.
+     * @param config
+     * @param address
+     * @return
+     */
     public static boolean testEmailAddress(final Configuration config, final String address) {
-        final String patternStr = config.readAppProperty(AppProperty.FORM_EMAIL_REGEX);
-        final Pattern pattern = Pattern.compile(patternStr);
-        final Matcher matcher = pattern.matcher(address);
-        return matcher.matches();
+        if (config != null) {
+            final String patternStr = config.readAppProperty(AppProperty.FORM_EMAIL_REGEX);
+            if (patternStr != null && !patternStr.isEmpty()) {
+                final Pattern pattern = Pattern.compile(patternStr);
+                final Matcher matcher = pattern.matcher(address);
+                return matcher.matches();
+            }
+        }
+
+        return EmailValidator.getInstance(true, true).isValid(address);
     }
 }

+ 1 - 1
src/main/java/password/pwm/config/PwmSetting.java

@@ -255,7 +255,7 @@ public enum PwmSetting {
     EMAIL_SERVER_PORT(
             "email.smtp.port", PwmSettingSyntax.NUMERIC, PwmSettingCategory.EMAIL_SETTINGS),
     EMAIL_DEFAULT_FROM_ADDRESS(
-            "email.default.fromAddress", PwmSettingSyntax.EMAIL_ADDRESS, PwmSettingCategory.EMAIL_SETTINGS),
+            "email.default.fromAddress", PwmSettingSyntax.STRING, PwmSettingCategory.EMAIL_SETTINGS),
     EMAIL_USERNAME(
             "email.smtp.username", PwmSettingSyntax.STRING, PwmSettingCategory.EMAIL_SETTINGS),
     EMAIL_PASSWORD(

+ 6 - 0
src/main/java/password/pwm/config/PwmSettingFlag.java

@@ -22,6 +22,9 @@
 
 package password.pwm.config;
 
+/**
+ * Flags defined for {@link PwmSetting} values.  Flags typically correspond to one or more {@link PwmSettingSyntax} types.
+ */
 public enum PwmSettingFlag {
     /* Marker to indicate in setting UI and generated docs that setting supports macros */
     MacroSupport,
@@ -29,6 +32,9 @@ public enum PwmSettingFlag {
     /* Setting uses LDAP DN syntax */
     ldapDNsyntax,
 
+    /* Setting must be a valid email address format */
+    emailSyntax,
+
     /* No Default - Makes the setting UI act as if there is not a default to reset to */
     NoDefault,
 

+ 5 - 1
src/main/java/password/pwm/config/PwmSettingSyntax.java

@@ -24,6 +24,11 @@ package password.pwm.config;
 
 import password.pwm.config.value.*;
 
+/**
+ * Setting syntax definitions.  Each syntax listed here corresponds to some type of native Java object.  The factory specified includes
+ * methods for marshaling to and from XML and JSON formats.  For user-facing syntactical differences in format, see the
+ * {@link PwmSetting#getRegExPattern()} or use a {@link PwmSettingFlag} type.
+ */
 public enum PwmSettingSyntax {
     STRING(StringValue.factory()),
     USER_PERMISSION(UserPermissionValue.factory()),
@@ -40,7 +45,6 @@ public enum PwmSettingSyntax {
     FORM(FormValue.factory()),
     ACTION(ActionValue.factory()),
     EMAIL(EmailValue.factory()),
-    EMAIL_ADDRESS(EmailAddressValue.factory()),
     X509CERT(X509CertificateValue.factory()),
     CHALLENGE(ChallengeValue.factory()),
     OPTIONLIST(OptionListValue.factory()),

+ 0 - 95
src/main/java/password/pwm/config/value/EmailAddressValue.java

@@ -1,95 +0,0 @@
-/*
- * Password Management Servlets (PWM)
- * http://www.pwm-project.org
- *
- * Copyright (c) 2006-2009 Novell, Inc.
- * Copyright (c) 2009-2016 The PWM Project
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-package password.pwm.config.value;
-
-import java.util.Collections;
-import java.util.List;
-import java.util.Locale;
-
-import org.apache.commons.validator.routines.EmailValidator;
-import org.jdom2.CDATA;
-import org.jdom2.Element;
-
-import password.pwm.config.PwmSetting;
-import password.pwm.config.StoredValue;
-import password.pwm.util.JsonUtil;
-import password.pwm.util.secure.PwmSecurityKey;
-
-public class EmailAddressValue extends AbstractValue implements StoredValue {
-    private static final long serialVersionUID = 1L;
-
-    protected String value;
-
-    EmailAddressValue() {
-    }
-
-    public EmailAddressValue(final String value) {
-        this.value = value == null ? "" : value;
-    }
-
-    public static StoredValueFactory factory() {
-        return new StoredValueFactory() {
-            public EmailAddressValue fromJson(final String input) {
-                final String newValue = JsonUtil.deserialize(input, String.class);
-                return new EmailAddressValue(newValue);
-            }
-
-            public EmailAddressValue fromXmlElement(final Element settingElement, final PwmSecurityKey key) {
-                final Element valueElement = settingElement.getChild("value");
-                return new EmailAddressValue(valueElement == null ? "" : valueElement.getText());
-            }
-        };
-    }
-
-    public List<Element> toXmlValues(final String valueElementName) {
-        final Element valueElement = new Element(valueElementName);
-        valueElement.addContent(new CDATA(value));
-        return Collections.singletonList(valueElement);
-    }
-
-    public String toNativeObject() {
-        return value;
-    }
-
-    @Override
-    public List<String> validateValue(PwmSetting pwmSetting) {
-        if (pwmSetting.isRequired()) {
-            if (value == null || value.length() < 1) {
-                return Collections.singletonList("required value missing");
-            }
-        }
-
-        if (value != null) {
-            if (!EmailValidator.getInstance(true, true).isValid(value)) {
-                return Collections.singletonList("Invalid email address format: '" + value + "'");
-            }
-        }
-
-        return Collections.emptyList();
-    }
-
-    @Override
-    public String toDebugString(Locale locale) {
-        return value == null ? "" : value;
-    }
-}

+ 10 - 0
src/main/java/password/pwm/config/value/StringValue.java

@@ -24,7 +24,9 @@ package password.pwm.config.value;
 
 import org.jdom2.CDATA;
 import org.jdom2.Element;
+import password.pwm.config.FormConfiguration;
 import password.pwm.config.PwmSetting;
+import password.pwm.config.PwmSettingFlag;
 import password.pwm.config.StoredValue;
 import password.pwm.util.JsonUtil;
 import password.pwm.util.secure.PwmSecurityKey;
@@ -87,6 +89,14 @@ public class StringValue extends AbstractValue implements StoredValue {
             }
         }
 
+        if (pwmSetting.getFlags().contains(PwmSettingFlag.emailSyntax)) {
+            if (value != null) {
+                if (!FormConfiguration.testEmailAddress(null, value)) {
+                    return Collections.singletonList("Invalid email address format: '" + value + "'");
+                }
+            }
+        }
+
         return Collections.emptyList();
     }
 

+ 1 - 1
src/main/resources/password/pwm/AppProperty.properties

@@ -59,7 +59,7 @@ configEditor.idleTimeoutSeconds=900
 configGuide.idleTimeoutSeconds=3600
 configManager.zipDebug.maxLogLines=100000
 configManager.zipDebug.maxLogSeconds=30
-form.email.regexTest=^[_+a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*$
+form.email.regexTest=
 healthCheck.nominalCheckIntervalSeconds=60
 healthCheck.minimumCheckIntervalSeconds=10
 healthCheck.maximumRecordAgeSeconds=300

+ 3 - 0
src/main/resources/password/pwm/config/PwmSetting.xml

@@ -670,6 +670,7 @@
         </properties>
     </setting>
     <setting hidden="false" key="email.default.fromAddress" level="1">
+        <flag>emailSyntax</flag>
         <default>
             <value>noreply@example.org</value>
         </default>
@@ -1469,6 +1470,7 @@
         </default>
     </setting>
     <setting hidden="false" key="email.adminAlert.toAddress" level="1">
+        <flag>emailSyntax</flag>
         <default/>
         <example><![CDATA[admin@example.com]]></example>
     </setting>
@@ -1490,6 +1492,7 @@
         </options>
     </setting>
     <setting hidden="false" key="audit.userEvent.toAddress" level="1">
+        <flag>emailSyntax</flag>
         <default/>
         <example><![CDATA[admin@example.com]]></example>
     </setting>

+ 0 - 4
src/main/webapp/public/resources/js/configeditor.js

@@ -909,10 +909,6 @@ PWM_CFGEDIT.initSettingDisplay = function(setting, options) {
             EmailTableHandler.init(settingKey);
             break;
 
-        case 'EMAIL_ADDRESS':
-            StringValueHandler.init(settingKey);
-            break;
-
         case 'ACTION':
             ActionHandler.init(settingKey);
             break;