mirror of
https://github.com/discourse/discourse.git
synced 2024-11-28 05:23:43 +08:00
b2a0d34bb7
* FEATURE: add setting `auto_approve_email_domains` to auto approve users This commit adds a new site setting `auto_approve_email_domains` to auto approve users based on their email address domain. Note that if a domain already exists in `email_domains_whitelist` then `auto_approve_email_domains` needs to be duplicated there as well, since users won’t be able to register with email address that is not allowed in `email_domains_whitelist`. * Update config/locales/server.en.yml Co-Authored-By: Robin Ward <robin.ward@gmail.com>
48 lines
1.5 KiB
Ruby
48 lines
1.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class EmailValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
|
unless EmailValidator.allowed?(value)
|
|
record.errors.add(attribute, I18n.t(:'user.email.not_allowed'))
|
|
end
|
|
|
|
if record.errors[attribute].blank? && value && ScreenedEmail.should_block?(value)
|
|
record.errors.add(attribute, I18n.t(:'user.email.blocked'))
|
|
end
|
|
end
|
|
|
|
def self.allowed?(email)
|
|
if (setting = SiteSetting.email_domains_whitelist).present?
|
|
return email_in_restriction_setting?(setting, email) || is_developer?(email)
|
|
elsif (setting = SiteSetting.email_domains_blacklist).present?
|
|
return !(email_in_restriction_setting?(setting, email) && !is_developer?(email))
|
|
end
|
|
|
|
true
|
|
end
|
|
|
|
def self.can_auto_approve_user?(email)
|
|
if (setting = SiteSetting.auto_approve_email_domains).present?
|
|
return !!(EmailValidator.allowed?(email) && email_in_restriction_setting?(setting, email))
|
|
end
|
|
|
|
false
|
|
end
|
|
|
|
def self.email_in_restriction_setting?(setting, value)
|
|
domains = setting.gsub('.', '\.')
|
|
regexp = Regexp.new("@(.+\\.)?(#{domains})$", true)
|
|
value =~ regexp
|
|
end
|
|
|
|
def self.is_developer?(value)
|
|
Rails.configuration.respond_to?(:developer_emails) && Rails.configuration.developer_emails.include?(value)
|
|
end
|
|
|
|
def self.email_regex
|
|
/\A[a-zA-Z0-9!#\$%&'*+\/=?\^_`{|}~\-]+(?:\.[a-zA-Z0-9!#\$%&'\*+\/=?\^_`{|}~\-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$\z/
|
|
end
|
|
|
|
end
|