auto_wizard.rb 3.9 KB

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