channels_email_controller.rb 7.3 KB

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