2017-10-04 09:16:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: email_domain_blocks
|
|
|
|
#
|
2022-04-29 17:27:03 -04:00
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# domain :string default(""), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# parent_id :bigint(8)
|
2017-10-04 09:16:10 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
class EmailDomainBlock < ApplicationRecord
|
2022-04-29 17:27:03 -04:00
|
|
|
self.ignored_columns = %w(
|
|
|
|
ips
|
|
|
|
last_refresh_at
|
|
|
|
)
|
|
|
|
|
2018-12-26 00:38:42 -05:00
|
|
|
include DomainNormalizable
|
2017-11-14 14:37:17 -05:00
|
|
|
|
2020-03-12 17:35:20 -04:00
|
|
|
belongs_to :parent, class_name: 'EmailDomainBlock', optional: true
|
|
|
|
has_many :children, class_name: 'EmailDomainBlock', foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
|
|
|
|
|
2019-08-08 17:04:19 -04:00
|
|
|
validates :domain, presence: true, uniqueness: true, domain: true
|
2017-11-14 14:37:17 -05:00
|
|
|
|
2022-02-24 11:28:23 -05:00
|
|
|
# Used for adding multiple blocks at once
|
|
|
|
attr_accessor :other_domains
|
2020-03-12 17:35:20 -04:00
|
|
|
|
2022-02-24 11:28:23 -05:00
|
|
|
def history
|
|
|
|
@history ||= Trends::History.new('email_domain_blocks', id)
|
2020-03-12 17:35:20 -04:00
|
|
|
end
|
|
|
|
|
2022-04-29 17:27:03 -04:00
|
|
|
def self.block?(domain_or_domains, attempt_ip: nil)
|
2022-02-24 11:28:23 -05:00
|
|
|
domains = Array(domain_or_domains).map do |str|
|
|
|
|
domain = begin
|
|
|
|
if str.include?('@')
|
|
|
|
str.split('@', 2).last
|
|
|
|
else
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
TagManager.instance.normalize_domain(domain) if domain.present?
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
nil
|
|
|
|
end
|
2020-03-12 17:35:20 -04:00
|
|
|
|
2022-02-24 11:28:23 -05:00
|
|
|
# If some of the inputs passed in are invalid, we definitely want to
|
|
|
|
# block the attempt, but we also want to register hits against any
|
|
|
|
# other valid matches
|
2017-11-14 14:37:17 -05:00
|
|
|
|
2022-02-24 11:28:23 -05:00
|
|
|
blocked = domains.any?(&:nil?)
|
2017-11-14 14:37:17 -05:00
|
|
|
|
2022-04-29 17:27:03 -04:00
|
|
|
where(domain: domains).find_each do |block|
|
2022-02-24 11:28:23 -05:00
|
|
|
blocked = true
|
|
|
|
block.history.add(attempt_ip) if attempt_ip.present?
|
2017-11-14 14:37:17 -05:00
|
|
|
end
|
|
|
|
|
2022-02-24 11:28:23 -05:00
|
|
|
blocked
|
2017-10-04 09:16:10 -04:00
|
|
|
end
|
|
|
|
end
|