getting_started_controller.rb 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. # Copyright (C) 2012-2014 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 || auto_wizard_data.empty?
  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. return if deny_if_not_role(Z_ROLENAME_ADMIN)
  91. # validate url
  92. messages = {}
  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. end
  98. # validate organization
  99. if !params[:organization] || params[:organization].empty?
  100. messages[:organization] = 'Invalid!'
  101. end
  102. # validate image
  103. if params[:logo] && params[:logo] =~ /^data:image/i
  104. file = StaticAssets.data_url_attributes( params[:logo] )
  105. if !file[:content] || !file[:mime_type]
  106. messages[:logo] = 'Unable to process image upload.'
  107. end
  108. end
  109. if !messages.empty?
  110. render json: {
  111. result: 'invalid',
  112. messages: messages,
  113. }
  114. return
  115. end
  116. # split url in http_type and fqdn
  117. settings = {}
  118. if !Setting.get('system_online_service')
  119. if params[:url] =~ %r{/^(http|https)://(.+?)$}
  120. Setting.set('http_type', $1)
  121. settings[:http_type] = $1
  122. Setting.set('fqdn', $2)
  123. settings[:fqdn] = $2
  124. end
  125. end
  126. # save organization
  127. Setting.set('organization', params[:organization])
  128. settings[:organization] = params[:organization]
  129. # save image
  130. if params[:logo] && params[:logo] =~ /^data:image/i
  131. # data:image/png;base64
  132. file = StaticAssets.data_url_attributes( params[:logo] )
  133. # store image 1:1
  134. StaticAssets.store_raw( file[:content], file[:mime_type] )
  135. end
  136. if params[:logo_resize] && params[:logo_resize] =~ /^data:image/i
  137. # data:image/png;base64
  138. file = StaticAssets.data_url_attributes( params[:logo_resize] )
  139. # store image 1:1
  140. settings[:product_logo] = StaticAssets.store( file[:content], file[:mime_type] )
  141. end
  142. # set changed settings
  143. settings.each {|key, value|
  144. Setting.set(key, value)
  145. }
  146. render json: {
  147. result: 'ok',
  148. settings: settings,
  149. }
  150. end
  151. def email_probe
  152. # check admin permissions
  153. return if deny_if_not_role(Z_ROLENAME_ADMIN)
  154. # validation
  155. user = nil
  156. domain = nil
  157. if params[:email] =~ /^(.+?)@(.+?)$/
  158. user = $1
  159. domain = $2
  160. end
  161. if !user || !domain
  162. render json: {
  163. result: 'invalid',
  164. messages: {
  165. email: 'Invalid email.'
  166. },
  167. }
  168. return
  169. end
  170. # check domain based attributes
  171. provider_map = {
  172. google: {
  173. domain: 'gmail.com|googlemail.com|gmail.de',
  174. inbound: {
  175. adapter: 'imap',
  176. options: {
  177. host: 'imap.gmail.com',
  178. port: '993',
  179. ssl: true,
  180. user: params[:email],
  181. password: params[:password],
  182. },
  183. },
  184. outbound: {
  185. adapter: 'smtp',
  186. options: {
  187. host: 'smtp.gmail.com',
  188. port: '25',
  189. start_tls: true,
  190. user: params[:email],
  191. password: params[:password],
  192. }
  193. },
  194. },
  195. microsoft: {
  196. domain: 'outlook.com|hotmail.com',
  197. inbound: {
  198. adapter: 'imap',
  199. options: {
  200. host: 'imap-mail.outlook.com',
  201. port: '993',
  202. ssl: true,
  203. user: params[:email],
  204. password: params[:password],
  205. },
  206. },
  207. outbound: {
  208. adapter: 'smtp',
  209. options: {
  210. host: 'smtp-mail.outlook.com',
  211. port: 25,
  212. start_tls: true,
  213. user: params[:email],
  214. password: params[:password],
  215. }
  216. },
  217. },
  218. }
  219. # probe based on email domain and mx
  220. domains = [domain]
  221. mail_exchangers = mxers(domain)
  222. if mail_exchangers && mail_exchangers[0]
  223. logger.info "MX for #{domain}: #{mail_exchangers} - #{mail_exchangers[0][0]}"
  224. end
  225. if mail_exchangers && mail_exchangers[0] && mail_exchangers[0][0]
  226. domains.push mail_exchangers[0][0]
  227. end
  228. provider_map.each {|_provider, settings|
  229. domains.each {|domain_to_check|
  230. next if domain_to_check !~ /#{settings[:domain]}/i
  231. # probe inbound
  232. result = email_probe_inbound( settings[:inbound] )
  233. if result[:result] != 'ok'
  234. render json: result
  235. return # rubocop:disable Lint/NonLocalExitFromIterator
  236. end
  237. # probe outbound
  238. result = email_probe_outbound( settings[:outbound], params[:email] )
  239. if result[:result] != 'ok'
  240. render json: result
  241. return # rubocop:disable Lint/NonLocalExitFromIterator
  242. end
  243. render json: {
  244. result: 'ok',
  245. setting: settings,
  246. }
  247. return # rubocop:disable Lint/NonLocalExitFromIterator
  248. }
  249. }
  250. # probe inbound
  251. inbound_map = []
  252. if mail_exchangers && mail_exchangers[0] && mail_exchangers[0][0]
  253. inbound_mx = [
  254. {
  255. adapter: 'imap',
  256. options: {
  257. host: mail_exchangers[0][0],
  258. port: 993,
  259. ssl: true,
  260. user: user,
  261. password: params[:password],
  262. },
  263. },
  264. {
  265. adapter: 'imap',
  266. options: {
  267. host: mail_exchangers[0][0],
  268. port: 993,
  269. ssl: true,
  270. user: params[:email],
  271. password: params[:password],
  272. },
  273. },
  274. ]
  275. inbound_map = inbound_map + inbound_mx
  276. end
  277. inbound_auto = [
  278. {
  279. adapter: 'imap',
  280. options: {
  281. host: "mail.#{domain}",
  282. port: 993,
  283. ssl: true,
  284. user: user,
  285. password: params[:password],
  286. },
  287. },
  288. {
  289. adapter: 'imap',
  290. options: {
  291. host: "mail.#{domain}",
  292. port: 993,
  293. ssl: true,
  294. user: params[:email],
  295. password: params[:password],
  296. },
  297. },
  298. {
  299. adapter: 'imap',
  300. options: {
  301. host: "imap.#{domain}",
  302. port: 993,
  303. ssl: true,
  304. user: user,
  305. password: params[:password],
  306. },
  307. },
  308. {
  309. adapter: 'imap',
  310. options: {
  311. host: "imap.#{domain}",
  312. port: 993,
  313. ssl: true,
  314. user: params[:email],
  315. password: params[:password],
  316. },
  317. },
  318. {
  319. adapter: 'pop3',
  320. options: {
  321. host: "mail.#{domain}",
  322. port: 995,
  323. ssl: true,
  324. user: user,
  325. password: params[:password],
  326. },
  327. },
  328. {
  329. adapter: 'pop3',
  330. options: {
  331. host: "mail.#{domain}",
  332. port: 995,
  333. ssl: true,
  334. user: params[:email],
  335. password: params[:password],
  336. },
  337. },
  338. {
  339. adapter: 'pop3',
  340. options: {
  341. host: "pop.#{domain}",
  342. port: 995,
  343. ssl: true,
  344. user: user,
  345. password: params[:password],
  346. },
  347. },
  348. {
  349. adapter: 'pop3',
  350. options: {
  351. host: "pop.#{domain}",
  352. port: 995,
  353. ssl: true,
  354. user: params[:email],
  355. password: params[:password],
  356. },
  357. },
  358. {
  359. adapter: 'pop3',
  360. options: {
  361. host: "pop3.#{domain}",
  362. port: 995,
  363. ssl: true,
  364. user: user,
  365. password: params[:password],
  366. },
  367. },
  368. {
  369. adapter: 'pop3',
  370. options: {
  371. host: "pop3.#{domain}",
  372. port: 995,
  373. ssl: true,
  374. user: params[:email],
  375. password: params[:password],
  376. },
  377. },
  378. ]
  379. inbound_map = inbound_map + inbound_auto
  380. settings = {}
  381. success = false
  382. inbound_map.each {|config|
  383. logger.info "INBOUND PROBE: #{config.inspect}"
  384. result = email_probe_inbound( config )
  385. logger.info "INBOUND RESULT: #{result.inspect}"
  386. next if result[:result] != 'ok'
  387. success = true
  388. settings[:inbound] = config
  389. break
  390. }
  391. if !success
  392. render json: {
  393. result: 'failed',
  394. }
  395. return
  396. end
  397. # probe outbound
  398. outbound_map = []
  399. if mail_exchangers && mail_exchangers[0] && mail_exchangers[0][0]
  400. outbound_mx = [
  401. {
  402. adapter: 'smtp',
  403. options: {
  404. host: mail_exchangers[0][0],
  405. port: 25,
  406. start_tls: true,
  407. user: user,
  408. password: params[:password],
  409. },
  410. },
  411. {
  412. adapter: 'smtp',
  413. options: {
  414. host: mail_exchangers[0][0],
  415. port: 25,
  416. start_tls: true,
  417. user: params[:email],
  418. password: params[:password],
  419. },
  420. },
  421. {
  422. adapter: 'smtp',
  423. options: {
  424. host: mail_exchangers[0][0],
  425. port: 465,
  426. start_tls: true,
  427. user: user,
  428. password: params[:password],
  429. },
  430. },
  431. {
  432. adapter: 'smtp',
  433. options: {
  434. host: mail_exchangers[0][0],
  435. port: 465,
  436. start_tls: true,
  437. user: params[:email],
  438. password: params[:password],
  439. },
  440. },
  441. ]
  442. outbound_map = outbound_map + outbound_mx
  443. end
  444. outbound_auto = [
  445. {
  446. adapter: 'smtp',
  447. options: {
  448. host: "mail.#{domain}",
  449. port: 25,
  450. start_tls: true,
  451. user: user,
  452. password: params[:password],
  453. },
  454. },
  455. {
  456. adapter: 'smtp',
  457. options: {
  458. host: "mail.#{domain}",
  459. port: 25,
  460. start_tls: true,
  461. user: params[:email],
  462. password: params[:password],
  463. },
  464. },
  465. {
  466. adapter: 'smtp',
  467. options: {
  468. host: "mail.#{domain}",
  469. port: 465,
  470. start_tls: true,
  471. user: user,
  472. password: params[:password],
  473. },
  474. },
  475. {
  476. adapter: 'smtp',
  477. options: {
  478. host: "mail.#{domain}",
  479. port: 465,
  480. start_tls: true,
  481. user: params[:email],
  482. password: params[:password],
  483. },
  484. },
  485. {
  486. adapter: 'smtp',
  487. options: {
  488. host: "smtp.#{domain}",
  489. port: 25,
  490. start_tls: true,
  491. user: user,
  492. password: params[:password],
  493. },
  494. },
  495. {
  496. adapter: 'smtp',
  497. options: {
  498. host: "smtp.#{domain}",
  499. port: 25,
  500. start_tls: true,
  501. user: params[:email],
  502. password: params[:password],
  503. },
  504. },
  505. {
  506. adapter: 'smtp',
  507. options: {
  508. host: "smtp.#{domain}",
  509. port: 465,
  510. start_tls: true,
  511. user: user,
  512. password: params[:password],
  513. },
  514. },
  515. {
  516. adapter: 'smtp',
  517. options: {
  518. host: "smtp.#{domain}",
  519. port: 465,
  520. start_tls: true,
  521. user: params[:email],
  522. password: params[:password],
  523. },
  524. },
  525. ]
  526. success = false
  527. outbound_map.each {|config|
  528. logger.info "OUTBOUND PROBE: #{config.inspect}"
  529. result = email_probe_outbound( config, params[:email] )
  530. logger.info "OUTBOUND RESULT: #{result.inspect}"
  531. next if result[:result] != 'ok'
  532. success = true
  533. settings[:outbound] = config
  534. break
  535. }
  536. if !success
  537. render json: {
  538. result: 'failed',
  539. }
  540. return
  541. end
  542. render json: {
  543. result: 'ok',
  544. setting: settings,
  545. }
  546. end
  547. def email_outbound
  548. # check admin permissions
  549. return if deny_if_not_role(Z_ROLENAME_ADMIN)
  550. # validate params
  551. if !params[:adapter]
  552. render json: {
  553. result: 'invalid',
  554. }
  555. return
  556. end
  557. # connection test
  558. result = email_probe_outbound( params, params[:email] )
  559. render json: result
  560. end
  561. def email_inbound
  562. # check admin permissions
  563. return if deny_if_not_role(Z_ROLENAME_ADMIN)
  564. # validate params
  565. if !params[:adapter]
  566. render json: {
  567. result: 'invalid',
  568. }
  569. return
  570. end
  571. # connection test
  572. result = email_probe_inbound( params )
  573. render json: result
  574. end
  575. def email_verify
  576. # check admin permissions
  577. return if deny_if_not_role(Z_ROLENAME_ADMIN)
  578. # send verify email to inbox
  579. if !params[:subject]
  580. subject = '#' + rand(99_999_999_999).to_s
  581. else
  582. subject = params[:subject]
  583. end
  584. result = email_probe_outbound( params[:outbound], params[:meta][:email], subject )
  585. (1..5).each {
  586. sleep 10
  587. # fetch mailbox
  588. found = nil
  589. begin
  590. if params[:inbound][:adapter] =~ /^imap$/i
  591. found = Channel::IMAP.new.fetch( { options: params[:inbound][:options] }, 'verify', subject )
  592. else
  593. found = Channel::POP3.new.fetch( { options: params[:inbound][:options] }, 'verify', subject )
  594. end
  595. rescue => e
  596. render json: {
  597. result: 'invalid',
  598. message: e.to_s,
  599. subject: subject,
  600. }
  601. return
  602. end
  603. next if !found
  604. next if found != 'verify ok'
  605. # remember address
  606. address = EmailAddress.where( email: params[:meta][:email] ).first
  607. if !address
  608. address = EmailAddress.first
  609. end
  610. if address
  611. address.update_attributes(
  612. realname: params[:meta][:realname],
  613. email: params[:meta][:email],
  614. active: 1,
  615. updated_by_id: 1,
  616. created_by_id: 1,
  617. )
  618. else
  619. EmailAddress.create(
  620. realname: params[:meta][:realname],
  621. email: params[:meta][:email],
  622. active: 1,
  623. updated_by_id: 1,
  624. created_by_id: 1,
  625. )
  626. end
  627. # store mailbox
  628. Channel.create(
  629. area: 'Email::Inbound',
  630. adapter: params[:inbound][:adapter],
  631. options: params[:inbound][:options],
  632. group_id: 1,
  633. active: 1,
  634. updated_by_id: 1,
  635. created_by_id: 1,
  636. )
  637. # save settings
  638. if params[:outbound][:adapter] =~ /^smtp$/i
  639. smtp = Channel.where( adapter: 'SMTP', area: 'Email::Outbound' ).first
  640. smtp.options = params[:outbound][:options]
  641. smtp.active = true
  642. smtp.save!
  643. sendmail = Channel.where( adapter: 'Sendmail' ).first
  644. sendmail.active = false
  645. sendmail.save!
  646. else
  647. sendmail = Channel.where( adapter: 'Sendmail', area: 'Email::Outbound' ).first
  648. sendmail.options = {}
  649. sendmail.active = true
  650. sendmail.save!
  651. smtp = Channel.where( adapter: 'SMTP' ).first
  652. smtp.active = false
  653. smtp.save
  654. end
  655. render json: {
  656. result: 'ok',
  657. }
  658. return
  659. }
  660. # check delivery for 30 sek.
  661. render json: {
  662. result: 'invalid',
  663. message: 'Verification Email not found in mailbox.',
  664. subject: subject,
  665. }
  666. end
  667. private
  668. def email_probe_outbound(params, email, subject = nil)
  669. # validate params
  670. if !params[:adapter]
  671. result = {
  672. result: 'invalid',
  673. message: 'Invalid, need adapter!',
  674. }
  675. return result
  676. end
  677. if subject
  678. mail = {
  679. :from => email,
  680. :to => email,
  681. :subject => "Zammad Getting started Test Email #{subject}",
  682. :body => "This is a Test Email of Zammad to check if sending and receiving is working correctly.\n\nYou can ignore or delete this email.",
  683. 'x-zammad-ignore' => 'true',
  684. }
  685. else
  686. mail = {
  687. from: email,
  688. to: 'emailtrytest@znuny.com',
  689. subject: 'test',
  690. body: 'test',
  691. }
  692. end
  693. # test connection
  694. translation_map = {
  695. 'authentication failed' => 'Authentication failed!',
  696. 'Incorrect username' => 'Authentication failed!',
  697. 'getaddrinfo: nodename nor servname provided, or not known' => 'Hostname not found!',
  698. 'No route to host' => 'No route to host!',
  699. 'Connection refused' => 'Connection refused!',
  700. }
  701. if params[:adapter] =~ /^smtp$/i
  702. # in case, fill missing params
  703. if !params[:options].key?(:port)
  704. params[:options][:port] = 25
  705. end
  706. if !params[:options].key?(:ssl)
  707. params[:options][:ssl] = true
  708. end
  709. begin
  710. Channel::SMTP.new.send(
  711. mail,
  712. {
  713. options: params[:options]
  714. }
  715. )
  716. rescue => e
  717. # check if sending email was ok, but mailserver rejected
  718. if !subject
  719. white_map = {
  720. 'Recipient address rejected' => true,
  721. }
  722. white_map.each {|key, _message|
  723. next if e.message !~ /#{Regexp.escape(key)}/i
  724. result = {
  725. result: 'ok',
  726. settings: params,
  727. notice: e.message,
  728. }
  729. return result
  730. }
  731. end
  732. message_human = ''
  733. translation_map.each {|key, message|
  734. if e.message =~ /#{Regexp.escape(key)}/i
  735. message_human = message
  736. end
  737. }
  738. result = {
  739. result: 'invalid',
  740. settings: params,
  741. message: e.message,
  742. message_human: message_human,
  743. }
  744. return result
  745. end
  746. result = {
  747. result: 'ok',
  748. }
  749. return result
  750. end
  751. begin
  752. Channel::Sendmail.new.send(
  753. mail,
  754. nil
  755. )
  756. rescue => e
  757. message_human = ''
  758. translation_map.each {|key, message|
  759. if e.message =~ /#{Regexp.escape(key)}/i
  760. message_human = message
  761. end
  762. }
  763. result = {
  764. result: 'invalid',
  765. settings: params,
  766. message: e.message,
  767. message_human: message_human,
  768. }
  769. return result
  770. end
  771. result = {
  772. result: 'ok',
  773. }
  774. result
  775. end
  776. def email_probe_inbound(params)
  777. # validate params
  778. if !params[:adapter]
  779. fail 'need :adapter param'
  780. end
  781. # connection test
  782. translation_map = {
  783. 'authentication failed' => 'Authentication failed!',
  784. 'Incorrect username' => 'Authentication failed!',
  785. 'getaddrinfo: nodename nor servname provided, or not known' => 'Hostname not found!',
  786. 'No route to host' => 'No route to host!',
  787. 'Connection refused' => 'Connection refused!',
  788. }
  789. if params[:adapter] =~ /^imap$/i
  790. begin
  791. Channel::IMAP.new.fetch( { options: params[:options] }, 'check' )
  792. rescue => e
  793. message_human = ''
  794. translation_map.each {|key, message|
  795. if e.message =~ /#{Regexp.escape(key)}/i
  796. message_human = message
  797. end
  798. }
  799. result = {
  800. result: 'invalid',
  801. settings: params,
  802. message: e.message,
  803. message_human: message_human,
  804. }
  805. return result
  806. end
  807. result = {
  808. result: 'ok',
  809. }
  810. return result
  811. end
  812. begin
  813. Channel::POP3.new.fetch( { options: params[:options] }, 'check' )
  814. rescue => e
  815. message_human = ''
  816. translation_map.each {|key, message|
  817. if e.message =~ /#{Regexp.escape(key)}/i
  818. message_human = message
  819. end
  820. }
  821. result = {
  822. result: 'invalid',
  823. settings: params,
  824. message: e.message,
  825. message_human: message_human,
  826. }
  827. return result
  828. end
  829. result = {
  830. result: 'ok',
  831. }
  832. result
  833. end
  834. def mxers(domain)
  835. begin
  836. mxs = Resolv::DNS.open do |dns|
  837. ress = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
  838. ress.map { |r| [r.exchange.to_s, IPSocket.getaddress(r.exchange.to_s), r.preference] }
  839. end
  840. rescue => e
  841. logger.error e.message
  842. logger.error e.backtrace.inspect
  843. end
  844. mxs
  845. end
  846. def auto_wizard_enabled_response
  847. return false if !AutoWizard.enabled?
  848. render json: {
  849. auto_wizard: true
  850. }
  851. true
  852. end
  853. def setup_done
  854. #return false
  855. count = User.all.count()
  856. done = true
  857. if count <= 2
  858. done = false
  859. end
  860. done
  861. end
  862. def setup_done_response
  863. return false if !setup_done
  864. # get all groups
  865. groups = Group.where( active: true )
  866. # get email addresses
  867. addresses = EmailAddress.where( active: true )
  868. render json: {
  869. setup_done: true,
  870. import_mode: Setting.get('import_mode'),
  871. import_backend: Setting.get('import_backend'),
  872. system_online_service: Setting.get('system_online_service'),
  873. addresses: addresses,
  874. groups: groups,
  875. }
  876. true
  877. end
  878. end