1
0

email_domain_block.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. # allow_with_approval :boolean default(FALSE), not null
  12. #
  13. class EmailDomainBlock < ApplicationRecord
  14. self.ignored_columns += %w(
  15. ips
  16. last_refresh_at
  17. )
  18. include DomainNormalizable
  19. include Paginable
  20. with_options class_name: 'EmailDomainBlock' do
  21. belongs_to :parent, optional: true
  22. has_many :children, foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
  23. end
  24. validates :domain, presence: true, uniqueness: true, domain: true
  25. scope :parents, -> { where(parent_id: nil) }
  26. # Used for adding multiple blocks at once
  27. attr_accessor :other_domains
  28. def to_log_human_identifier
  29. domain
  30. end
  31. def history
  32. @history ||= Trends::History.new('email_domain_blocks', id)
  33. end
  34. class Matcher
  35. def initialize(domain_or_domains, attempt_ip: nil)
  36. @uris = extract_uris(domain_or_domains)
  37. @attempt_ip = attempt_ip
  38. end
  39. def match?(...)
  40. blocking?(...) || invalid_uri?
  41. end
  42. private
  43. def invalid_uri?
  44. @uris.any?(&:nil?)
  45. end
  46. def blocking?(allow_with_approval: false)
  47. blocks = EmailDomainBlock.where(domain: domains_with_variants, allow_with_approval: allow_with_approval).by_domain_length
  48. blocks.each { |block| block.history.add(@attempt_ip) } if @attempt_ip.present?
  49. blocks.any?
  50. end
  51. def domains_with_variants
  52. @uris.flat_map do |uri|
  53. next if uri.nil?
  54. segments = uri.normalized_host.split('.')
  55. segments.map.with_index { |_, i| segments[i..].join('.') }
  56. end
  57. end
  58. def extract_uris(domain_or_domains)
  59. Array(domain_or_domains).map do |str|
  60. domain = if str.include?('@')
  61. str.split('@', 2).last
  62. else
  63. str
  64. end
  65. Addressable::URI.new.tap { |u| u.host = domain.strip } if domain.present?
  66. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  67. nil
  68. end
  69. end
  70. end
  71. def self.block?(domain_or_domains, attempt_ip: nil)
  72. Matcher.new(domain_or_domains, attempt_ip: attempt_ip).match?
  73. end
  74. def self.requires_approval?(domain_or_domains, attempt_ip: nil)
  75. Matcher.new(domain_or_domains, attempt_ip: attempt_ip).match?(allow_with_approval: true)
  76. end
  77. end