From 978a1539fa329de8f2a7d88140f248ea76ca2c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 10 Dec 2015 22:23:54 +0100 Subject: [PATCH] new pop3_polling_enabled setting validator to ensure credentials are working before enabling it --- config/locales/server.en.yml | 4 ++ config/site_settings.yml | 4 +- .../pop3_polling_enabled_setting_validator.rb | 44 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 lib/validators/pop3_polling_enabled_setting_validator.rb diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index c419a44fc..722726fef 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1236,6 +1236,10 @@ en: invalid_string_min: "Must be at least %{min} characters." invalid_string_max: "Must be no more than %{max} characters." invalid_reply_by_email_address: "Value must contain '%{reply_key}' and be different from the notification email." + pop3_polling_host_is_empty: "You must set a 'pop3 polling host' before enabling POP3 polling." + pop3_polling_username_is_empty: "You must set a 'pop3 polling username' before enabling POP3 polling." + pop3_polling_password_is_empty: "You must set a 'pop3 polling password' before enabling POP3 polling." + pop3_polling_authentication_failed: "POP3 authentication failed. Please verify your pop3 credentials." notification_types: group_mentioned: "%{group_name} was mentioned in %{link}" diff --git a/config/site_settings.yml b/config/site_settings.yml index 66272c245..495dbadbd 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -506,7 +506,9 @@ email: reply_by_email_address: default: '' validator: "ReplyByEmailAddressValidator" - pop3_polling_enabled: false + pop3_polling_enabled: + default: false + validator: "POP3PollingEnabledSettingValidator" pop3_polling_ssl: true pop3_polling_period_mins: 5 pop3_polling_host: '' diff --git a/lib/validators/pop3_polling_enabled_setting_validator.rb b/lib/validators/pop3_polling_enabled_setting_validator.rb new file mode 100644 index 000000000..adee3dfc5 --- /dev/null +++ b/lib/validators/pop3_polling_enabled_setting_validator.rb @@ -0,0 +1,44 @@ +require "net/pop" + +class POP3PollingEnabledSettingValidator + + def initialize(opts={}) + @opts = opts + end + + def valid_value?(val) + # only validate when enabling polling + return true if val == "f" + # ensure we can authenticate + SiteSetting.pop3_polling_host.present? && + SiteSetting.pop3_polling_username.present? && + SiteSetting.pop3_polling_password.present? && + authentication_works? + end + + def error_message + if SiteSetting.pop3_polling_host.blank? + I18n.t("site_settings.errors.pop3_polling_host_is_empty") + elsif SiteSetting.pop3_polling_username.blank? + I18n.t("site_settings.errors.pop3_polling_username_is_empty") + elsif SiteSetting.pop3_polling_password.blank? + I18n.t("site_settings.errors.pop3_polling_password_is_empty") + else + I18n.t("site_settings.errors.pop3_polling_authentication_failed") + end + end + + private + + def authentication_works? + @authentication_works ||= begin + pop3 = Net::POP3.new(SiteSetting.pop3_polling_host, SiteSetting.pop3_polling_port) + pop3.enable_ssl(OpenSSL::SSL::VERIFY_NONE) if SiteSetting.pop3_polling_ssl + pop3.auth_only(SiteSetting.pop3_polling_username, SiteSetting.pop3_polling_password) + rescue Net::POPAuthenticationError + false + else + true + end + end +end