From e07f41ed1619c9309feaae4c4b5a59861a16bb43 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Tue, 11 Sep 2018 17:21:09 +0800 Subject: [PATCH] Automate clearing of broken LDAP configurations (fixes #2140) --- app/models/setting.rb | 2 +- ...0911064647_issue_2140_reset_ldap_config.rb | 15 ++++++++ .../issue_2140_reset_ldap_config_spec.rb | 35 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180911064647_issue_2140_reset_ldap_config.rb create mode 100644 spec/db/migrate/issue_2140_reset_ldap_config_spec.rb diff --git a/app/models/setting.rb b/app/models/setting.rb index 73058483c..d08c84a48 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -51,7 +51,7 @@ get config setting def self.get(name) load - @@current[name] + @@current[name].deep_dup # prevents accidental modification of settings in console end =begin diff --git a/db/migrate/20180911064647_issue_2140_reset_ldap_config.rb b/db/migrate/20180911064647_issue_2140_reset_ldap_config.rb new file mode 100644 index 000000000..4a2a94f20 --- /dev/null +++ b/db/migrate/20180911064647_issue_2140_reset_ldap_config.rb @@ -0,0 +1,15 @@ +class Issue2140ResetLdapConfig < ActiveRecord::Migration[5.1] + def up + # return if it's a new setup + return if !Setting.find_by(name: 'system_init_done') + + ldap_config = Setting.get('ldap_config') + + # finish if LDAP config isn't broken + ldap_config.to_json + rescue Encoding::UndefinedConversionError + ldap_config[:wizardData].delete(:backend_user_attributes) + + Setting.set('ldap_config', ldap_config) + end +end diff --git a/spec/db/migrate/issue_2140_reset_ldap_config_spec.rb b/spec/db/migrate/issue_2140_reset_ldap_config_spec.rb new file mode 100644 index 000000000..46c2b39a7 --- /dev/null +++ b/spec/db/migrate/issue_2140_reset_ldap_config_spec.rb @@ -0,0 +1,35 @@ +require 'rails_helper' + +RSpec.describe Issue2140ResetLdapConfig, type: :db_migration do + before { Setting.set('ldap_config', config) } + + context 'when LDAP config isn’t broken' do + let(:config) do + { 'wizardData' => + { 'backend_user_attributes' => + { 'foo' => 'bar' }, + 'user_attributes' => + { 'baz' => 'qux' } } }.with_indifferent_access + end + + it 'makes no changes' do + expect { migrate }.not_to change { Setting.get('ldap_config') } + end + end + + context 'when LDAP config is broken' do + let(:config) do + { 'wizardData' => + { 'backend_user_attributes' => + { 'foo' => "\u0001\u0001\u0004€" }, + 'user_attributes' => + { 'baz' => 'qux' } } }.with_indifferent_access + end + + it 'removes the offending backend_user_attributes sub-hash' do + expect { migrate } + .to change { Setting.get('ldap_config') } + .to(config.tap { |c| c[:wizardData].delete(:backend_user_attributes) }) + end + end +end