auto_wizard.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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'] && auto_wizard_hash['CalendarSetup']['Ip']
  40. Calendar.init_setup(auto_wizard_hash['CalendarSetup']['Ip'])
  41. end
  42. # load text modules
  43. if auto_wizard_hash['TextModuleLocale'] && auto_wizard_hash['TextModuleLocale']['Locale']
  44. begin
  45. TextModule.load(auto_wizard_hash['TextModuleLocale']['Locale'])
  46. rescue => e
  47. Rails.logger.error "Unable to load text modules #{auto_wizard_hash['TextModuleLocale']['Locale']}: #{e.message}"
  48. end
  49. end
  50. # set Settings
  51. auto_wizard_hash['Settings']&.each do |setting_data|
  52. Setting.set(setting_data['name'], setting_data['value'])
  53. end
  54. # create Permissions/Organization
  55. model_map = {
  56. 'Permissions' => 'Permission',
  57. 'Organizations' => 'Organization',
  58. }
  59. model_map.each do |map_name, model|
  60. next if !auto_wizard_hash[map_name]
  61. auto_wizard_hash[map_name].each do |data|
  62. data.symbolize_keys!
  63. model.constantize.create_or_update_with_ref(data)
  64. end
  65. end
  66. # create Users
  67. auto_wizard_hash['Users']&.each do |user_data|
  68. user_data.symbolize_keys!
  69. if admin_user.id == 1
  70. if !user_data[:roles] && !user_data[:role_ids]
  71. user_data[:roles] = Role.where(name: %w[Agent Admin])
  72. end
  73. if !user_data[:groups] && !user_data[:group_ids]
  74. user_data[:groups] = Group.all
  75. end
  76. end
  77. created_user = User.create_or_update_with_ref(user_data)
  78. # use first created user as admin
  79. next if admin_user.id != 1
  80. admin_user = created_user
  81. UserInfo.current_user_id = admin_user.id
  82. # fetch org logo
  83. if admin_user.email.present?
  84. Service::Image.organization_suggest(admin_user.email)
  85. end
  86. end
  87. # create EmailAddresses/Channels/Signatures
  88. model_map = {
  89. 'Channels' => 'Channel',
  90. 'EmailAddresses' => 'EmailAddress',
  91. 'Signatures' => 'Signature',
  92. 'Groups' => 'Group',
  93. }
  94. model_map.each do |map_name, model|
  95. next if !auto_wizard_hash[map_name]
  96. auto_wizard_hash[map_name].each do |data|
  97. data.symbolize_keys!
  98. model.constantize.create_or_update_with_ref(data)
  99. end
  100. end
  101. # reset primary key sequences
  102. DbHelper.import_post
  103. # remove auto wizard file
  104. FileUtils.rm auto_wizard_file_location
  105. admin_user
  106. end
  107. def self.file_location
  108. auto_wizard_file_name = 'auto_wizard.json'
  109. Rails.root.join(auto_wizard_file_name)
  110. end
  111. private_class_method :file_location
  112. end