auto_wizard.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. data.symbolize_keys!
  67. model.constantize.create_or_update_with_ref(data)
  68. end
  69. end
  70. # create Users
  71. auto_wizard_hash['Users']&.each do |user_data|
  72. user_data.symbolize_keys!
  73. if admin_user.id == 1
  74. if !user_data[:roles] && !user_data[:role_ids]
  75. user_data[:roles] = Role.where(name: %w[Agent Admin])
  76. end
  77. if !user_data[:groups] && !user_data[:group_ids]
  78. user_data[:groups] = Group.all
  79. end
  80. end
  81. created_user = User.create_or_update_with_ref(user_data)
  82. # use first created user as admin
  83. next if admin_user.id != 1
  84. admin_user = created_user
  85. UserInfo.current_user_id = admin_user.id
  86. # fetch org logo
  87. if admin_user.email.present?
  88. Service::Image.organization_suggest(admin_user.email)
  89. end
  90. end
  91. # create EmailAddresses/Channels/Signatures
  92. model_map = {
  93. 'Channels' => 'Channel',
  94. 'EmailAddresses' => 'EmailAddress',
  95. 'Signatures' => 'Signature',
  96. 'Groups' => 'Group',
  97. }
  98. model_map.each do |map_name, model|
  99. next if !auto_wizard_hash[map_name]
  100. auto_wizard_hash[map_name].each do |data|
  101. data.symbolize_keys!
  102. model.constantize.create_or_update_with_ref(data)
  103. end
  104. end
  105. # reset primary key sequences
  106. DbHelper.import_post
  107. # remove auto wizard file
  108. FileUtils.rm auto_wizard_file_location
  109. admin_user
  110. end
  111. def self.file_location
  112. auto_wizard_file_name = 'auto_wizard.json'
  113. auto_wizard_file_location = Rails.root.join(auto_wizard_file_name)
  114. auto_wizard_file_location
  115. end
  116. private_class_method :file_location
  117. end