auto_wizard.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Copyright (C) 2012-2024 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 "The required file #{auto_wizard_file_location} was not found." 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. raise AutoWizardDisabledError if !enabled?
  36. ttl = 60.minutes.to_i * 60.seconds.to_i * 1000
  37. Service::ExecuteLockedBlock.new('Zammad::System::Setup', ttl).execute { run }
  38. end
  39. def self.run
  40. auto_wizard_file_location = file_location
  41. auto_wizard_hash = data
  42. admin_user = User.find(1)
  43. UserInfo.current_user_id = admin_user.id
  44. # set default calendar
  45. if auto_wizard_hash['CalendarSetup'] && auto_wizard_hash['CalendarSetup']['Ip']
  46. Calendar.init_setup(auto_wizard_hash['CalendarSetup']['Ip'])
  47. end
  48. # load text modules
  49. if auto_wizard_hash['TextModuleLocale'] && auto_wizard_hash['TextModuleLocale']['Locale']
  50. begin
  51. TextModule.load(auto_wizard_hash['TextModuleLocale']['Locale'])
  52. rescue => e
  53. Rails.logger.error "Unable to load text modules #{auto_wizard_hash['TextModuleLocale']['Locale']}: #{e.message}"
  54. end
  55. end
  56. # set Settings
  57. auto_wizard_hash['Settings']&.each do |setting_data|
  58. Setting.set(setting_data['name'], setting_data['value'])
  59. end
  60. # create Permissions/Organization
  61. model_map = {
  62. 'Permissions' => 'Permission',
  63. 'Organizations' => 'Organization',
  64. }
  65. model_map.each do |map_name, model|
  66. next if !auto_wizard_hash[map_name]
  67. auto_wizard_hash[map_name].each do |data|
  68. data.symbolize_keys!
  69. model.constantize.create_or_update_with_ref(data)
  70. end
  71. end
  72. # create Users
  73. auto_wizard_hash['Users']&.each do |user_data|
  74. user_data.symbolize_keys!
  75. if admin_user.id == 1
  76. if !user_data[:roles] && !user_data[:role_ids]
  77. user_data[:roles] = Role.where(name: %w[Agent Admin])
  78. end
  79. if !user_data[:groups] && !user_data[:group_ids]
  80. user_data[:groups] = Group.all
  81. end
  82. end
  83. created_user = User.create_or_update_with_ref(user_data)
  84. # use first created user as admin
  85. next if admin_user.id != 1
  86. admin_user = created_user
  87. UserInfo.current_user_id = admin_user.id
  88. # fetch org logo
  89. if admin_user.email.present?
  90. Service::Image.organization_suggest(admin_user.email)
  91. end
  92. end
  93. # create EmailAddresses/Channels/Signatures
  94. model_map = {
  95. 'Channels' => 'Channel',
  96. 'EmailAddresses' => 'EmailAddress',
  97. 'Signatures' => 'Signature',
  98. 'Groups' => 'Group',
  99. }
  100. model_map.each do |map_name, model|
  101. next if !auto_wizard_hash[map_name]
  102. auto_wizard_hash[map_name].each do |data|
  103. data.symbolize_keys!
  104. model.constantize.create_or_update_with_ref(data)
  105. end
  106. end
  107. # reset primary key sequences
  108. DbHelper.import_post
  109. # remove auto wizard file
  110. begin
  111. FileUtils.rm auto_wizard_file_location
  112. rescue
  113. # Tolerate deletion errors, e.g. on read-only file systems.
  114. end
  115. admin_user
  116. end
  117. def self.file_location
  118. Rails.root.join(ENV['AUTOWIZARD_RELATIVE_PATH'].presence || 'auto_wizard.json')
  119. end
  120. private_class_method :file_location
  121. class AutoWizardDisabledError < StandardError
  122. def initialize
  123. super(__('AutoWizard is disabled'))
  124. end
  125. end
  126. end