auto_wizard.rb 4.1 KB

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