From e5e702a9767734c5ec5bde03e1f0afef008c4987 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 4 Dec 2016 19:07:02 +0100 Subject: [PATCH] Adding configurable e-mail blacklist --- app/lib/email_validator.rb | 18 ++++++++++++++++++ app/models/user.rb | 1 + config/initializers/blacklists.rb | 5 +++++ config/initializers/ostatus.rb | 4 +++- config/initializers/paperclip.rb | 2 ++ config/locales/en.yml | 2 ++ 6 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 app/lib/email_validator.rb create mode 100644 config/initializers/blacklists.rb diff --git a/app/lib/email_validator.rb b/app/lib/email_validator.rb new file mode 100644 index 0000000000..856b8b1f7b --- /dev/null +++ b/app/lib/email_validator.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class EmailValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + return if Rails.configuration.x.email_domains_blacklist.empty? + + record.errors.add(attribute, I18n.t('users.invalid_email')) if blocked_email?(value) + end + + private + + def blocked_email?(value) + domains = Rails.configuration.x.email_domains_blacklist.gsub('.', '\.') + regexp = Regexp.new("@(.+\\.)?(#{domains})", true) + + value =~ regexp + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 423833d471..3fc028a6a4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,6 +8,7 @@ class User < ApplicationRecord validates :account, presence: true validates :locale, inclusion: I18n.available_locales.map(&:to_s), unless: 'locale.nil?' + validates :email, email: true scope :prolific, -> { joins('inner join statuses on statuses.account_id = users.account_id').select('users.*, count(statuses.id) as statuses_count').group('users.id').order('statuses_count desc') } scope :recent, -> { order('id desc') } diff --git a/config/initializers/blacklists.rb b/config/initializers/blacklists.rb new file mode 100644 index 0000000000..52646e64d6 --- /dev/null +++ b/config/initializers/blacklists.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +Rails.application.configure do + config.x.email_domains_blacklist = ENV.fetch('EMAIL_DOMAIN_BLACKLIST') { 'mvrht.com' } +end diff --git a/config/initializers/ostatus.rb b/config/initializers/ostatus.rb index 4ba432b6a9..c5723b2e94 100644 --- a/config/initializers/ostatus.rb +++ b/config/initializers/ostatus.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + port = ENV.fetch('PORT') { 3000 } host = ENV.fetch('LOCAL_DOMAIN') { "localhost:#{port}" } https = ENV['LOCAL_HTTPS'] == 'true' - + Rails.application.configure do config.x.local_domain = host config.x.hub_url = ENV.fetch('HUB_URL') { 'https://pubsubhubbub.superfeedr.com' } diff --git a/config/initializers/paperclip.rb b/config/initializers/paperclip.rb index 61bb52480d..93822a2d19 100644 --- a/config/initializers/paperclip.rb +++ b/config/initializers/paperclip.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + if ENV['S3_ENABLED'] == 'true' Aws.eager_autoload!(services: %w(S3)) diff --git a/config/locales/en.yml b/config/locales/en.yml index 426f3928a1..50a1f0e955 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -55,5 +55,7 @@ en: stream_entries: favourited: favourited a post by is_now_following: is now following + users: + invalid_email: The e-mail address is invalid will_paginate: page_gap: "…"