20230829085650_ldap_source_config.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class LdapSourceConfig < ActiveRecord::Migration[6.1]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. LdapSource.in_batches.each_record do |source|
  7. source.update!(preferences: adjust_config(source.preferences))
  8. end
  9. end
  10. private
  11. LDAP_SSL_MAPPING = {
  12. 'ldap' => 'off',
  13. 'ldaps' => 'ssl',
  14. }.freeze
  15. def adjust_config(preferences)
  16. ssl_legacy(preferences)
  17. change_host_and_ssl(preferences, 'ldap')
  18. change_host_and_ssl(preferences, 'ldaps')
  19. preferences.delete('host_url')
  20. preferences
  21. end
  22. def ssl_legacy(preferences)
  23. return preferences if preferences.key?('host_url')
  24. return preferences if !preferences.key?('ssl')
  25. preferences['ssl'] = 'ssl' if preferences['ssl']
  26. preferences['ssl'] = 'off' if !preferences['ssl']
  27. end
  28. def change_host_and_ssl(preferences, protocol)
  29. return preferences if !preferences.key?('host_url')
  30. return preferences if !preferences['host_url'].starts_with?("#{protocol}://")
  31. preferences['host'] = preferences['host_url'].sub("#{protocol}://", '')
  32. preferences['ssl'] = LDAP_SSL_MAPPING[protocol]
  33. end
  34. end