postmaster_filter.rb 1.1 KB

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