Automate clearing of broken LDAP configurations (fixes #2140)

This commit is contained in:
Ryan Lue 2018-09-11 17:21:09 +08:00 committed by Ryan Lue
parent 4f6503593e
commit e07f41ed16
3 changed files with 51 additions and 1 deletions

View file

@ -51,7 +51,7 @@ get config setting
def self.get(name) def self.get(name)
load load
@@current[name] @@current[name].deep_dup # prevents accidental modification of settings in console
end end
=begin =begin

View file

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

View file

@ -0,0 +1,35 @@
require 'rails_helper'
RSpec.describe Issue2140ResetLdapConfig, type: :db_migration do
before { Setting.set('ldap_config', config) }
context 'when LDAP config isnt 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