Add uniqueness to block email domains (#5692)
This commit is contained in:
parent
8087aa83d4
commit
20150659e6
4 changed files with 35 additions and 6 deletions
|
@ -4,14 +4,33 @@
|
|||
# Table name: email_domain_blocks
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# domain :string not null
|
||||
# domain :string default(""), not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
|
||||
class EmailDomainBlock < ApplicationRecord
|
||||
before_validation :normalize_domain
|
||||
|
||||
validates :domain, presence: true, uniqueness: true
|
||||
|
||||
def self.block?(email)
|
||||
domain = email.gsub(/.+@([^.]+)/, '\1')
|
||||
_, domain = email.split('@', 2)
|
||||
|
||||
return true if domain.nil?
|
||||
|
||||
begin
|
||||
domain = TagManager.instance.normalize_domain(domain)
|
||||
rescue Addressable::URI::InvalidURIError
|
||||
return true
|
||||
end
|
||||
|
||||
where(domain: domain).exists?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def normalize_domain
|
||||
self.domain = TagManager.instance.normalize_domain(domain)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue