auto_wizard.rb 3.5 KB

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