channels_email_controller.rb 7.6 KB

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