getting_started_controller.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Copyright (C) 2012-2023 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. # check it auto wizard is enabled
  43. if !AutoWizard.enabled?
  44. render json: {
  45. auto_wizard: false,
  46. }
  47. return
  48. end
  49. # verify auto wizard file
  50. auto_wizard_data = AutoWizard.data
  51. if auto_wizard_data.blank?
  52. render json: {
  53. auto_wizard: true,
  54. auto_wizard_success: false,
  55. message: __('Invalid auto wizard file.'),
  56. }
  57. return
  58. end
  59. # verify auto wizard token
  60. if auto_wizard_data['Token'] && auto_wizard_data['Token'] != params[:token]
  61. render json: {
  62. auto_wizard: true,
  63. auto_wizard_success: false,
  64. }
  65. return
  66. end
  67. # execute auto wizard
  68. auto_wizard_admin = AutoWizard.setup
  69. if !auto_wizard_admin
  70. render json: {
  71. auto_wizard: true,
  72. auto_wizard_success: false,
  73. message: __('Error during execution of auto wizard.'),
  74. }
  75. return
  76. end
  77. # set current session user
  78. current_user_set(auto_wizard_admin)
  79. # set system init to done
  80. Setting.set('system_init_done', true)
  81. render json: {
  82. auto_wizard: true,
  83. auto_wizard_success: true,
  84. }
  85. end
  86. def base
  87. # validate url
  88. messages = {}
  89. settings = {}
  90. if !Setting.get('system_online_service')
  91. if (result = self.class.validate_uri(params[:url]))
  92. settings[:http_type] = result[:scheme]
  93. settings[:fqdn] = result[:fqdn]
  94. else
  95. messages[:url] = __('A URL looks like this: https://zammad.example.com')
  96. end
  97. end
  98. # validate organization
  99. if params[:organization].blank?
  100. messages[:organization] = 'Invalid!'
  101. else
  102. settings[:organization] = params[:organization]
  103. end
  104. # validate image
  105. if params[:logo] && params[:logo] =~ %r{^data:image}i
  106. file = StaticAssets.data_url_attributes(params[:logo])
  107. if !file[:content] || !file[:mime_type]
  108. messages[:logo] = __('The uploaded image could not be processed.')
  109. end
  110. end
  111. # add locale_default
  112. if params[:locale_default].present?
  113. settings[:locale_default] = params[:locale_default]
  114. end
  115. # add timezone_default
  116. if params[:timezone_default].present?
  117. settings[:timezone_default] = params[:timezone_default]
  118. end
  119. if messages.present?
  120. render json: {
  121. result: 'invalid',
  122. messages: messages,
  123. }
  124. return
  125. end
  126. # save image
  127. if params[:logo] && params[:logo] =~ %r{^data:image}i
  128. # data:image/png;base64
  129. file = StaticAssets.data_url_attributes(params[:logo])
  130. # store image 1:1
  131. StaticAssets.store_raw(file[:content], file[:mime_type])
  132. end
  133. if params[:logo_resize] && params[:logo_resize] =~ %r{^data:image}i
  134. # data:image/png;base64
  135. file = StaticAssets.data_url_attributes(params[:logo_resize])
  136. # store image 1:1
  137. settings[:product_logo] = StaticAssets.store(file[:content], file[:mime_type])
  138. end
  139. # set changed settings
  140. settings.each do |key, value|
  141. Setting.set(key, value)
  142. end
  143. render json: {
  144. result: 'ok',
  145. settings: settings,
  146. }
  147. end
  148. def self.validate_uri(string)
  149. uri = URI(string)
  150. return false if %w[http https].exclude?(uri.scheme) || uri.host.blank?
  151. defaults = [['http', 80], ['https', 443]]
  152. actual = [uri.scheme, uri.port]
  153. fqdn = if defaults.include? actual
  154. uri.host
  155. else
  156. "#{uri.host}:#{uri.port}"
  157. end
  158. { scheme: uri.scheme, fqdn: fqdn }
  159. rescue
  160. false
  161. end
  162. private
  163. def auto_wizard_enabled_response
  164. return false if !AutoWizard.enabled?
  165. render json: {
  166. auto_wizard: true
  167. }
  168. true
  169. end
  170. def setup_done
  171. # return false
  172. count = User.all.count
  173. done = true
  174. if count <= 2
  175. done = false
  176. end
  177. done
  178. end
  179. def setup_done_response
  180. return false if !setup_done
  181. groups = Group.where(active: true)
  182. addresses = EmailAddress.where(active: true)
  183. render json: {
  184. setup_done: true,
  185. import_mode: Setting.get('import_mode'),
  186. import_backend: Setting.get('import_backend'),
  187. system_online_service: Setting.get('system_online_service'),
  188. addresses: addresses,
  189. groups: groups,
  190. config: config_to_update,
  191. channel_driver: {
  192. email: EmailHelper.available_driver,
  193. },
  194. }
  195. true
  196. end
  197. def config_to_update
  198. {
  199. product_logo: Setting.get('product_logo')
  200. }
  201. end
  202. end