diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 8817422fa24..a44704fd897 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1615,6 +1615,7 @@ en: enable_sso_disabled: "You must first enable 'enable sso' before enabling this setting." staged_users_disabled: "You must first enable 'staged users' before enabling this setting." reply_by_email_disabled: "You must first enable 'reply by email' before enabling this setting." + sso_url_is_empty: "You must set a 'sso url' before enabling this setting." search: within_post: "#%{post_number} by %{username}" diff --git a/config/site_settings.yml b/config/site_settings.yml index 44ad6c6014f..e03b424ae7d 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -291,6 +291,7 @@ login: enable_sso: client: true default: false + validator: "EnableSsoValidator" sso_allows_all_return_paths: false enable_sso_provider: false verbose_sso_logging: false diff --git a/lib/validators/enable_sso_validator.rb b/lib/validators/enable_sso_validator.rb new file mode 100644 index 00000000000..075798654f7 --- /dev/null +++ b/lib/validators/enable_sso_validator.rb @@ -0,0 +1,14 @@ +class EnableSsoValidator + def initialize(opts = {}) + @opts = opts + end + + def valid_value?(val) + return true if val == 'f' + SiteSetting.sso_url.present? + end + + def error_message + I18n.t('site_settings.errors.sso_url_is_empty') + end +end diff --git a/spec/components/validators/enable_sso_validator_spec.rb b/spec/components/validators/enable_sso_validator_spec.rb new file mode 100644 index 00000000000..d60c7b1b756 --- /dev/null +++ b/spec/components/validators/enable_sso_validator_spec.rb @@ -0,0 +1,48 @@ +require 'rails_helper' + +RSpec.describe EnableSsoValidator do + subject { described_class.new } + + describe '#valid_value?' do + describe "when 'sso url' is empty" do + before do + SiteSetting.sso_url = "" + end + + describe 'when val is false' do + it 'should be valid' do + expect(subject.valid_value?('f')).to eq(true) + end + end + + describe 'when value is true' do + it 'should not be valid' do + expect(subject.valid_value?('t')).to eq(false) + + expect(subject.error_message).to eq(I18n.t( + 'site_settings.errors.sso_url_is_empty' + )) + end + end + end + + describe "when 'sso url' is present" do + before do + SiteSetting.sso_url = "https://www.example.com/sso" + end + + describe 'when value is false' do + it 'should be valid' do + expect(subject.valid_value?('f')).to eq(true) + end + end + + describe 'when value is true' do + it 'should be valid' do + expect(subject.valid_value?('t')).to eq(true) + end + end + end + + end +end