checks_html_sanitized.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. module ChecksHtmlSanitized
  3. extend ActiveSupport::Concern
  4. included do
  5. before_create :sanitized_html_attributes
  6. before_update :sanitized_html_attributes
  7. end
  8. def sanitized_html_attributes
  9. html_attributes = self.class.instance_variable_get(:@sanitized_html) || []
  10. return true if html_attributes.blank?
  11. html_attributes.each do |attribute|
  12. next if changes[attribute].blank?
  13. value = send(attribute)
  14. next if value.blank?
  15. next if !sanitizeable?(attribute, value)
  16. send(:"#{attribute}=", HtmlSanitizer.strict(value))
  17. end
  18. true
  19. end
  20. def sanitizeable?(_attribute, _value)
  21. true
  22. end
  23. # methods defined here are going to extend the class, not the instance of it
  24. class_methods do
  25. =begin
  26. serve method to mark HTML attributes that need to get sanitized
  27. class Model < ApplicationModel
  28. include Sanitized
  29. sanitized_html :body
  30. end
  31. =end
  32. def sanitized_html(*attributes)
  33. @sanitized_html = attributes
  34. end
  35. end
  36. end