new pop3_polling_enabled setting validator to ensure credentials are working before enabling it

This commit is contained in:
Régis Hanol 2015-12-10 22:23:54 +01:00
parent fd5f984401
commit 978a1539fa
3 changed files with 51 additions and 1 deletions

View File

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

View File

@ -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: ''

View File

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