20240207144547_update_user_attributes.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class UpdateUserAttributes < ActiveRecord::Migration[7.0]
  3. def up
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. migrate_email_attribute
  7. migrate_role_ids_attribute
  8. migrate_group_ids_attribute
  9. end
  10. private
  11. def migrate_email_attribute
  12. email_attribute = user_attribute('email')
  13. email_attribute.screens[:signup] ||= {}
  14. email_attribute.screens[:signup]['-all-'] ||= {}
  15. email_attribute.screens[:signup]['-all-']['null'] = false
  16. email_attribute.screens[:invite_agent] ||= {}
  17. email_attribute.screens[:invite_agent]['-all-'] ||= {}
  18. email_attribute.screens[:invite_agent]['-all-']['null'] = false
  19. email_attribute.screens[:invite_customer] ||= {}
  20. email_attribute.screens[:invite_customer]['-all-'] ||= {}
  21. email_attribute.screens[:invite_customer]['-all-']['null'] = false
  22. email_attribute.save!(validate: false)
  23. end
  24. def migrate_role_ids_attribute
  25. role_ids_attribute = user_attribute('role_ids')
  26. role_ids_attribute.data_option[:relation] = 'Role'
  27. role_ids_attribute.save!(validate: false)
  28. end
  29. def migrate_group_ids_attribute
  30. group_ids_attribute = user_attribute('group_ids')
  31. group_ids_attribute.screens[:invite_agent] ||= {}
  32. group_ids_attribute.screens[:invite_agent]['-all-'] ||= {}
  33. group_ids_attribute.screens[:invite_agent]['-all-']['null'] = true
  34. group_ids_attribute.save!(validate: false)
  35. end
  36. def user_attribute(name)
  37. ObjectManager::Attribute.for_object('User').find_by(name: name)
  38. end
  39. end