channels_controller.rb 9.1 KB

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