getting_started_controller.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. require 'resolv'
  3. class GettingStartedController < ApplicationController
  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 master user already exists, we need to be authenticated
  30. if setup_done
  31. return if !authentication_check
  32. end
  33. # return result
  34. render json: {
  35. setup_done: setup_done,
  36. import_mode: Setting.get('import_mode'),
  37. import_backend: Setting.get('import_backend'),
  38. system_online_service: Setting.get('system_online_service'),
  39. }
  40. end
  41. def auto_wizard_admin
  42. # check if system setup is already done
  43. return if setup_done_response
  44. # check it auto wizard is enabled
  45. if !AutoWizard.enabled?
  46. render json: {
  47. auto_wizard: false,
  48. }
  49. return
  50. end
  51. # verify auto wizard file
  52. auto_wizard_data = AutoWizard.data
  53. if auto_wizard_data.blank?
  54. render json: {
  55. auto_wizard: true,
  56. auto_wizard_success: false,
  57. message: 'Invalid auto wizard file.',
  58. }
  59. return
  60. end
  61. # verify auto wizard token
  62. if auto_wizard_data['Token'] && auto_wizard_data['Token'] != params[:token]
  63. render json: {
  64. auto_wizard: true,
  65. auto_wizard_success: false,
  66. }
  67. return
  68. end
  69. # execute auto wizard
  70. auto_wizard_admin = AutoWizard.setup
  71. if !auto_wizard_admin
  72. render json: {
  73. auto_wizard: true,
  74. auto_wizard_success: false,
  75. message: 'Error during execution of auto wizard.',
  76. }
  77. return
  78. end
  79. # set current session user
  80. current_user_set(auto_wizard_admin)
  81. # set system init to done
  82. Setting.set('system_init_done', true)
  83. render json: {
  84. auto_wizard: true,
  85. auto_wizard_success: true,
  86. }
  87. end
  88. def base
  89. # check admin permissions
  90. permission_check('admin.wizard')
  91. # validate url
  92. messages = {}
  93. settings = {}
  94. if !Setting.get('system_online_service')
  95. if !params[:url] || params[:url] !~ %r{^(http|https)://.+?$}
  96. messages[:url] = 'A URL looks like http://zammad.example.com'
  97. end
  98. # split url in http_type and fqdn
  99. if params[:url]
  100. if params[:url] =~ %r{^(http|https)://(.+?)(:.+?|/.+?|)$}
  101. settings[:http_type] = $1
  102. settings[:fqdn] = $2
  103. else
  104. messages[:url] = 'A URL looks like http://zammad.example.com'
  105. end
  106. end
  107. end
  108. # validate organization
  109. if params[:organization].blank?
  110. messages[:organization] = 'Invalid!'
  111. else
  112. settings[:organization] = params[:organization]
  113. end
  114. # validate image
  115. if params[:logo] && params[:logo] =~ /^data:image/i
  116. file = StaticAssets.data_url_attributes(params[:logo])
  117. if !file[:content] || !file[:mime_type]
  118. messages[:logo] = 'Unable to process image upload.'
  119. end
  120. end
  121. # add locale_default
  122. if params[:locale_default].present?
  123. settings[:locale_default] = params[:locale_default]
  124. end
  125. if messages.present?
  126. render json: {
  127. result: 'invalid',
  128. messages: messages,
  129. }
  130. return
  131. end
  132. # save image
  133. if params[:logo] && params[:logo] =~ /^data:image/i
  134. # data:image/png;base64
  135. file = StaticAssets.data_url_attributes(params[:logo])
  136. # store image 1:1
  137. StaticAssets.store_raw(file[:content], file[:mime_type])
  138. end
  139. if params[:logo_resize] && params[:logo_resize] =~ /^data:image/i
  140. # data:image/png;base64
  141. file = StaticAssets.data_url_attributes(params[:logo_resize])
  142. # store image 1:1
  143. settings[:product_logo] = StaticAssets.store(file[:content], file[:mime_type])
  144. end
  145. # set changed settings
  146. settings.each do |key, value|
  147. Setting.set(key, value)
  148. end
  149. render json: {
  150. result: 'ok',
  151. settings: settings,
  152. }
  153. end
  154. private
  155. def auto_wizard_enabled_response
  156. return false if !AutoWizard.enabled?
  157. render json: {
  158. auto_wizard: true
  159. }
  160. true
  161. end
  162. def setup_done
  163. #return false
  164. count = User.all.count()
  165. done = true
  166. if count <= 2
  167. done = false
  168. end
  169. done
  170. end
  171. def setup_done_response
  172. return false if !setup_done
  173. groups = Group.where(active: true)
  174. addresses = EmailAddress.where(active: true)
  175. render json: {
  176. setup_done: true,
  177. import_mode: Setting.get('import_mode'),
  178. import_backend: Setting.get('import_backend'),
  179. system_online_service: Setting.get('system_online_service'),
  180. addresses: addresses,
  181. groups: groups,
  182. config: config_to_update,
  183. channel_driver: {
  184. email: EmailHelper.available_driver,
  185. },
  186. }
  187. true
  188. end
  189. def config_to_update
  190. {
  191. product_logo: Setting.get('product_logo')
  192. }
  193. end
  194. end