getting_started_controller.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://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 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. # validate url
  90. messages = {}
  91. settings = {}
  92. if !Setting.get('system_online_service')
  93. if !params[:url] || params[:url] !~ %r{^(http|https)://.+?$}
  94. messages[:url] = 'A URL looks like http://zammad.example.com'
  95. end
  96. # split url in http_type and fqdn
  97. if params[:url]
  98. if params[:url] =~ %r{^(http|https)://(.+?)(:.+?|/.+?|)$}
  99. settings[:http_type] = $1
  100. settings[:fqdn] = $2
  101. else
  102. messages[:url] = 'A URL looks like http://zammad.example.com'
  103. end
  104. end
  105. end
  106. # validate organization
  107. if params[:organization].blank?
  108. messages[:organization] = 'Invalid!'
  109. else
  110. settings[:organization] = params[:organization]
  111. end
  112. # validate image
  113. if params[:logo] && params[:logo] =~ /^data:image/i
  114. file = StaticAssets.data_url_attributes(params[:logo])
  115. if !file[:content] || !file[:mime_type]
  116. messages[:logo] = 'Unable to process image upload.'
  117. end
  118. end
  119. # add locale_default
  120. if params[:locale_default].present?
  121. settings[:locale_default] = params[:locale_default]
  122. end
  123. # add timezone_default
  124. if params[:timezone_default].present?
  125. settings[:timezone_default] = params[:timezone_default]
  126. end
  127. if messages.present?
  128. render json: {
  129. result: 'invalid',
  130. messages: messages,
  131. }
  132. return
  133. end
  134. # save image
  135. if params[:logo] && params[:logo] =~ /^data:image/i
  136. # data:image/png;base64
  137. file = StaticAssets.data_url_attributes(params[:logo])
  138. # store image 1:1
  139. StaticAssets.store_raw(file[:content], file[:mime_type])
  140. end
  141. if params[:logo_resize] && params[:logo_resize] =~ /^data:image/i
  142. # data:image/png;base64
  143. file = StaticAssets.data_url_attributes(params[:logo_resize])
  144. # store image 1:1
  145. settings[:product_logo] = StaticAssets.store(file[:content], file[:mime_type])
  146. end
  147. # set changed settings
  148. settings.each do |key, value|
  149. Setting.set(key, value)
  150. end
  151. render json: {
  152. result: 'ok',
  153. settings: settings,
  154. }
  155. end
  156. private
  157. def auto_wizard_enabled_response
  158. return false if !AutoWizard.enabled?
  159. render json: {
  160. auto_wizard: true
  161. }
  162. true
  163. end
  164. def setup_done
  165. #return false
  166. count = User.all.count()
  167. done = true
  168. if count <= 2
  169. done = false
  170. end
  171. done
  172. end
  173. def setup_done_response
  174. return false if !setup_done
  175. groups = Group.where(active: true)
  176. addresses = EmailAddress.where(active: true)
  177. render json: {
  178. setup_done: true,
  179. import_mode: Setting.get('import_mode'),
  180. import_backend: Setting.get('import_backend'),
  181. system_online_service: Setting.get('system_online_service'),
  182. addresses: addresses,
  183. groups: groups,
  184. config: config_to_update,
  185. channel_driver: {
  186. email: EmailHelper.available_driver,
  187. },
  188. }
  189. true
  190. end
  191. def config_to_update
  192. {
  193. product_logo: Setting.get('product_logo')
  194. }
  195. end
  196. end