mirror of https://github.com/mastodon/mastodon
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.3 KiB
Ruby
43 lines
1.3 KiB
Ruby
8 years ago
|
# frozen_string_literal: true
|
||
|
|
||
5 months ago
|
class UserEmailValidator < ActiveModel::Validator
|
||
8 years ago
|
def validate(user)
|
||
4 years ago
|
return if user.valid_invitation? || user.email.blank?
|
||
6 years ago
|
|
||
3 years ago
|
user.errors.add(:email, :blocked) if blocked_email_provider?(user.email, user.sign_up_ip)
|
||
|
user.errors.add(:email, :taken) if blocked_canonical_email?(user.email)
|
||
8 years ago
|
end
|
||
|
|
||
|
private
|
||
|
|
||
3 years ago
|
def blocked_email_provider?(email, ip)
|
||
|
disallowed_through_email_domain_block?(email, ip) || disallowed_through_configuration?(email) || not_allowed_through_configuration?(email)
|
||
8 years ago
|
end
|
||
|
|
||
3 years ago
|
def blocked_canonical_email?(email)
|
||
|
CanonicalEmailBlock.block?(email)
|
||
4 years ago
|
end
|
||
8 years ago
|
|
||
3 years ago
|
def disallowed_through_email_domain_block?(email, ip)
|
||
|
EmailDomainBlock.block?(email, attempt_ip: ip)
|
||
8 years ago
|
end
|
||
8 years ago
|
|
||
3 years ago
|
def not_allowed_through_configuration?(email)
|
||
5 months ago
|
return false if Rails.configuration.x.email_domains_allowlist.blank?
|
||
8 years ago
|
|
||
5 months ago
|
domains = Rails.configuration.x.email_domains_allowlist.gsub('.', '\.')
|
||
8 years ago
|
regexp = Regexp.new("@(.+\\.)?(#{domains})$", true)
|
||
8 years ago
|
|
||
3 years ago
|
email !~ regexp
|
||
8 years ago
|
end
|
||
4 years ago
|
|
||
3 years ago
|
def disallowed_through_configuration?(email)
|
||
5 months ago
|
return false if Rails.configuration.x.email_domains_denylist.blank?
|
||
4 years ago
|
|
||
5 months ago
|
domains = Rails.configuration.x.email_domains_denylist.gsub('.', '\.')
|
||
4 years ago
|
regexp = Regexp.new("@(.+\\.)?(#{domains})", true)
|
||
|
|
||
3 years ago
|
regexp.match?(email)
|
||
4 years ago
|
end
|
||
8 years ago
|
end
|