channels_controller.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class ChannelsController < ApplicationController
  3. before_action :authentication_check
  4. =begin
  5. Resource:
  6. POST /api/v1/channels/group/{id}.json
  7. Response:
  8. {}
  9. Test:
  10. curl http://localhost/api/v1/group/channels.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST '{group_id:123}'
  11. =end
  12. def group_update
  13. permission_check('admin')
  14. check_access
  15. channel = Channel.find(params[:id])
  16. channel.group_id = params[:group_id]
  17. channel.save
  18. render json: {}
  19. end
  20. =begin
  21. Resource:
  22. DELETE /api/v1/channels/{id}.json
  23. Response:
  24. {}
  25. Test:
  26. curl http://localhost/api/v1/channels.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE
  27. =end
  28. def destroy
  29. permission_check('admin')
  30. check_access
  31. model_destory_render(Channel, params)
  32. end
  33. def twitter_index
  34. permission_check('admin.channel_twitter')
  35. assets = {}
  36. ExternalCredential.where(name: 'twitter').each { |external_credential|
  37. assets = external_credential.assets(assets)
  38. }
  39. channel_ids = []
  40. Channel.order(:id).each { |channel|
  41. next if channel.area != 'Twitter::Account'
  42. assets = channel.assets(assets)
  43. channel_ids.push channel.id
  44. }
  45. render json: {
  46. assets: assets,
  47. channel_ids: channel_ids,
  48. callback_url: ExternalCredential.callback_url('twitter'),
  49. }
  50. end
  51. def twitter_verify
  52. permission_check('admin.channel_twitter')
  53. model_update_render(Channel, params)
  54. end
  55. def facebook_index
  56. permission_check('admin.channel_facebook')
  57. assets = {}
  58. ExternalCredential.where(name: 'facebook').each { |external_credential|
  59. assets = external_credential.assets(assets)
  60. }
  61. channel_ids = []
  62. Channel.order(:id).each { |channel|
  63. next if channel.area != 'Facebook::Account'
  64. assets = channel.assets(assets)
  65. channel_ids.push channel.id
  66. }
  67. render json: {
  68. assets: assets,
  69. channel_ids: channel_ids,
  70. callback_url: ExternalCredential.callback_url('facebook'),
  71. }
  72. end
  73. def facebook_verify
  74. permission_check('admin.channel_facebook')
  75. model_update_render(Channel, params)
  76. end
  77. def email_index
  78. permission_check('admin.channel_email')
  79. system_online_service = Setting.get('system_online_service')
  80. account_channel_ids = []
  81. notification_channel_ids = []
  82. email_address_ids = []
  83. not_used_email_address_ids = []
  84. accounts_fixed = []
  85. assets = {}
  86. Channel.order(:id).each { |channel|
  87. if system_online_service && channel.preferences && channel.preferences['online_service_disable']
  88. email_addresses = EmailAddress.where(channel_id: channel.id)
  89. email_addresses.each { |email_address|
  90. accounts_fixed.push email_address
  91. }
  92. next
  93. end
  94. if channel.area == 'Email::Account'
  95. account_channel_ids.push channel.id
  96. assets = channel.assets(assets)
  97. elsif channel.area == 'Email::Notification' && channel.active
  98. notification_channel_ids.push channel.id
  99. assets = channel.assets(assets)
  100. end
  101. }
  102. EmailAddress.all.each { |email_address|
  103. next if system_online_service && email_address.preferences && email_address.preferences['online_service_disable']
  104. email_address_ids.push email_address.id
  105. assets = email_address.assets(assets)
  106. if !email_address.channel_id || !email_address.active || !Channel.find_by(id: email_address.channel_id)
  107. not_used_email_address_ids.push email_address.id
  108. end
  109. }
  110. render json: {
  111. accounts_fixed: accounts_fixed,
  112. assets: assets,
  113. account_channel_ids: account_channel_ids,
  114. notification_channel_ids: notification_channel_ids,
  115. email_address_ids: email_address_ids,
  116. not_used_email_address_ids: not_used_email_address_ids,
  117. channel_driver: {
  118. email: EmailHelper.available_driver,
  119. },
  120. config: {
  121. notification_sender: Setting.get('notification_sender'),
  122. }
  123. }
  124. end
  125. def email_probe
  126. # check admin permissions
  127. permission_check('admin.channel_email')
  128. # probe settings based on email and password
  129. result = EmailHelper::Probe.full(
  130. email: params[:email],
  131. password: params[:password],
  132. folder: params[:folder],
  133. )
  134. # verify if user+host already exists
  135. if result[:result] == 'ok'
  136. return if email_account_duplicate?(result)
  137. end
  138. render json: result
  139. end
  140. def email_outbound
  141. # check admin permissions
  142. permission_check('admin.channel_email')
  143. # verify access
  144. return if params[:channel_id] && !check_access(params[:channel_id])
  145. # connection test
  146. render json: EmailHelper::Probe.outbound(params, params[:email])
  147. end
  148. def email_inbound
  149. # check admin permissions
  150. permission_check('admin.channel_email')
  151. # verify access
  152. return if params[:channel_id] && !check_access(params[:channel_id])
  153. # connection test
  154. result = EmailHelper::Probe.inbound(params)
  155. # check account duplicate
  156. return if email_account_duplicate?({ setting: { inbound: params } }, params[:channel_id])
  157. render json: result
  158. end
  159. def email_verify
  160. # check admin permissions
  161. permission_check('admin.channel_email')
  162. email = params[:email] || params[:meta][:email]
  163. email = email.downcase
  164. channel_id = params[:channel_id]
  165. # verify access
  166. return if channel_id && !check_access(channel_id)
  167. # check account duplicate
  168. return if email_account_duplicate?({ setting: { inbound: params[:inbound] } }, channel_id)
  169. # check delivery for 30 sek.
  170. result = EmailHelper::Verify.email(
  171. outbound: params[:outbound],
  172. inbound: params[:inbound],
  173. sender: email,
  174. subject: params[:subject],
  175. )
  176. if result[:result] != 'ok'
  177. render json: result
  178. return
  179. end
  180. # fallback
  181. if !params[:group_id]
  182. params[:group_id] = Group.first.id
  183. end
  184. # update account
  185. if channel_id
  186. channel = Channel.find(channel_id)
  187. channel.update_attributes(
  188. options: {
  189. inbound: params[:inbound],
  190. outbound: params[:outbound],
  191. },
  192. group_id: params[:group_id],
  193. last_log_in: nil,
  194. last_log_out: nil,
  195. status_in: 'ok',
  196. status_out: 'ok',
  197. )
  198. render json: result
  199. return
  200. end
  201. # create new account
  202. channel = Channel.create(
  203. area: 'Email::Account',
  204. options: {
  205. inbound: params[:inbound],
  206. outbound: params[:outbound],
  207. },
  208. group_id: params[:group_id],
  209. last_log_in: nil,
  210. last_log_out: nil,
  211. status_in: 'ok',
  212. status_out: 'ok',
  213. active: true,
  214. )
  215. # remember address && set channel for email address
  216. address = EmailAddress.find_by(email: email)
  217. # if we are on initial setup, use already exisiting dummy email address
  218. if Channel.count == 1
  219. address = EmailAddress.first
  220. end
  221. if address
  222. address.update_attributes(
  223. realname: params[:meta][:realname],
  224. email: email,
  225. active: true,
  226. channel_id: channel.id,
  227. )
  228. else
  229. address = EmailAddress.create(
  230. realname: params[:meta][:realname],
  231. email: email,
  232. active: true,
  233. channel_id: channel.id,
  234. )
  235. end
  236. render json: result
  237. end
  238. def email_notification
  239. check_online_service
  240. # check admin permissions
  241. permission_check('admin.channel_email')
  242. adapter = params[:adapter].downcase
  243. email = Setting.get('notification_sender')
  244. # connection test
  245. result = EmailHelper::Probe.outbound(params, email)
  246. # save settings
  247. if result[:result] == 'ok'
  248. Channel.where(area: 'Email::Notification').each { |channel|
  249. active = false
  250. if adapter =~ /^#{channel.options[:outbound][:adapter]}$/i
  251. active = true
  252. channel.options = {
  253. outbound: {
  254. adapter: adapter,
  255. options: params[:options],
  256. },
  257. }
  258. channel.status_out = 'ok'
  259. channel.last_log_out = nil
  260. end
  261. channel.active = active
  262. channel.save
  263. }
  264. end
  265. render json: result
  266. end
  267. private
  268. def email_account_duplicate?(result, channel_id = nil)
  269. Channel.where(area: 'Email::Account').each { |channel|
  270. next if !channel.options
  271. next if !channel.options[:inbound]
  272. next if !channel.options[:inbound][:adapter]
  273. next if channel.options[:inbound][:adapter] != result[:setting][:inbound][:adapter]
  274. next if channel.options[:inbound][:options][:host] != result[:setting][:inbound][:options][:host]
  275. next if channel.options[:inbound][:options][:user] != result[:setting][:inbound][:options][:user]
  276. next if channel.id.to_s == channel_id.to_s
  277. render json: {
  278. result: 'duplicate',
  279. message: 'Account already exists!',
  280. }
  281. return true
  282. }
  283. false
  284. end
  285. def check_online_service
  286. return true if !Setting.get('system_online_service')
  287. raise Exceptions::NotAuthorized
  288. end
  289. def check_access(id = nil)
  290. if !id
  291. id = params[:id]
  292. end
  293. return true if !Setting.get('system_online_service')
  294. channel = Channel.find(id)
  295. return true if channel.preferences && !channel.preferences[:online_service_disable]
  296. raise Exceptions::NotAuthorized
  297. end
  298. end