From f40f602a4f26e528585831fccb9875dcdd8bec2e Mon Sep 17 00:00:00 2001 From: David Taylor Date: Mon, 3 Jul 2017 16:28:26 +0100 Subject: [PATCH] Add validation to slack settings (adapted from old slack plugin) --- config/locales/server.en.yml | 3 +++ config/settings.yml | 2 ++ ...ntegration_slack_enabled_setting_validator.rb | 16 ++++++++++++++++ plugin.rb | 2 ++ 4 files changed, 23 insertions(+) create mode 100644 lib/validators/chat_integration_slack_enabled_setting_validator.rb diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 8d6a2a1..082c7e2 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -14,6 +14,9 @@ en: chat_integration_slack_icon_url: 'Icon to post to slack with (defaults to forum logo)' chat_integration_slack_access_token: 'Token if you are using the Web API instead of webhooks' + errors: + chat_integration_slack_api_configs_are_empty: "You must enter either an outbound webhook URL, or an access token" + ####################################### ######### TELEGRAM SETTINGS ########### ####################################### diff --git a/config/settings.yml b/config/settings.yml index 25216f8..ee3d622 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -12,8 +12,10 @@ plugins: ####################################### chat_integration_slack_enabled: default: false + validator: "ChatIntegrationSlackEnabledSettingValidator" chat_integration_slack_outbound_webhook_url: default: '' + regex: '^https:\/\/hooks\.slack\.com\/services\/.+$' chat_integration_slack_excerpt_length: default: 400 chat_integration_slack_icon_url: diff --git a/lib/validators/chat_integration_slack_enabled_setting_validator.rb b/lib/validators/chat_integration_slack_enabled_setting_validator.rb new file mode 100644 index 0000000..c18c4ec --- /dev/null +++ b/lib/validators/chat_integration_slack_enabled_setting_validator.rb @@ -0,0 +1,16 @@ +class ChatIntegrationSlackEnabledSettingValidator + def initialize(opts={}) + @opts = opts + end + + def valid_value?(val) + return true if val == 'f' + return false if SiteSetting.chat_integration_slack_outbound_webhook_url.blank? && SiteSetting.chat_integration_slack_access_token.blank? + true + end + + def error_message + I18n.t('site_settings.errors.chat_integration_slack_api_configs_are_empty') + end + +end diff --git a/plugin.rb b/plugin.rb index 1c713df..6275b38 100644 --- a/plugin.rb +++ b/plugin.rb @@ -5,6 +5,8 @@ enabled_site_setting :chat_integration_enabled +# Site setting validators must be loaded before initialize +require_relative "lib/validators/chat_integration_slack_enabled_setting_validator" after_initialize do