Commit 098440b9 authored by Jackie Fraser's avatar Jackie Fraser Committed by Gabriel Mazetto

Improve User username error for namespace conflict

parent 86a789cd
...@@ -1377,7 +1377,14 @@ class User < ApplicationRecord ...@@ -1377,7 +1377,14 @@ class User < ApplicationRecord
def set_username_errors def set_username_errors
namespace_path_errors = self.errors.delete(:"namespace.path") namespace_path_errors = self.errors.delete(:"namespace.path")
self.errors[:username].concat(namespace_path_errors) if namespace_path_errors
return unless namespace_path_errors&.any?
if namespace_path_errors.include?('has already been taken') && !User.exists?(username: username)
self.errors.add(:base, :username_exists_as_a_different_namespace)
else
self.errors[:username].concat(namespace_path_errors)
end
end end
def username_changed_hook def username_changed_hook
......
---
title: Improve error message when username and namespace conflict
merge_request: 47537
author:
type: changed
...@@ -169,6 +169,7 @@ en: ...@@ -169,6 +169,7 @@ en:
format: "%{attribute} %{message}" format: "%{attribute} %{message}"
messages: messages:
label_already_exists_at_group_level: "already exists at group level for %{group}. Please choose another one." label_already_exists_at_group_level: "already exists at group level for %{group}. Please choose another one."
username_exists_as_a_different_namespace: A user, alias, or group already exists with that username.
wrong_size: "is the wrong size (should be %{file_size})" wrong_size: "is the wrong size (should be %{file_size})"
size_too_small: "is too small (should be at least %{file_size})" size_too_small: "is too small (should be at least %{file_size})"
size_too_big: "is too big (should be at most %{file_size})" size_too_big: "is too big (should be at most %{file_size})"
......
...@@ -4280,7 +4280,7 @@ RSpec.describe User do ...@@ -4280,7 +4280,7 @@ RSpec.describe User do
it 'adds the namespace errors to the user' do it 'adds the namespace errors to the user' do
user.update(username: new_username) user.update(username: new_username)
expect(user.errors.full_messages.first).to eq('Username has already been taken') expect(user.errors.full_messages.first).to eq('A user, alias, or group already exists with that username.')
end end
end end
end end
......
...@@ -31,7 +31,7 @@ RSpec.describe Users::UpdateService do ...@@ -31,7 +31,7 @@ RSpec.describe Users::UpdateService do
result = update_user(user, { username: 'taken' }) result = update_user(user, { username: 'taken' })
end.not_to change { user.reload.username } end.not_to change { user.reload.username }
expect(result[:status]).to eq(:error) expect(result[:status]).to eq(:error)
expect(result[:message]).to eq('Username has already been taken') expect(result[:message]).to eq('A user, alias, or group already exists with that username.')
end end
it 'updates the status if status params were given' do it 'updates the status if status params were given' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment