getting_started_controller.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class GettingStartedController < ApplicationController
  3. prepend_before_action -> { authorize! }, only: [:base]
  4. =begin
  5. Resource:
  6. GET /api/v1/getting_started
  7. Response:
  8. {
  9. "master_user": 1,
  10. "groups": [
  11. {
  12. "name": "group1",
  13. "active":true
  14. },
  15. {
  16. "name": "group2",
  17. "active":true
  18. }
  19. ]
  20. }
  21. Test:
  22. curl http://localhost/api/v1/getting_started -v -u #{login}:#{password}
  23. =end
  24. def index
  25. # check if first user already exists
  26. return if setup_done_response
  27. # check it auto wizard is already done
  28. return if auto_wizard_enabled_response
  29. # if admin user already exists, we need to be authenticated
  30. return if setup_done && !authentication_check
  31. # return result
  32. render json: {
  33. setup_done: setup_done,
  34. import_mode: Setting.get('import_mode'),
  35. import_backend: Setting.get('import_backend'),
  36. system_online_service: Setting.get('system_online_service'),
  37. }
  38. end
  39. def auto_wizard_admin
  40. # check if system setup is already done
  41. return if setup_done_response
  42. begin
  43. auto_wizard_admin = Service::System::RunAutoWizard.new.execute(token: params[:token])
  44. rescue Service::System::RunAutoWizard::AutoWizardNotEnabledError
  45. return render json: {
  46. auto_wizard: false,
  47. }
  48. rescue Service::System::RunAutoWizard::AutoWizardExecutionError => e
  49. return render json: {
  50. auto_wizard: true,
  51. auto_wizard_success: false,
  52. message: e.message,
  53. }
  54. end
  55. # set current session user
  56. current_user_set(auto_wizard_admin)
  57. # set system init to done
  58. Setting.set('system_init_done', true)
  59. render json: {
  60. auto_wizard: true,
  61. auto_wizard_success: true,
  62. }
  63. end
  64. def base
  65. args = params.slice(:url, :locale_default, :timezone_default, :organization)
  66. %i[logo logo_resize].each do |key|
  67. data = params[key]
  68. next if !data&.match? %r{^data:image}i
  69. file = ImageHelper.data_url_attributes(data)
  70. args[key] = file[:content] if file
  71. end
  72. begin
  73. set_system_information_service = Service::System::SetSystemInformation.new(data: args)
  74. result = set_system_information_service.execute
  75. render json: {
  76. result: 'ok',
  77. settings: result,
  78. }
  79. rescue Exceptions::MissingAttribute, Exceptions::InvalidAttribute => e
  80. render json: {
  81. result: 'invalid',
  82. messages: { e.attribute => e.message }
  83. }
  84. end
  85. end
  86. private
  87. def auto_wizard_enabled_response
  88. return false if !AutoWizard.enabled?
  89. render json: {
  90. auto_wizard: true
  91. }
  92. true
  93. end
  94. def setup_done
  95. # return false
  96. count = User.count
  97. done = true
  98. if count <= 2
  99. done = false
  100. end
  101. done
  102. end
  103. def setup_done_response
  104. return false if !setup_done
  105. groups = Group.where(active: true)
  106. addresses = EmailAddress.where(active: true)
  107. render json: {
  108. setup_done: true,
  109. import_mode: Setting.get('import_mode'),
  110. import_backend: Setting.get('import_backend'),
  111. system_online_service: Setting.get('system_online_service'),
  112. addresses: addresses,
  113. groups: groups,
  114. config: config_to_update,
  115. channel_driver: {
  116. email: EmailHelper.available_driver,
  117. },
  118. }
  119. true
  120. end
  121. def config_to_update
  122. {
  123. product_logo: Setting.get('product_logo')
  124. }
  125. end
  126. end