webhook.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Webhook < ApplicationModel
  3. include ChecksClientNotification
  4. include ChecksHtmlSanitized
  5. include HasCollectionUpdate
  6. include HasSearchIndexBackend
  7. include CanSelector
  8. include CanSearch
  9. before_save :reset_custom_payload
  10. before_destroy Webhook::EnsureNoRelatedObjects
  11. validates :name, presence: true
  12. validate :validate_endpoint
  13. validate :validate_custom_payload
  14. validates :note, length: { maximum: 500 }
  15. sanitized_html :note
  16. store :preferences
  17. private
  18. def reset_custom_payload
  19. return true if customized_payload
  20. self.custom_payload = nil
  21. true
  22. end
  23. def validate_endpoint
  24. uri = URI.parse(endpoint)
  25. errors.add(:endpoint, __('The provided endpoint is invalid, no http or https protocol was specified.')) if !uri.is_a?(URI::HTTP)
  26. errors.add(:endpoint, __('The provided endpoint is invalid, no hostname was specified.')) if uri.host.blank?
  27. rescue URI::InvalidURIError
  28. errors.add :endpoint, __('The provided endpoint is invalid.')
  29. end
  30. def validate_custom_payload
  31. return true if custom_payload.blank?
  32. begin
  33. JSON.parse(custom_payload)
  34. rescue
  35. errors.add :custom_payload, __('The provided payload is invalid. Please check your syntax.')
  36. end
  37. true
  38. end
  39. end