getting_started_controller.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. if messages.present?
  125. render json: {
  126. result: 'invalid',
  127. messages: messages,
  128. }
  129. return
  130. end
  131. # save image
  132. if params[:logo] && params[:logo] =~ /^data:image/i
  133. # data:image/png;base64
  134. file = StaticAssets.data_url_attributes(params[:logo])
  135. # store image 1:1
  136. StaticAssets.store_raw(file[:content], file[:mime_type])
  137. end
  138. if params[:logo_resize] && params[:logo_resize] =~ /^data:image/i
  139. # data:image/png;base64
  140. file = StaticAssets.data_url_attributes(params[:logo_resize])
  141. # store image 1:1
  142. settings[:product_logo] = StaticAssets.store(file[:content], file[:mime_type])
  143. end
  144. # set changed settings
  145. settings.each do |key, value|
  146. Setting.set(key, value)
  147. end
  148. render json: {
  149. result: 'ok',
  150. settings: settings,
  151. }
  152. end
  153. private
  154. def auto_wizard_enabled_response
  155. return false if !AutoWizard.enabled?
  156. render json: {
  157. auto_wizard: true
  158. }
  159. true
  160. end
  161. def setup_done
  162. #return false
  163. count = User.all.count()
  164. done = true
  165. if count <= 2
  166. done = false
  167. end
  168. done
  169. end
  170. def setup_done_response
  171. return false if !setup_done
  172. groups = Group.where(active: true)
  173. addresses = EmailAddress.where(active: true)
  174. render json: {
  175. setup_done: true,
  176. import_mode: Setting.get('import_mode'),
  177. import_backend: Setting.get('import_backend'),
  178. system_online_service: Setting.get('system_online_service'),
  179. addresses: addresses,
  180. groups: groups,
  181. config: config_to_update,
  182. channel_driver: {
  183. email: EmailHelper.available_driver,
  184. },
  185. }
  186. true
  187. end
  188. def config_to_update
  189. {
  190. product_logo: Setting.get('product_logo')
  191. }
  192. end
  193. end