getting_started_controller.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class GettingStartedController < ApplicationController
  3. =begin
  4. Resource:
  5. GET /api/v1/getting_started.json
  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.json -v -u #{login}:#{password}
  22. =end
  23. def index
  24. # check if first user already exists
  25. return if setup_done_response
  26. # if master user already exists, we need to be authenticated
  27. if setup_done
  28. return if !authentication_check
  29. end
  30. # get all groups
  31. groups = Group.where( :active => true )
  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. :groups => groups,
  38. }
  39. end
  40. def base_url
  41. return if setup_done_response
  42. # validate
  43. if !params[:url] ||params[:url] !~ /^(http|https):\/\/.+?$/
  44. render :json => {
  45. :result => 'invalid',
  46. :message => 'Invalid!',
  47. }
  48. return
  49. end
  50. # split url in http_type and fqdn
  51. if params[:url] =~ /^(http|https):\/\/(.+?)$/
  52. Setting.set('http_type', $1)
  53. Setting.set('fqdn', $2)
  54. render :json => {
  55. :result => 'ok',
  56. }
  57. return
  58. end
  59. render :json => {
  60. :result => 'invalid',
  61. :message => 'Unable to parse url!',
  62. }
  63. end
  64. def base_outbound
  65. return if setup_done_response
  66. # validate params
  67. if !params[:adapter]
  68. render :json => {
  69. :result => 'invalid',
  70. :message => 'Invalid!',
  71. }
  72. return
  73. end
  74. # test connection
  75. translationMap = {
  76. 'authentication failed' => 'Authentication failed!',
  77. 'getaddrinfo: nodename nor servname provided, or not known' => 'Hostname not found!',
  78. 'No route to host' => 'No route to host!',
  79. 'Connection refused' => 'Connection refused!',
  80. }
  81. if params[:adapter] == 'smtp'
  82. begin
  83. Channel::SMTP.new.send(
  84. {
  85. :from => 'me@example.com',
  86. :to => 'emailtrytest@znuny.com',
  87. :subject => 'test',
  88. :body => 'test',
  89. },
  90. {
  91. :options => params[:options]
  92. }
  93. )
  94. rescue Exception => e
  95. # check if sending email was ok, but mailserver rejected
  96. whiteMap = {
  97. 'Recipient address rejected' => true,
  98. }
  99. whiteMap.each {|key, message|
  100. if e.message =~ /#{Regexp.escape(key)}/i
  101. render :json => {
  102. :result => 'ok',
  103. }
  104. return
  105. end
  106. }
  107. message_human = ''
  108. translationMap.each {|key, message|
  109. if e.message =~ /#{Regexp.escape(key)}/i
  110. message_human = message
  111. end
  112. }
  113. render :json => {
  114. :result => 'invalid',
  115. :message => e.message,
  116. :message_human => message_human,
  117. }
  118. return
  119. end
  120. else
  121. begin
  122. Channel::Sendmail.new.send(
  123. {
  124. :from => 'me@example.com',
  125. :to => 'emailtrytest@znuny.com',
  126. :subject => 'test',
  127. :body => 'test',
  128. },
  129. nil
  130. )
  131. rescue Exception => e
  132. message_human = ''
  133. translationMap.each {|key, message|
  134. if e.message =~ /#{Regexp.escape(key)}/i
  135. message_human = message
  136. end
  137. }
  138. render :json => {
  139. :result => 'invalid',
  140. :message => e.message,
  141. :message_human => message_human,
  142. }
  143. return
  144. end
  145. end
  146. # save settings
  147. if params[:adapter] == 'smtp'
  148. smtp = Channel.where( :adapter => 'SMTP', :area => 'Email::Outbound' ).first
  149. smtp.options = params[:options]
  150. smtp.active = true
  151. smtp.save!
  152. sendmail = Channel.where(:adapter => 'Sendmail').first
  153. sendmail.active = false
  154. sendmail.save!
  155. else
  156. sendmail = Channel.where( :adapter => 'Sendmail', :area => 'Email::Outbound' ).first
  157. sendmail.options = {}
  158. sendmail.active = true
  159. sendmail.save!
  160. smtp = Channel.where(:adapter => 'SMTP').first
  161. smtp.active = false
  162. smtp.save
  163. end
  164. # return result
  165. render :json => {
  166. :result => 'ok',
  167. }
  168. end
  169. def base_inbound
  170. return if setup_done_response
  171. # validate params
  172. if !params[:adapter]
  173. render :json => {
  174. :result => 'invalid',
  175. }
  176. return
  177. end
  178. # connection test
  179. translationMap = {
  180. 'authentication failed' => 'Authentication failed!',
  181. 'getaddrinfo: nodename nor servname provided, or not known' => 'Hostname not found!',
  182. 'No route to host' => 'No route to host!',
  183. 'Connection refused' => 'Connection refused!',
  184. }
  185. if params[:adapter] == 'IMAP'
  186. begin
  187. Channel::IMAP.new.fetch( { :options => params[:options] }, 'check' )
  188. rescue Exception => e
  189. message_human = ''
  190. translationMap.each {|key, message|
  191. if e.message =~ /#{Regexp.escape(key)}/i
  192. message_human = message
  193. end
  194. }
  195. render :json => {
  196. :result => 'invalid',
  197. :message => e.message,
  198. :message_human => message_human,
  199. }
  200. return
  201. end
  202. else
  203. begin
  204. Channel::POP3.new.fetch( { :options => params[:options] }, 'check' )
  205. rescue Exception => e
  206. message_human = ''
  207. translationMap.each {|key, message|
  208. if e.message =~ /#{Regexp.escape(key)}/i
  209. message_human = message
  210. end
  211. }
  212. render :json => {
  213. :result => 'invalid',
  214. :message => e.message,
  215. :message_human => message_human,
  216. }
  217. return
  218. end
  219. end
  220. # send verify email to inbox
  221. subject = '#' + rand(99999999999).to_s
  222. Channel::EmailSend.new.send(
  223. {
  224. :from => params[:email],
  225. :to => params[:email],
  226. :subject => "Zammad Getting started Test Email #{subject}",
  227. :body => '.',
  228. 'x-zammad-ignore' => 'true',
  229. }
  230. )
  231. (1..5).each {|loop|
  232. sleep 10
  233. # fetch mailbox
  234. found = nil
  235. if params[:adapter] == 'IMAP'
  236. found = Channel::IMAP.new.fetch( { :options => params[:options] }, 'verify', subject )
  237. else
  238. found = Channel::POP3.new.fetch( { :options => params[:options] }, 'verify', subject )
  239. end
  240. if found && found == 'verify ok'
  241. # remember address
  242. address = EmailAddress.all.first
  243. if address
  244. address.update_attributes(
  245. :realname => 'Zammad',
  246. :email => params[:email],
  247. :active => 1,
  248. :updated_by_id => 1,
  249. :created_by_id => 1,
  250. )
  251. else
  252. EmailAddress.create(
  253. :realname => 'Zammad',
  254. :email => params[:email],
  255. :active => 1,
  256. :updated_by_id => 1,
  257. :created_by_id => 1,
  258. )
  259. end
  260. # store mailbox
  261. Channel.create(
  262. :area => 'Email::Inbound',
  263. :adapter => params[:adapter],
  264. :options => params[:options],
  265. :group_id => 1,
  266. :active => 1,
  267. :updated_by_id => 1,
  268. :created_by_id => 1,
  269. )
  270. render :json => {
  271. :result => 'ok',
  272. }
  273. return
  274. end
  275. }
  276. # check dilivery for 30 sek.
  277. render :json => {
  278. :result => 'invalid',
  279. :message => 'Verification Email not found in mailbox.',
  280. }
  281. return
  282. end
  283. private
  284. def setup_done
  285. #return false
  286. count = User.all.count()
  287. done = true
  288. if count <= 2
  289. done = false
  290. end
  291. done
  292. end
  293. def setup_done_response
  294. if !setup_done
  295. return false
  296. end
  297. render :json => {
  298. :setup_done => true,
  299. :import_mode => Setting.get('import_mode'),
  300. :import_backend => Setting.get('import_backend'),
  301. }
  302. true
  303. end
  304. end