email_domain_block.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: email_domain_blocks
  5. #
  6. # id :bigint(8) not null, primary key
  7. # domain :string default(""), not null
  8. # created_at :datetime not null
  9. # updated_at :datetime not null
  10. # parent_id :bigint(8)
  11. #
  12. class EmailDomainBlock < ApplicationRecord
  13. self.ignored_columns = %w(
  14. ips
  15. last_refresh_at
  16. )
  17. include DomainNormalizable
  18. include Paginable
  19. belongs_to :parent, class_name: 'EmailDomainBlock', optional: true
  20. has_many :children, class_name: 'EmailDomainBlock', foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
  21. validates :domain, presence: true, uniqueness: true, domain: true
  22. # Used for adding multiple blocks at once
  23. attr_accessor :other_domains
  24. def to_log_human_identifier
  25. domain
  26. end
  27. def history
  28. @history ||= Trends::History.new('email_domain_blocks', id)
  29. end
  30. class Matcher
  31. def initialize(domain_or_domains, attempt_ip: nil)
  32. @uris = extract_uris(domain_or_domains)
  33. @attempt_ip = attempt_ip
  34. end
  35. def match?
  36. blocking? || invalid_uri?
  37. end
  38. private
  39. def invalid_uri?
  40. @uris.any?(&:nil?)
  41. end
  42. def blocking?
  43. blocks = EmailDomainBlock.where(domain: domains_with_variants).order(Arel.sql('char_length(domain) desc'))
  44. blocks.each { |block| block.history.add(@attempt_ip) } if @attempt_ip.present?
  45. blocks.any?
  46. end
  47. def domains_with_variants
  48. @uris.flat_map do |uri|
  49. next if uri.nil?
  50. segments = uri.normalized_host.split('.')
  51. segments.map.with_index { |_, i| segments[i..-1].join('.') }
  52. end
  53. end
  54. def extract_uris(domain_or_domains)
  55. Array(domain_or_domains).map do |str|
  56. domain = begin
  57. if str.include?('@')
  58. str.split('@', 2).last
  59. else
  60. str
  61. end
  62. end
  63. Addressable::URI.new.tap { |u| u.host = domain.strip } if domain.present?
  64. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  65. nil
  66. end
  67. end
  68. end
  69. def self.block?(domain_or_domains, attempt_ip: nil)
  70. Matcher.new(domain_or_domains, attempt_ip: attempt_ip).match?
  71. end
  72. end