A platform for community discussion. Free, open, simple.
FIX: allow users with normalized emails to change emails and remove + / . (#37133)
Sam9aba90b9a9c4
diff --git a/app/models/user_email.rb b/app/models/user_email.rbindex dcc6907f5f7..c19eda91bb2 100644--- a/app/models/user_email.rb+++ b/app/models/user_email.rb@@ -75,14 +75,18 @@ class UserEmail < ActiveRecord::Baseenddef unique_email+ scope = self.class+ scope = scope.where.not(id: id) if persisted?+email_exists =if self.normalize_emails?- self- .class- .where("lower(email) = ? OR lower(normalized_email) = ?", email, normalized_email)- .exists?+ scope.where(+ "lower(email) = ? OR lower(normalized_email) = ?",+ email,+ normalized_email,+ ).exists?else- self.class.where("lower(email) = ?", email).exists?+ scope.where("lower(email) = ?", email).exists?endself.errors.add(:email, :taken) if email_existsdiff --git a/spec/models/user_email_spec.rb b/spec/models/user_email_spec.rbindex 44a4d379825..7224943d5c7 100644--- a/spec/models/user_email_spec.rb+++ b/spec/models/user_email_spec.rb@@ -48,6 +48,30 @@ RSpec.describe UserEmail doexpect(user_email.normalized_email).to eq("[email protected]")expect(user_email).to be_validend++ it "allows updating an email to a variant with the same normalized value" do+ SiteSetting.normalize_emails = true++ user_email = user.user_emails.create!(email: "[email protected]", primary: false)+ expect(user_email.normalized_email).to eq("[email protected]")++ user_email.email = "[email protected]"+ expect(user_email).to be_valid+ expect(user_email.save).to eq(true)+ expect(user_email.reload.email).to eq("[email protected]")+ end++ it "blocks updating to an email that normalizes to another user's email" do+ SiteSetting.normalize_emails = true++ other_user = Fabricate(:user)+ other_user.user_emails.create!(email: "[email protected]", primary: false)++ user_email = user.user_emails.create!(email: "[email protected]", primary: false)+ user_email.email = "[email protected]"+ expect(user_email).not_to be_valid+ expect(user_email.errors[:email]).to include(I18n.t("errors.messages.taken"))+ endenddescribe "Indexes" do