ldap_samaccountname_to_uid.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. require_dependency 'ldap'
  2. require_dependency 'ldap/user'
  3. module MigrationJob
  4. class LdapSamaccountnameToUid
  5. def perform
  6. Rails.logger.info 'Checking for active LDAP configuration...'
  7. if ldap_config.blank?
  8. Rails.logger.info 'Blank LDAP configuration. Exiting.'
  9. return
  10. end
  11. Rails.logger.info 'Checking for different LDAP uid attribute...'
  12. if uid_attribute_obsolete == uid_attribute_new
  13. Rails.logger.info 'Equal LDAP uid attributes. Exiting.'
  14. return
  15. end
  16. Rails.logger.info 'Starting to migrate LDAP config to new uid attribute...'
  17. migrate_ldap_config
  18. Rails.logger.info 'LDAP uid attribute migration completed.'
  19. end
  20. private
  21. def ldap
  22. @ldap ||= ::Ldap.new(ldap_config)
  23. end
  24. def ldap_config
  25. @ldap_config ||= Import::Ldap.config
  26. end
  27. def uid_attribute_new
  28. @uid_attribute_new ||= begin
  29. config = {
  30. filter: ldap_config['user_filter']
  31. }
  32. ::Ldap::User.new(config, ldap: ldap).uid_attribute
  33. end
  34. end
  35. def uid_attribute_obsolete
  36. @uid_attribute_obsolete ||= ldap_config['user_uid']
  37. end
  38. def migrate_ldap_config
  39. ldap_config_new = ldap_config.merge(
  40. 'user_uid' => uid_attribute_new
  41. )
  42. Setting.set('ldap_config', ldap_config_new)
  43. end
  44. end
  45. end