A platform for community discussion. Free, open, simple.
FIX: Allow admins to use reserved usernames (#30262)
Martin Brennan7c96d7587e0d
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rbindex a6825127f5b..2c970da1dc4 100644--- a/app/controllers/users_controller.rb+++ b/app/controllers/users_controller.rb@@ -269,7 +269,7 @@ class UsersController < ApplicationControllerparams.require(:new_username)if clashing_with_existing_route?(params[:new_username]) ||- User.reserved_username?(params[:new_username])+ (User.reserved_username?(params[:new_username]) && !current_user.admin?)return render_json_error(I18n.t("login.reserved_username"))end@@ -614,7 +614,7 @@ class UsersController < ApplicationController# The special case where someone is changing the case of their own usernamereturn render_available_true if changing_case_of_own_username(target_user, username)- checker = UsernameCheckerService.new+ checker = UsernameCheckerService.new(allow_reserved_username: current_user&.admin?)email = params[:email] || target_user.try(:email)render json: checker.check_username(username, email)enddiff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rbindex 80e5afa3983..7425eaee0a7 100644--- a/spec/requests/users_controller_spec.rb+++ b/spec/requests/users_controller_spec.rb@@ -1846,7 +1846,7 @@ RSpec.describe UsersController doit "raises an error without a new_username param" doput "/u/#{user.username}/preferences/username.json", params: { username: user.username }- expect(response.status).to eq(400)+ expect(response).to be_bad_requestexpect(user.reload.username).to eq(old_username)end@@ -1862,7 +1862,7 @@ RSpec.describe UsersController doit "raises an error when change_username fails" doput "/u/#{user.username}/preferences/username.json", params: { new_username: "@" }- expect(response.status).to eq(422)+ expect(response).to be_unprocessablebody = response.parsed_body@@ -1876,7 +1876,7 @@ RSpec.describe UsersController doit "should succeed in normal circumstances" doput "/u/#{user.username}/preferences/username.json", params: { new_username: new_username }- expect(response.status).to eq(200)+ expect(response).to be_successfulexpect(user.reload.username).to eq(new_username)end@@ -1895,9 +1895,30 @@ RSpec.describe UsersController doSiteSetting.reserved_usernames = "reserved"put "/u/#{user.username}/preferences/username.json", params: { new_username: "reserved" }- body = response.parsed_body+ expect(response).to be_unprocessable- expect(body["errors"].first).to include(I18n.t("login.reserved_username"))+ expect(response.parsed_body["errors"].first).to include(I18n.t("login.reserved_username"))+ end++ it "allows admins to change a username to one in the reserved list" do+ sign_in(admin)+ SiteSetting.reserved_usernames = "reserved"++ put "/u/#{user.username}/preferences/username.json", params: { new_username: "reserved" }+ expect(response).to be_successful++ expect(response.parsed_body["username"]).to eq("reserved")+ end++ it "does not allow admins to change a username to one in the reserved list if that user already exists" do+ sign_in(admin)+ SiteSetting.reserved_usernames = "reserved"+ Fabricate(:user, username: "reserved")++ put "/u/#{user.username}/preferences/username.json", params: { new_username: "reserved" }+ expect(response).to be_unprocessable++ expect(response.parsed_body["errors"].first).to include("Username must be unique")endit "should fail if the user is old" do@@ -1921,7 +1942,7 @@ RSpec.describe UsersController doput "/u/#{user.username}/preferences/username.json", params: { new_username: new_username }- expect(response.status).to eq(200)+ expect(response).to be_successfulexpect(UserHistory.where(action: UserHistory.actions[:change_username],@@ -1947,7 +1968,7 @@ RSpec.describe UsersController doput "/u/#{user.username}/preferences/username.json", params: { new_username: new_username }- expect(response.status).to eq(422)+ expect(response).to be_unprocessableexpect(response.parsed_body["errors"].first).to include(I18n.t("errors.messages.auth_overrides_username"),)@@ -1991,6 +2012,30 @@ RSpec.describe UsersController doinclude_examples "when username is unavailable"end+ describe "reserved usernames" do+ before { SiteSetting.reserved_usernames = SiteSetting.reserved_usernames + "|reserved" }++ context "when checking a reserved username" do+ before { get "/u/check_username.json", params: { username: "reserved" } }+ include_examples "when username is unavailable"+ end++ context "when checking a reserved username as an admin" do+ before { sign_in(admin) }++ context "when user already exists" do+ fab!(:user) { Fabricate(:user, username: "reserved") }+ before { get "/u/check_username.json", params: { username: "reserved" } }+ include_examples "when username is unavailable"+ end++ context "when user does not exist" do+ before { get "/u/check_username.json", params: { username: "reserved" } }+ include_examples "when username is available"+ end+ end+ end+shared_examples "checking an invalid username" doit "should not return an available key but should return an error message" doexpect(response.status).to eq(200)