auto_wizard.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module AutoWizard
  3. =begin
  4. check if auto wizard is enabled
  5. AutoWizard.enabled?
  6. returns
  7. true | false
  8. =end
  9. def self.enabled?
  10. auto_wizard_file_location = file_location
  11. return false if !File.file?(auto_wizard_file_location)
  12. true
  13. end
  14. =begin
  15. get auto wizard data
  16. AutoWizard.data
  17. returns
  18. content of auto wizard file as object
  19. =end
  20. def self.data
  21. auto_wizard_file_location = file_location
  22. raise "So such file #{auto_wizard_file_location}" if !File.file?(auto_wizard_file_location)
  23. JSON.parse(File.read(auto_wizard_file_location))
  24. end
  25. =begin
  26. creates or updates Users, EmailAddresses and sets Settings based on the 'auto_wizard.json' file placed in the root directory.
  27. there is an example file 'contrib/auto_wizard_example.json'
  28. AutoWizard.setup
  29. returns
  30. the first created User if a 'auto_wizard.json' file was found and processed, containing at least one entry in Users
  31. the User with id 1 (NULL) if a 'auto_wizard.json' file was found and processed, containing no Users
  32. nil if no 'auto_wizard.json' file was found
  33. =end
  34. def self.setup
  35. auto_wizard_file_location = file_location
  36. auto_wizard_hash = data
  37. admin_user = User.find(1)
  38. UserInfo.current_user_id = admin_user.id
  39. # set default calendar
  40. if auto_wizard_hash['CalendarSetup'] && auto_wizard_hash['CalendarSetup']['Ip']
  41. Calendar.init_setup(auto_wizard_hash['CalendarSetup']['Ip'])
  42. end
  43. # load text modules
  44. if auto_wizard_hash['TextModuleLocale'] && auto_wizard_hash['TextModuleLocale']['Locale']
  45. begin
  46. TextModule.load(auto_wizard_hash['TextModuleLocale']['Locale'])
  47. rescue => e
  48. Rails.logger.error "Unable to load text modules #{auto_wizard_hash['TextModuleLocale']['Locale']}: #{e.message}"
  49. end
  50. end
  51. # set Settings
  52. auto_wizard_hash['Settings']&.each do |setting_data|
  53. Setting.set(setting_data['name'], setting_data['value'])
  54. end
  55. # create Permissions/Organization
  56. model_map = {
  57. 'Permissions' => 'Permission',
  58. 'Organizations' => 'Organization',
  59. }
  60. model_map.each do |map_name, model|
  61. next if !auto_wizard_hash[map_name]
  62. auto_wizard_hash[map_name].each do |data|
  63. data.symbolize_keys!
  64. model.constantize.create_or_update_with_ref(data)
  65. end
  66. end
  67. # create Users
  68. auto_wizard_hash['Users']&.each do |user_data|
  69. user_data.symbolize_keys!
  70. if admin_user.id == 1
  71. if !user_data[:roles] && !user_data[:role_ids]
  72. user_data[:roles] = Role.where(name: %w[Agent Admin])
  73. end
  74. if !user_data[:groups] && !user_data[:group_ids]
  75. user_data[:groups] = Group.all
  76. end
  77. end
  78. created_user = User.create_or_update_with_ref(user_data)
  79. # use first created user as admin
  80. next if admin_user.id != 1
  81. admin_user = created_user
  82. UserInfo.current_user_id = admin_user.id
  83. # fetch org logo
  84. if admin_user.email.present?
  85. Service::Image.organization_suggest(admin_user.email)
  86. end
  87. end
  88. # create EmailAddresses/Channels/Signatures
  89. model_map = {
  90. 'Channels' => 'Channel',
  91. 'EmailAddresses' => 'EmailAddress',
  92. 'Signatures' => 'Signature',
  93. 'Groups' => 'Group',
  94. }
  95. model_map.each do |map_name, model|
  96. next if !auto_wizard_hash[map_name]
  97. auto_wizard_hash[map_name].each do |data|
  98. data.symbolize_keys!
  99. model.constantize.create_or_update_with_ref(data)
  100. end
  101. end
  102. # reset primary key sequences
  103. DbHelper.import_post
  104. # remove auto wizard file
  105. FileUtils.rm auto_wizard_file_location
  106. admin_user
  107. end
  108. def self.file_location
  109. Rails.root.join(ENV['AUTOWIZARD_RELATIVE_PATH'].presence || 'auto_wizard.json')
  110. end
  111. private_class_method :file_location
  112. end