瀏覽代碼

Added validation for email address in Email Settings

James Albright 8 年之前
父節點
當前提交
b329bec559

+ 5 - 0
pom.xml

@@ -438,6 +438,11 @@
             <artifactId>commons-lang3</artifactId>
             <version>3.4</version>
         </dependency>
+        <dependency>
+            <groupId>commons-validator</groupId>
+            <artifactId>commons-validator</artifactId>
+            <version>1.5.1</version>
+        </dependency>
         <dependency>
             <groupId>com.sun.mail</groupId>
             <artifactId>javax.mail</artifactId>

+ 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.STRING, PwmSettingCategory.EMAIL_SETTINGS),
+            "email.default.fromAddress", PwmSettingSyntax.EMAIL_ADDRESS, PwmSettingCategory.EMAIL_SETTINGS),
     EMAIL_USERNAME(
             "email.smtp.username", PwmSettingSyntax.STRING, PwmSettingCategory.EMAIL_SETTINGS),
     EMAIL_PASSWORD(

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

@@ -40,6 +40,7 @@ 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()),

+ 2 - 2
src/main/java/password/pwm/config/stored/StoredConfigurationImpl.java

@@ -455,7 +455,7 @@ public class StoredConfigurationImpl implements Serializable, StoredConfiguratio
                     try {
                         final List<String> errors = loopValue.validateValue(loopSetting);
                         for (final String loopError : errors) {
-                            errorStrings.add(loopSetting.toMenuLocationDebug(profile,PwmConstants.DEFAULT_LOCALE) + " " + loopError);
+                            errorStrings.add(loopSetting.toMenuLocationDebug(profile,PwmConstants.DEFAULT_LOCALE) + " - " + loopError);
                         }
                     } catch (Exception e) {
                         LOGGER.error("unexpected error during validate value for " + loopSetting.toMenuLocationDebug(profile,PwmConstants.DEFAULT_LOCALE) + ", error: " + e.getMessage(),e);
@@ -467,7 +467,7 @@ public class StoredConfigurationImpl implements Serializable, StoredConfiguratio
                 try {
                     final List<String> errors = loopValue.validateValue(loopSetting);
                     for (final String loopError : errors) {
-                        errorStrings.add(loopSetting.toMenuLocationDebug(null,PwmConstants.DEFAULT_LOCALE) + loopError);
+                        errorStrings.add(loopSetting.toMenuLocationDebug(null,PwmConstants.DEFAULT_LOCALE) + " - " + loopError);
                     }
                 } catch (Exception e) {
                     LOGGER.error("unexpected error during validate value for " + loopSetting.toMenuLocationDebug(null,PwmConstants.DEFAULT_LOCALE) + ", error: " + e.getMessage(),e);

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

@@ -0,0 +1,95 @@
+/*
+ * 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;
+    }
+}

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

@@ -1467,6 +1467,7 @@ ActionHandler.drawRow = function(settingKey, iteration, value) {
         var selected = optionList[optionItem] == PWM_VAR['clientSettingCache'][settingKey][iteration]['type'];
         htmlRow += '<option value="' + optionList[optionItem] + '"' + (selected ? ' selected' : '') + '>' + optionList[optionItem] + '</option>';
     }
+    htmlRow += '</select>';
     htmlRow += '</td><td>';
     htmlRow += '<button id="button-' + inputID + '-options"><span class="btn-icon pwm-icon pwm-icon-sliders"/> Options</button>';
     htmlRow += '</td>';

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

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