getting_started_controller.rb 5.4 KB

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