form_controller.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class FormController < ApplicationController
  3. def config
  4. return if !enabled?
  5. api_path = Rails.configuration.api_path
  6. http_type = Setting.get('http_type')
  7. fqdn = Setting.get('fqdn')
  8. endpoint = "#{http_type}://#{fqdn}#{api_path}/form_submit"
  9. config = {
  10. enabled: Setting.get('form_ticket_create'),
  11. endpoint: endpoint,
  12. }
  13. if params[:test] && current_user && current_user.permissions?('admin.channel_formular')
  14. config[:enabled] = true
  15. end
  16. render json: config, status: :ok
  17. end
  18. def submit
  19. return if !enabled?
  20. # validate input
  21. errors = {}
  22. if !params[:name] || params[:name].empty?
  23. errors['name'] = 'required'
  24. end
  25. if !params[:email] || params[:email].empty?
  26. errors['email'] = 'required'
  27. end
  28. if params[:email] !~ /@/
  29. errors['email'] = 'invalid'
  30. end
  31. if params[:email] =~ /(>|<|\||\!|"|§|'|\$|%|&|\(|\)|\?|\s)/
  32. errors['email'] = 'invalid'
  33. end
  34. if !params[:title] || params[:title].empty?
  35. errors['title'] = 'required'
  36. end
  37. if !params[:body] || params[:body].empty?
  38. errors['body'] = 'required'
  39. end
  40. # realtime verify
  41. if !errors['email']
  42. begin
  43. checker = EmailVerifier::Checker.new(params[:email])
  44. checker.connect
  45. if !checker.verify
  46. errors['email'] = "Unable to send to '#{params[:email]}'"
  47. end
  48. rescue => e
  49. message = e.to_s
  50. Rails.logger.info "Can't verify email #{params[:email]}: #{message}"
  51. # ignore 450, graylistings
  52. if message !~ /450/
  53. errors['email'] = message
  54. end
  55. end
  56. end
  57. if errors && !errors.empty?
  58. render json: {
  59. errors: errors
  60. }, status: :ok
  61. return
  62. end
  63. name = params[:name].strip
  64. email = params[:email].strip.downcase
  65. customer = User.find_by(email: email)
  66. if !customer
  67. role_ids = Role.signup_role_ids
  68. customer = User.create(
  69. firstname: name,
  70. lastname: '',
  71. email: email,
  72. password: '',
  73. active: true,
  74. role_ids: role_ids,
  75. updated_by_id: 1,
  76. created_by_id: 1,
  77. )
  78. end
  79. # set current user
  80. UserInfo.current_user_id = customer.id
  81. ticket = Ticket.create(
  82. group_id: 1,
  83. customer_id: customer.id,
  84. title: params[:title],
  85. state_id: Ticket::State.find_by(name: 'new').id,
  86. priority_id: Ticket::Priority.find_by(name: '2 normal').id,
  87. )
  88. article = Ticket::Article.create(
  89. ticket_id: ticket.id,
  90. type_id: Ticket::Article::Type.find_by(name: 'web').id,
  91. sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
  92. body: params[:body],
  93. subject: params[:title],
  94. internal: false,
  95. )
  96. UserInfo.current_user_id = 1
  97. result = {}
  98. render json: result, status: :ok
  99. end
  100. private
  101. def enabled?
  102. return true if params[:test] && current_user && current_user.permissions?('admin.channel_formular')
  103. return true if Setting.get('form_ticket_create')
  104. response_access_deny
  105. false
  106. end
  107. end