channels_email_controller.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class ChannelsEmailController < ApplicationController
  3. prepend_before_action :authenticate_and_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.reorder(: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. ::Service::Channel::Email::Create.new.execute(
  118. inbound_configuration: params[:inbound].to_h,
  119. outbound_configuration: params[:outbound].to_h,
  120. group: ::Group.find(params[:group_id]),
  121. email_address: email,
  122. email_realname: params[:meta][:realname],
  123. )
  124. render json: result
  125. end
  126. def enable
  127. channel = Channel.find_by(id: params[:id], area: 'Email::Account')
  128. channel.active = true
  129. channel.save!
  130. render json: {}
  131. end
  132. def disable
  133. channel = Channel.find_by(id: params[:id], area: 'Email::Account')
  134. channel.active = false
  135. channel.save!
  136. render json: {}
  137. end
  138. def destroy
  139. channel = Channel.find_by(id: params[:id], area: 'Email::Account')
  140. channel.destroy
  141. render json: {}
  142. end
  143. def group
  144. check_access
  145. channel = Channel.find_by(id: params[:id], area: 'Email::Account')
  146. channel.group_id = params[:group_id]
  147. channel.save!
  148. render json: {}
  149. end
  150. def notification
  151. params.permit!
  152. check_online_service
  153. adapter = params[:adapter].downcase
  154. email = Setting.get('notification_sender')
  155. # connection test
  156. result = EmailHelper::Probe.outbound(params, email)
  157. # save settings
  158. if result[:result] == 'ok'
  159. Service::System::SetEmailNotificationConfiguration
  160. .new(
  161. adapter:,
  162. new_configuration: params[:options].to_h
  163. ).execute
  164. end
  165. render json: result
  166. end
  167. private
  168. def account_duplicate?(result, channel_id = nil)
  169. Channel.where(area: 'Email::Account').each do |channel|
  170. next if !channel.options
  171. next if !channel.options[:inbound]
  172. next if !channel.options[:inbound][:adapter]
  173. next if channel.options[:inbound][:adapter] != result[:setting][:inbound][:adapter]
  174. next if channel.options[:inbound][:options][:host] != result[:setting][:inbound][:options][:host]
  175. next if channel.options[:inbound][:options][:user] != result[:setting][:inbound][:options][:user]
  176. next if channel.options[:inbound][:options][:folder].to_s != result[:setting][:inbound][:options][:folder].to_s
  177. next if channel.id.to_s == channel_id.to_s
  178. render json: {
  179. result: 'duplicate',
  180. message: __('Account already exists!'),
  181. }
  182. return true
  183. end
  184. false
  185. end
  186. def check_online_service
  187. return true if !Setting.get('system_online_service')
  188. raise Exceptions::Forbidden
  189. end
  190. def check_access(id = nil)
  191. if !id
  192. id = params[:id]
  193. end
  194. return true if !Setting.get('system_online_service')
  195. channel = Channel.find(id)
  196. return true if channel.preferences && !channel.preferences[:online_service_disable]
  197. raise Exceptions::Forbidden
  198. end
  199. end