channels_email_controller.rb 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class ChannelsEmailController < ApplicationController
  3. prepend_before_action { authentication_check && authorize! }
  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 do |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 do |email_address|
  16. accounts_fixed.push email_address
  17. end
  18. next
  19. end
  20. assets = channel.assets(assets)
  21. if channel.area == 'Email::Account'
  22. account_channel_ids.push channel.id
  23. elsif channel.area == 'Email::Notification' && channel.active
  24. notification_channel_ids.push channel.id
  25. end
  26. end
  27. EmailAddress.all.each do |email_address|
  28. next if system_online_service && email_address.preferences && email_address.preferences['online_service_disable']
  29. email_address_ids.push email_address.id
  30. assets = email_address.assets(assets)
  31. if !email_address.channel_id || !email_address.active || !Channel.exists?(id: email_address.channel_id)
  32. not_used_email_address_ids.push email_address.id
  33. end
  34. end
  35. render json: {
  36. accounts_fixed: accounts_fixed,
  37. assets: assets,
  38. account_channel_ids: account_channel_ids,
  39. notification_channel_ids: notification_channel_ids,
  40. email_address_ids: email_address_ids,
  41. not_used_email_address_ids: not_used_email_address_ids,
  42. channel_driver: {
  43. email: EmailHelper.available_driver,
  44. },
  45. config: {
  46. notification_sender: Setting.get('notification_sender'),
  47. }
  48. }
  49. end
  50. def probe
  51. # probe settings based on email and password
  52. result = EmailHelper::Probe.full(
  53. email: params[:email],
  54. password: params[:password],
  55. folder: params[:folder],
  56. )
  57. # verify if user+host already exists
  58. if result[:result] == 'ok'
  59. return if account_duplicate?(result)
  60. end
  61. render json: result
  62. end
  63. def outbound
  64. # verify access
  65. return if params[:channel_id] && !check_access(params[:channel_id])
  66. # connection test
  67. render json: EmailHelper::Probe.outbound(params, params[:email])
  68. end
  69. def inbound
  70. # verify access
  71. return if params[:channel_id] && !check_access(params[:channel_id])
  72. # connection test
  73. result = EmailHelper::Probe.inbound(params)
  74. # check account duplicate
  75. return if account_duplicate?({ setting: { inbound: params } }, params[:channel_id])
  76. render json: result
  77. end
  78. def verify
  79. params.permit!
  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 sec.
  88. result = EmailHelper::Verify.email(
  89. outbound: params[:outbound].to_h,
  90. inbound: params[:inbound].to_h,
  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!(
  106. options: {
  107. inbound: params[:inbound].to_h,
  108. outbound: params[:outbound].to_h,
  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].to_h,
  124. outbound: params[:outbound].to_h,
  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. # on initial setup, use placeholder email address
  136. if Channel.count == 1
  137. address = EmailAddress.first
  138. end
  139. if address
  140. address.update!(
  141. realname: params[:meta][:realname],
  142. email: email,
  143. active: true,
  144. channel_id: channel.id,
  145. )
  146. else
  147. 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. params.permit!
  182. check_online_service
  183. adapter = params[:adapter].downcase
  184. email = Setting.get('notification_sender')
  185. # connection test
  186. result = EmailHelper::Probe.outbound(params, email)
  187. # save settings
  188. if result[:result] == 'ok'
  189. Channel.where(area: 'Email::Notification').each do |channel|
  190. active = false
  191. if adapter.match?(/^#{channel.options[:outbound][:adapter]}$/i)
  192. active = true
  193. channel.options = {
  194. outbound: {
  195. adapter: adapter,
  196. options: params[:options].to_h,
  197. },
  198. }
  199. channel.status_out = 'ok'
  200. channel.last_log_out = nil
  201. end
  202. channel.active = active
  203. channel.save
  204. end
  205. end
  206. render json: result
  207. end
  208. private
  209. def account_duplicate?(result, channel_id = nil)
  210. Channel.where(area: 'Email::Account').each do |channel|
  211. next if !channel.options
  212. next if !channel.options[:inbound]
  213. next if !channel.options[:inbound][:adapter]
  214. next if channel.options[:inbound][:adapter] != result[:setting][:inbound][:adapter]
  215. next if channel.options[:inbound][:options][:host] != result[:setting][:inbound][:options][:host]
  216. next if channel.options[:inbound][:options][:user] != result[:setting][:inbound][:options][:user]
  217. next if channel.options[:inbound][:options][:folder].to_s != result[:setting][:inbound][:options][:folder].to_s
  218. next if channel.id.to_s == channel_id.to_s
  219. render json: {
  220. result: 'duplicate',
  221. message: 'Account already exists!',
  222. }
  223. return true
  224. end
  225. false
  226. end
  227. def check_online_service
  228. return true if !Setting.get('system_online_service')
  229. raise Exceptions::NotAuthorized
  230. end
  231. def check_access(id = nil)
  232. if !id
  233. id = params[:id]
  234. end
  235. return true if !Setting.get('system_online_service')
  236. channel = Channel.find(id)
  237. return true if channel.preferences && !channel.preferences[:online_service_disable]
  238. raise Exceptions::NotAuthorized
  239. end
  240. end