postmaster_filter.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class PostmasterFilter < ApplicationModel
  3. include ChecksHtmlSanitized
  4. store :perform
  5. store :match
  6. validates :name, presence: true
  7. validates :perform, 'validations/verify_perform_rules': true
  8. before_create :validate_condition
  9. before_update :validate_condition
  10. validates :note, length: { maximum: 250 }
  11. sanitized_html :note
  12. VALID_OPERATORS = [
  13. 'contains',
  14. 'contains not',
  15. 'is any of',
  16. 'is none of',
  17. 'starts with one of',
  18. 'ends with one of',
  19. 'matches regex',
  20. 'does not match regex',
  21. ].freeze
  22. def validate_condition
  23. raise Exceptions::UnprocessableEntity, __('At least one match rule is required, but none was provided.') if match.blank?
  24. match.each_value do |meta|
  25. raise Exceptions::UnprocessableEntity, __('The provided match operator is missing or invalid.') if meta['operator'].blank? || VALID_OPERATORS.exclude?(meta['operator'])
  26. raise Exceptions::UnprocessableEntity, __('The required match value is missing.') if meta['value'].blank?
  27. validate_regex_match_rule!(meta['value'], meta['operator'])
  28. end
  29. true
  30. end
  31. private
  32. def validate_regex_match_rule!(match_rule, operator)
  33. return if !operator.eql?('matches regex') && !operator.eql?('does not match regex')
  34. Channel::Filter::Match::EmailRegex.match(value: 'test content', match_rule: match_rule, check_mode: true)
  35. rescue => e
  36. raise Exceptions::UnprocessableEntity, e.message
  37. end
  38. end