channels_sms_controller.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class ChannelsSmsController < ApplicationController
  3. prepend_before_action :authenticate_and_authorize!, except: [:webhook]
  4. skip_before_action :verify_csrf_token, only: [:webhook]
  5. def index
  6. assets = {}
  7. render json: {
  8. account_channel_ids: channels_data('Sms::Account', assets),
  9. notification_channel_ids: channels_data('Sms::Notification', assets),
  10. config: channels_config,
  11. assets: assets
  12. }
  13. end
  14. def show
  15. model_show_render(Channel, params)
  16. end
  17. def create
  18. model_create_render(Channel, channel_params)
  19. end
  20. def update
  21. model_update_render(Channel, channel_params)
  22. end
  23. def enable
  24. channel.update!(active: true)
  25. render json: channel
  26. end
  27. def disable
  28. channel.update!(active: false)
  29. render json: channel
  30. end
  31. def destroy
  32. channel.destroy!
  33. render json: {}
  34. end
  35. def test
  36. raise __("The required parameter 'options.adapter' is missing.") if params[:options][:adapter].blank?
  37. driver = Channel.driver_class(params[:options][:adapter])
  38. resp = driver.new.deliver(params[:options], test_options)
  39. render json: { success: resp }
  40. rescue => e
  41. render json: { error: e.inspect, error_human: e.message }
  42. end
  43. def webhook
  44. raise Exceptions::UnprocessableEntity, 'token param missing' if params['token'].blank?
  45. ApplicationHandleInfo.in_context('sms') do
  46. channel = nil
  47. Channel.where(active: true, area: 'Sms::Account').each do |local_channel|
  48. next if local_channel.options[:webhook_token] != params['token']
  49. channel = local_channel
  50. end
  51. if !channel
  52. render(
  53. json: { message: 'channel not found' },
  54. status: :not_found
  55. )
  56. return
  57. end
  58. content_type, content = channel.process(params.permit!.to_h)
  59. send_data content, type: content_type
  60. end
  61. end
  62. private
  63. def channel
  64. @channel ||= Channel.lookup(id: params[:id])
  65. end
  66. def test_options
  67. params.permit(:recipient, :message)
  68. end
  69. def channel_params
  70. raise __("The required parameter 'area' is missing.") if params[:area].blank?
  71. if ['Sms::Notification', 'Sms::Account'].exclude?(params[:area])
  72. raise "The parameter 'area' has the invalid value '#{params[:area]}'!"
  73. end
  74. raise __("The required parameter 'params' is missing.") if params[:options].blank?
  75. raise __("The required parameter 'options.adapter' is missing.") if params[:options][:adapter].blank?
  76. params
  77. end
  78. def channels_config
  79. list = []
  80. Rails.root.glob('app/models/channel/driver/sms/*.rb').each do |path|
  81. filename = File.basename(path)
  82. next if !Channel.driver_class("sms/#{filename}").const_defined?(:NAME)
  83. list.push Channel.driver_class("sms/#{filename}").definition
  84. end
  85. list
  86. end
  87. def channels_data(area, assets)
  88. channel_ids = []
  89. Channel.where(area: area).each do |channel|
  90. assets = channel.assets(assets)
  91. channel_ids.push(channel.id)
  92. end
  93. channel_ids
  94. end
  95. end