auto_wizard.rb 4.0 KB

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