sets_headers.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module ApplicationController::SetsHeaders
  3. extend ActiveSupport::Concern
  4. included do
  5. before_action :cors_preflight_check
  6. after_action :set_access_control_headers
  7. end
  8. private
  9. # For all responses in this controller, return the CORS access control headers.
  10. def set_access_control_headers
  11. return if @_auth_type != 'token_auth' && @_auth_type != 'basic_auth'
  12. set_access_control_headers_execute
  13. end
  14. def set_access_control_headers_execute
  15. headers['Access-Control-Allow-Origin'] = '*'
  16. headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, PATCH, OPTIONS'
  17. headers['Access-Control-Max-Age'] = '1728000'
  18. headers['Access-Control-Allow-Headers'] = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Accept-Language' # rubocop:disable Zammad/DetectTranslatableString
  19. end
  20. # If this is a preflight OPTIONS request, then short-circuit the
  21. # request, return only the necessary headers and return an empty
  22. # text/plain.
  23. def cors_preflight_check
  24. return if request.method != 'OPTIONS'
  25. headers['Access-Control-Allow-Origin'] = '*'
  26. headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, PATCH, OPTIONS'
  27. headers['Access-Control-Allow-Headers'] = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Accept-Language' # rubocop:disable Zammad/DetectTranslatableString
  28. headers['Access-Control-Max-Age'] = '1728000'
  29. render plain: ''
  30. end
  31. end