FEATURE: the hide_email_address_taken setting works with the change email address form in user preferences

This commit is contained in:
Neil Lalonde 2017-10-04 11:41:08 -04:00
parent abdb334823
commit c29334cf23
4 changed files with 61 additions and 16 deletions

View File

@ -2660,11 +2660,11 @@ en:
title: "Account already exists"
subject_template: "[%{email_prefix}] Account already exists"
text_body_template: |
You just tried to create an account at %{site_name}. However, an account already exists for %{email}.
You just tried to create an account at %{site_name}, or tried to change the email of an account to %{email}. However, an account already exists for %{email}.
If you forgot your password, [reset it now](%{base_url}/password-reset).
If you didnt try to create an account for %{email}, dont worry you can safely ignore this message.
If you didnt try to create an account for %{email} or change your email address, dont worry you can safely ignore this message.
If you have any questions, [contact our friendly staff](%{base_url}/about).

View File

@ -27,12 +27,16 @@ class EmailUpdater
EmailValidator.new(attributes: :email).validate_each(self, :email, email)
if existing_user = User.find_by_email(email)
error_message = 'change_email.error'
error_message << '_staged' if existing_user.staged?
errors.add(:base, I18n.t(error_message))
if SiteSetting.hide_email_address_taken
Jobs.enqueue(:critical_user_email, type: :account_exists, user_id: existing_user.id)
else
error_message = 'change_email.error'
error_message << '_staged' if existing_user.staged?
errors.add(:base, I18n.t(error_message))
end
end
if errors.blank?
if errors.blank? && existing_user.nil?
args = {
old_email: @user.email,
new_email: email,

View File

@ -131,4 +131,25 @@ describe EmailUpdater do
end
end
end
context 'hide_email_address_taken is enabled' do
before do
SiteSetting.hide_email_address_taken = true
end
let(:user) { Fabricate(:user, email: old_email) }
let(:existing) { Fabricate(:user, email: new_email) }
let(:updater) { EmailUpdater.new(user.guardian, user) }
it "doesn't error if user exists with new email" do
updater.change_to(existing.email)
expect(updater.errors).to be_blank
expect(user.email_change_requests).to be_empty
end
it 'sends an email to the owner of the account with the new email' do
Jobs.expects(:enqueue).once.with(:critical_user_email, has_entries(type: :account_exists, user_id: existing.id))
updater.change_to(existing.email)
end
end
end

View File

@ -96,20 +96,40 @@ describe UsersEmailController do
context 'when the new email address is taken' do
let!(:other_user) { Fabricate(:coding_horror) }
it 'raises an error' do
put "/u/#{user.username}/preferences/email.json", params: {
email: other_user.email
}
context 'hide_email_address_taken is disabled' do
before do
SiteSetting.hide_email_address_taken = false
end
expect(response).to_not be_success
it 'raises an error' do
put "/u/#{user.username}/preferences/email.json", params: {
email: other_user.email
}
expect(response).to_not be_success
end
it 'raises an error if there is whitespace too' do
put "/u/#{user.username}/preferences/email.json", params: {
email: "#{other_user.email} "
}
expect(response).to_not be_success
end
end
it 'raises an error if there is whitespace too' do
put "/u/#{user.username}/preferences/email.json", params: {
email: "#{other_user.email} "
}
context 'hide_email_address_taken is enabled' do
before do
SiteSetting.hide_email_address_taken = true
end
expect(response).to_not be_success
it 'responds with success' do
put "/u/#{user.username}/preferences/email.json", params: {
email: other_user.email
}
expect(response).to be_success
end
end
end