channels_email_controller.rb 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://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. return if result[:result] == 'ok' && account_duplicate?(result)
  59. render json: result
  60. end
  61. def outbound
  62. # verify access
  63. return if params[:channel_id] && !check_access(params[:channel_id])
  64. # connection test
  65. render json: EmailHelper::Probe.outbound(params, params[:email])
  66. end
  67. def inbound
  68. # verify access
  69. return if params[:channel_id] && !check_access(params[:channel_id])
  70. # connection test
  71. result = EmailHelper::Probe.inbound(params)
  72. # check account duplicate
  73. return if account_duplicate?({ setting: { inbound: params } }, params[:channel_id])
  74. render json: result
  75. end
  76. def verify
  77. params.permit!
  78. email = params[:email] || params[:meta][:email]
  79. email = email.downcase
  80. channel_id = params[:channel_id]
  81. # verify access
  82. return if channel_id && !check_access(channel_id)
  83. # check account duplicate
  84. return if account_duplicate?({ setting: { inbound: params[:inbound] } }, channel_id)
  85. # check delivery for 30 sec.
  86. result = EmailHelper::Verify.email(
  87. outbound: params[:outbound].to_h,
  88. inbound: params[:inbound].to_h,
  89. sender: email,
  90. subject: params[:subject],
  91. )
  92. if result[:result] != 'ok'
  93. render json: result
  94. return
  95. end
  96. # fallback
  97. if !params[:group_id]
  98. params[:group_id] = Group.first.id
  99. end
  100. # update account
  101. if channel_id
  102. channel = Channel.find(channel_id)
  103. channel.update!(
  104. options: {
  105. inbound: params[:inbound].to_h,
  106. outbound: params[:outbound].to_h,
  107. },
  108. group_id: params[:group_id],
  109. last_log_in: nil,
  110. last_log_out: nil,
  111. status_in: 'ok',
  112. status_out: 'ok',
  113. )
  114. render json: result
  115. return
  116. end
  117. # create new account
  118. channel = Channel.create(
  119. area: 'Email::Account',
  120. options: {
  121. inbound: params[:inbound].to_h,
  122. outbound: params[:outbound].to_h,
  123. },
  124. group_id: params[:group_id],
  125. last_log_in: nil,
  126. last_log_out: nil,
  127. status_in: 'ok',
  128. status_out: 'ok',
  129. active: true,
  130. )
  131. # remember address && set channel for email address
  132. address = EmailAddress.find_by(email: email)
  133. # on initial setup, use placeholder email address
  134. if Channel.count == 1
  135. address = EmailAddress.first
  136. end
  137. if address
  138. address.update!(
  139. realname: params[:meta][:realname],
  140. email: email,
  141. active: true,
  142. channel_id: channel.id,
  143. )
  144. else
  145. EmailAddress.create(
  146. realname: params[:meta][:realname],
  147. email: email,
  148. active: true,
  149. channel_id: channel.id,
  150. )
  151. end
  152. render json: result
  153. end
  154. def enable
  155. channel = Channel.find_by(id: params[:id], area: 'Email::Account')
  156. channel.active = true
  157. channel.save!
  158. render json: {}
  159. end
  160. def disable
  161. channel = Channel.find_by(id: params[:id], area: 'Email::Account')
  162. channel.active = false
  163. channel.save!
  164. render json: {}
  165. end
  166. def destroy
  167. channel = Channel.find_by(id: params[:id], area: 'Email::Account')
  168. channel.destroy
  169. render json: {}
  170. end
  171. def group
  172. check_access
  173. channel = Channel.find_by(id: params[:id], area: 'Email::Account')
  174. channel.group_id = params[:group_id]
  175. channel.save!
  176. render json: {}
  177. end
  178. def notification
  179. params.permit!
  180. check_online_service
  181. adapter = params[:adapter].downcase
  182. email = Setting.get('notification_sender')
  183. # connection test
  184. result = EmailHelper::Probe.outbound(params, email)
  185. # save settings
  186. if result[:result] == 'ok'
  187. Channel.where(area: 'Email::Notification').each do |channel|
  188. active = false
  189. if adapter.match?(%r{^#{channel.options[:outbound][:adapter]}$}i)
  190. active = true
  191. channel.options = {
  192. outbound: {
  193. adapter: adapter,
  194. options: params[:options].to_h,
  195. },
  196. }
  197. channel.status_out = 'ok'
  198. channel.last_log_out = nil
  199. end
  200. channel.active = active
  201. channel.save
  202. end
  203. end
  204. render json: result
  205. end
  206. private
  207. def account_duplicate?(result, channel_id = nil)
  208. Channel.where(area: 'Email::Account').each do |channel|
  209. next if !channel.options
  210. next if !channel.options[:inbound]
  211. next if !channel.options[:inbound][:adapter]
  212. next if channel.options[:inbound][:adapter] != result[:setting][:inbound][:adapter]
  213. next if channel.options[:inbound][:options][:host] != result[:setting][:inbound][:options][:host]
  214. next if channel.options[:inbound][:options][:user] != result[:setting][:inbound][:options][:user]
  215. next if channel.options[:inbound][:options][:folder].to_s != result[:setting][:inbound][:options][:folder].to_s
  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. end
  223. false
  224. end
  225. def check_online_service
  226. return true if !Setting.get('system_online_service')
  227. raise Exceptions::Forbidden
  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::Forbidden
  237. end
  238. end