Change e-mail domain blocks to block IPs dynamically (#17635)
* Change e-mail domain blocks to block IPs dynamically * Update app/workers/scheduler/email_domain_block_refresh_scheduler.rb Co-authored-by: Yamagishi Kazutoshi <ykzts@desire.sh> * Update app/workers/scheduler/email_domain_block_refresh_scheduler.rb Co-authored-by: Yamagishi Kazutoshi <ykzts@desire.sh> Co-authored-by: Yamagishi Kazutoshi <ykzts@desire.sh>
This commit is contained in:
parent
91cc8d1e63
commit
a29a982eaa
20 changed files with 322 additions and 157 deletions
|
@ -3,11 +3,13 @@
|
|||
#
|
||||
# Table name: email_domain_blocks
|
||||
#
|
||||
# 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)
|
||||
# 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)
|
||||
# ips :inet is an Array
|
||||
# last_refresh_at :datetime
|
||||
#
|
||||
|
||||
class EmailDomainBlock < ApplicationRecord
|
||||
|
@ -18,27 +20,42 @@ class EmailDomainBlock < ApplicationRecord
|
|||
|
||||
validates :domain, presence: true, uniqueness: true, domain: true
|
||||
|
||||
def with_dns_records=(val)
|
||||
@with_dns_records = ActiveModel::Type::Boolean.new.cast(val)
|
||||
# Used for adding multiple blocks at once
|
||||
attr_accessor :other_domains
|
||||
|
||||
def history
|
||||
@history ||= Trends::History.new('email_domain_blocks', id)
|
||||
end
|
||||
|
||||
def with_dns_records?
|
||||
@with_dns_records
|
||||
end
|
||||
def self.block?(domain_or_domains, ips: [], attempt_ip: nil)
|
||||
domains = Array(domain_or_domains).map do |str|
|
||||
domain = begin
|
||||
if str.include?('@')
|
||||
str.split('@', 2).last
|
||||
else
|
||||
str
|
||||
end
|
||||
end
|
||||
|
||||
alias with_dns_records with_dns_records?
|
||||
|
||||
def self.block?(email)
|
||||
_, domain = email.split('@', 2)
|
||||
|
||||
return true if domain.nil?
|
||||
|
||||
begin
|
||||
domain = TagManager.instance.normalize_domain(domain)
|
||||
TagManager.instance.normalize_domain(domain) if domain.present?
|
||||
rescue Addressable::URI::InvalidURIError
|
||||
return true
|
||||
nil
|
||||
end
|
||||
|
||||
where(domain: domain).exists?
|
||||
# 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
|
||||
|
||||
blocked = domains.any?(&:nil?)
|
||||
|
||||
scope = where(domain: domains)
|
||||
scope = scope.or(where('ips && ARRAY[?]::inet[]', ips)) if ips.any?
|
||||
|
||||
scope.find_each do |block|
|
||||
blocked = true
|
||||
block.history.add(attempt_ip) if attempt_ip.present?
|
||||
end
|
||||
|
||||
blocked
|
||||
end
|
||||
end
|
||||
|
|
30
app/models/form/email_domain_block_batch.rb
Normal file
30
app/models/form/email_domain_block_batch.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Form::EmailDomainBlockBatch
|
||||
include ActiveModel::Model
|
||||
include Authorization
|
||||
include AccountableConcern
|
||||
|
||||
attr_accessor :email_domain_block_ids, :action, :current_account
|
||||
|
||||
def save
|
||||
case action
|
||||
when 'delete'
|
||||
delete!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def email_domain_blocks
|
||||
@email_domain_blocks ||= EmailDomainBlock.where(id: email_domain_block_ids)
|
||||
end
|
||||
|
||||
def delete!
|
||||
email_domain_blocks.each do |email_domain_block|
|
||||
authorize(email_domain_block, :destroy?)
|
||||
email_domain_block.destroy!
|
||||
log_action :destroy, email_domain_block
|
||||
end
|
||||
end
|
||||
end
|
|
@ -24,6 +24,7 @@
|
|||
# poll_id :bigint(8)
|
||||
# deleted_at :datetime
|
||||
# edited_at :datetime
|
||||
# trendable :boolean
|
||||
#
|
||||
|
||||
class Status < ApplicationRecord
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue