ldap.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class MigrateLdapSamaccountnameToUidJob::Ldap
  3. attr_accessor :ldap, :ldap_config
  4. def initialize(ldap_config)
  5. @ldap_config = ldap_config
  6. @ldap = ::Ldap.new(ldap_config)
  7. end
  8. def perform
  9. log('Checking for active LDAP configuration...')
  10. if ldap_config.preferences.blank?
  11. log('Blank LDAP configuration. Exiting.')
  12. return
  13. end
  14. log('Checking for different LDAP uid attribute...')
  15. if uid_attribute_obsolete == uid_attribute_new
  16. log('Equal LDAP uid attributes. Exiting.')
  17. return
  18. end
  19. log('Starting to migrate LDAP config to new uid attribute...')
  20. migrate_ldap_config
  21. log('LDAP uid attribute migration completed.')
  22. end
  23. def uid_attribute_new
  24. @uid_attribute_new ||= begin
  25. config = {
  26. filter: ldap_config.preferences['user_filter']
  27. }
  28. ::Ldap::User.new(config, ldap: ldap).uid_attribute
  29. end
  30. end
  31. def uid_attribute_obsolete
  32. @uid_attribute_obsolete ||= ldap_config.preferences['user_uid']
  33. end
  34. def migrate_ldap_config
  35. ldap_config_new = ldap_config.preferences.merge(
  36. 'user_uid' => uid_attribute_new
  37. )
  38. ldap_config.update(preferences: ldap_config_new)
  39. end
  40. def log(message)
  41. Rails.logger.info "LDAP '#{ldap_config.name}' - #{message}"
  42. end
  43. end