Browse Source

Change e-mail whitelist/blacklist to not be checked when invited (#10683)

* Change e-mail whitelist/blacklist to not be checked when invited

And only when creating an account, not when updating it later

Fix #10648

* Fix test
Eugen Rochko 3 weeks ago
parent
commit
7cb369d4c6
No account linked to committer's email address

+ 1
- 1
app/models/user.rb View File

@@ -78,7 +78,7 @@ class User < ApplicationRecord
78 78
   accepts_nested_attributes_for :invite_request, reject_if: ->(attributes) { attributes['text'].blank? }
79 79
 
80 80
   validates :locale, inclusion: I18n.available_locales.map(&:to_s), if: :locale?
81
-  validates_with BlacklistedEmailValidator, if: :email_changed?
81
+  validates_with BlacklistedEmailValidator, on: :create
82 82
   validates_with EmailMxValidator, if: :validate_email_dns?
83 83
   validates :agreement, acceptance: { allow_nil: false, accept: [true, 'true', '1'] }, on: :create
84 84
 

+ 4
- 1
app/validators/blacklisted_email_validator.rb View File

@@ -2,7 +2,10 @@
2 2
 
3 3
 class BlacklistedEmailValidator < ActiveModel::Validator
4 4
   def validate(user)
5
+    return if user.invited?
6
+
5 7
     @email = user.email
8
+
6 9
     user.errors.add(:email, I18n.t('users.invalid_email')) if blocked_email?
7 10
   end
8 11
 
@@ -13,7 +16,7 @@ class BlacklistedEmailValidator < ActiveModel::Validator
13 16
   end
14 17
 
15 18
   def on_blacklist?
16
-    return true if EmailDomainBlock.block?(@email)
19
+    return true  if EmailDomainBlock.block?(@email)
17 20
     return false if Rails.configuration.x.email_domains_blacklist.blank?
18 21
 
19 22
     domains = Rails.configuration.x.email_domains_blacklist.gsub('.', '\.')

+ 1
- 0
spec/validators/blacklisted_email_validator_spec.rb View File

@@ -8,6 +8,7 @@ RSpec.describe BlacklistedEmailValidator, type: :validator do
8 8
     let(:errors) { double(add: nil) }
9 9
 
10 10
     before do
11
+      allow(user).to receive(:invited?) { false }
11 12
       allow_any_instance_of(described_class).to receive(:blocked_email?) { blocked_email }
12 13
       described_class.new.validate(user)
13 14
     end

Loading…
Cancel
Save