channels_email_controller.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class ChannelsEmailController < ApplicationController
  3. prepend_before_action { authentication_check(permission: 'admin.channel_email') }
  4. def index
  5. system_online_service = Setting.get('system_online_service')
  6. account_channel_ids = []
  7. notification_channel_ids = []
  8. email_address_ids = []
  9. not_used_email_address_ids = []
  10. accounts_fixed = []
  11. assets = {}
  12. Channel.order(:id).each { |channel|
  13. if system_online_service && channel.preferences && channel.preferences['online_service_disable']
  14. email_addresses = EmailAddress.where(channel_id: channel.id)
  15. email_addresses.each { |email_address|
  16. accounts_fixed.push email_address
  17. }
  18. next
  19. end
  20. if channel.area == 'Email::Account'
  21. account_channel_ids.push channel.id
  22. assets = channel.assets(assets)
  23. elsif channel.area == 'Email::Notification' && channel.active
  24. notification_channel_ids.push channel.id
  25. assets = channel.assets(assets)
  26. end
  27. }
  28. EmailAddress.all.each { |email_address|
  29. next if system_online_service && email_address.preferences && email_address.preferences['online_service_disable']
  30. email_address_ids.push email_address.id
  31. assets = email_address.assets(assets)
  32. if !email_address.channel_id || !email_address.active || !Channel.find_by(id: email_address.channel_id)
  33. not_used_email_address_ids.push email_address.id
  34. end
  35. }
  36. render json: {
  37. accounts_fixed: accounts_fixed,
  38. assets: assets,
  39. account_channel_ids: account_channel_ids,
  40. notification_channel_ids: notification_channel_ids,
  41. email_address_ids: email_address_ids,
  42. not_used_email_address_ids: not_used_email_address_ids,
  43. channel_driver: {
  44. email: EmailHelper.available_driver,
  45. },
  46. config: {
  47. notification_sender: Setting.get('notification_sender'),
  48. }
  49. }
  50. end
  51. def probe
  52. # probe settings based on email and password
  53. result = EmailHelper::Probe.full(
  54. email: params[:email],
  55. password: params[:password],
  56. folder: params[:folder],
  57. )
  58. # verify if user+host already exists
  59. if result[:result] == 'ok'
  60. return if account_duplicate?(result)
  61. end
  62. render json: result
  63. end
  64. def outbound
  65. # verify access
  66. return if params[:channel_id] && !check_access(params[:channel_id])
  67. # connection test
  68. render json: EmailHelper::Probe.outbound(params, params[:email])
  69. end
  70. def inbound
  71. # verify access
  72. return if params[:channel_id] && !check_access(params[:channel_id])
  73. # connection test
  74. result = EmailHelper::Probe.inbound(params)
  75. # check account duplicate
  76. return if account_duplicate?({ setting: { inbound: params } }, params[:channel_id])
  77. render json: result
  78. end
  79. def verify
  80. email = params[:email] || params[:meta][:email]
  81. email = email.downcase
  82. channel_id = params[:channel_id]
  83. # verify access
  84. return if channel_id && !check_access(channel_id)
  85. # check account duplicate
  86. return if account_duplicate?({ setting: { inbound: params[:inbound] } }, channel_id)
  87. # check delivery for 30 sek.
  88. result = EmailHelper::Verify.email(
  89. outbound: params[:outbound],
  90. inbound: params[:inbound],
  91. sender: email,
  92. subject: params[:subject],
  93. )
  94. if result[:result] != 'ok'
  95. render json: result
  96. return
  97. end
  98. # fallback
  99. if !params[:group_id]
  100. params[:group_id] = Group.first.id
  101. end
  102. # update account
  103. if channel_id
  104. channel = Channel.find(channel_id)
  105. channel.update_attributes(
  106. options: {
  107. inbound: params[:inbound],
  108. outbound: params[:outbound],
  109. },
  110. group_id: params[:group_id],
  111. last_log_in: nil,
  112. last_log_out: nil,
  113. status_in: 'ok',
  114. status_out: 'ok',
  115. )
  116. render json: result
  117. return
  118. end
  119. # create new account
  120. channel = Channel.create(
  121. area: 'Email::Account',
  122. options: {
  123. inbound: params[:inbound],
  124. outbound: params[:outbound],
  125. },
  126. group_id: params[:group_id],
  127. last_log_in: nil,
  128. last_log_out: nil,
  129. status_in: 'ok',
  130. status_out: 'ok',
  131. active: true,
  132. )
  133. # remember address && set channel for email address
  134. address = EmailAddress.find_by(email: email)
  135. # if we are on initial setup, use already exisiting dummy email address
  136. if Channel.count == 1
  137. address = EmailAddress.first
  138. end
  139. if address
  140. address.update_attributes(
  141. realname: params[:meta][:realname],
  142. email: email,
  143. active: true,
  144. channel_id: channel.id,
  145. )
  146. else
  147. address = EmailAddress.create(
  148. realname: params[:meta][:realname],
  149. email: email,
  150. active: true,
  151. channel_id: channel.id,
  152. )
  153. end
  154. render json: result
  155. end
  156. def enable
  157. channel = Channel.find_by(id: params[:id], area: 'Email::Account')
  158. channel.active = true
  159. channel.save!
  160. render json: {}
  161. end
  162. def disable
  163. channel = Channel.find_by(id: params[:id], area: 'Email::Account')
  164. channel.active = false
  165. channel.save!
  166. render json: {}
  167. end
  168. def destroy
  169. channel = Channel.find_by(id: params[:id], area: 'Email::Account')
  170. channel.destroy
  171. render json: {}
  172. end
  173. def group
  174. check_access
  175. channel = Channel.find_by(id: params[:id], area: 'Email::Account')
  176. channel.group_id = params[:group_id]
  177. channel.save!
  178. render json: {}
  179. end
  180. def notification
  181. check_online_service
  182. adapter = params[:adapter].downcase
  183. email = Setting.get('notification_sender')
  184. # connection test
  185. result = EmailHelper::Probe.outbound(params, email)
  186. # save settings
  187. if result[:result] == 'ok'
  188. Channel.where(area: 'Email::Notification').each { |channel|
  189. active = false
  190. if adapter =~ /^#{channel.options[:outbound][:adapter]}$/i
  191. active = true
  192. channel.options = {
  193. outbound: {
  194. adapter: adapter,
  195. options: params[:options],
  196. },
  197. }
  198. channel.status_out = 'ok'
  199. channel.last_log_out = nil
  200. end
  201. channel.active = active
  202. channel.save
  203. }
  204. end
  205. render json: result
  206. end
  207. private
  208. def account_duplicate?(result, channel_id = nil)
  209. Channel.where(area: 'Email::Account').each { |channel|
  210. next if !channel.options
  211. next if !channel.options[:inbound]
  212. next if !channel.options[:inbound][:adapter]
  213. next if channel.options[:inbound][:adapter] != result[:setting][:inbound][:adapter]
  214. next if channel.options[:inbound][:options][:host] != result[:setting][:inbound][:options][:host]
  215. next if channel.options[:inbound][:options][:user] != result[:setting][:inbound][:options][:user]
  216. next if channel.id.to_s == channel_id.to_s
  217. render json: {
  218. result: 'duplicate',
  219. message: 'Account already exists!',
  220. }
  221. return true
  222. }
  223. false
  224. end
  225. def check_online_service
  226. return true if !Setting.get('system_online_service')
  227. raise Exceptions::NotAuthorized
  228. end
  229. def check_access(id = nil)
  230. if !id
  231. id = params[:id]
  232. end
  233. return true if !Setting.get('system_online_service')
  234. channel = Channel.find(id)
  235. return true if channel.preferences && !channel.preferences[:online_service_disable]
  236. raise Exceptions::NotAuthorized
  237. end
  238. end