postmaster_filter.rb 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. # Copyright (C) 2012-2022 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. before_create :validate_condition
  8. before_update :validate_condition
  9. sanitized_html :note
  10. def validate_condition
  11. raise Exceptions::UnprocessableEntity, __('At least one match rule is required, but none was provided.') if match.blank?
  12. match.each_value do |meta|
  13. raise Exceptions::UnprocessableEntity, __('The provided match operator is missing or invalid.') if meta['operator'].blank? || meta['operator'] !~ %r{^(contains|contains not)$}
  14. raise Exceptions::UnprocessableEntity, __('The required match value is missing.') if meta['value'].blank?
  15. begin
  16. Channel::Filter::Match::EmailRegex.match(value: 'test content', match_rule: meta['value'], check_mode: true)
  17. rescue => e
  18. raise Exceptions::UnprocessableEntity, e.message
  19. end
  20. end
  21. true
  22. end
  23. end