20220504105038_issue3141_multi_ldap.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Issue3141MultiLdap < 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. add_table
  7. migrate_config
  8. remove_config
  9. end
  10. def add_table
  11. create_table :ldap_sources do |t|
  12. t.string :name, limit: 100, null: false
  13. t.text :preferences, limit: 5.megabytes + 1, null: true
  14. t.boolean :active, null: false, default: true
  15. t.integer :prio, null: false
  16. t.integer :updated_by_id, null: false
  17. t.integer :created_by_id, null: false
  18. t.timestamps limit: 3, null: false
  19. end
  20. add_index :ldap_sources, [:name], unique: true
  21. add_foreign_key :ldap_sources, :users, column: :created_by_id
  22. add_foreign_key :ldap_sources, :users, column: :updated_by_id
  23. end
  24. def migrate_config
  25. config = Setting.get('ldap_config')
  26. return if config.blank?
  27. UserInfo.current_user_id = 1
  28. source = LdapSource.create!(
  29. name: 'LDAP #1',
  30. preferences: config
  31. )
  32. update_user_source(source)
  33. end
  34. def update_user_source(source)
  35. User.where(source: 'Ldap').update_all(source: "Ldap::#{source.id}") # rubocop:disable Rails/SkipsModelValidations
  36. Rails.cache.clear
  37. end
  38. def remove_config
  39. Setting.find_by(name: 'ldap_config').destroy
  40. end
  41. end