auto_wizard.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. # set Settings
  45. if auto_wizard_hash['Settings']
  46. auto_wizard_hash['Settings'].each { |setting_data|
  47. Setting.set( setting_data['name'], setting_data['value'] )
  48. }
  49. end
  50. # create Organizations
  51. if auto_wizard_hash['Organizations']
  52. auto_wizard_hash['Organizations'].each { |organization_data|
  53. Organization.create_or_update(organization_data.symbolize_keys)
  54. }
  55. end
  56. # create Users
  57. if auto_wizard_hash['Users']
  58. roles = Role.where( name: %w(Agent Admin) )
  59. groups = Group.all
  60. auto_wizard_hash['Users'].each { |user_data|
  61. # lookup organization
  62. if user_data['organization'] && !user_data['organization'].empty?
  63. organization = Organization.find_by(name: user_data['organization'])
  64. if organization
  65. user_data['organization_id'] = organization.id
  66. end
  67. end
  68. user_data.delete('organization')
  69. user_data_symbolized = user_data.symbolize_keys.merge(
  70. {
  71. active: true,
  72. roles: roles,
  73. groups: groups,
  74. }
  75. )
  76. created_user = User.create_or_update(user_data_symbolized)
  77. # use first created user as admin
  78. next if admin_user.id != 1
  79. admin_user = created_user
  80. UserInfo.current_user_id = admin_user.id
  81. # fetch org logo
  82. if admin_user.email
  83. Service::Image.organization_suggest(admin_user.email)
  84. end
  85. }
  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 {|map_name, model|
  95. next if !auto_wizard_hash[map_name]
  96. auto_wizard_hash[map_name].each {|data|
  97. if data['id'] || data['name']
  98. Kernel.const_get(model).create_or_update(data.symbolize_keys)
  99. else
  100. Kernel.const_get(model).create(data.symbolize_keys)
  101. end
  102. }
  103. }
  104. # remove auto wizard file
  105. FileUtils.rm auto_wizard_file_location
  106. admin_user
  107. end
  108. def self.file_location
  109. auto_wizard_file_name = 'auto_wizard.json'
  110. auto_wizard_file_location = "#{Rails.root}/#{auto_wizard_file_name}"
  111. auto_wizard_file_location
  112. end
  113. private_class_method :file_location
  114. end