mirror of
https://github.com/discourse/discourse.git
synced 2025-09-07 12:02:53 +08:00
new pop3_polling_enabled setting validator to ensure credentials are working before enabling it
This commit is contained in:
parent
fd5f984401
commit
978a1539fa
3 changed files with 51 additions and 1 deletions
44
lib/validators/pop3_polling_enabled_setting_validator.rb
Normal file
44
lib/validators/pop3_polling_enabled_setting_validator.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue