FIX: Updating a user profile as admin shouldn't change the user's locale

FIX: Updating a user profile as admin shouldn’t change the user’s locale

diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 5a63496..76ed63f 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -352,6 +352,8 @@ class UsersController < ApplicationController
       return fail_with("login.reserved_username")
     end
 
+    params[:locale] ||= I18n.locale unless current_user
+
     new_user_params = user_params
     user = User.unstage(new_user_params)
     user = User.new(new_user_params) if user.nil?
@@ -1259,8 +1261,7 @@ class UsersController < ApplicationController
       .permit(permitted, theme_ids: [])
       .reverse_merge(
         ip_address: request.remote_ip,
-        registration_ip_address: request.remote_ip,
-        locale: user_locale
+        registration_ip_address: request.remote_ip
       )
 
     if !UsernameCheckerService.is_developer?(result['email']) &&
@@ -1279,10 +1280,6 @@ class UsersController < ApplicationController
     attrs
   end
 
-  def user_locale
-    I18n.locale
-  end
-
   def fail_with(key)
     render json: { success: false, message: I18n.t(key) }
   end
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index f8e45d7..542b81b 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -738,6 +738,18 @@ describe UsersController do
           expect(response.status).to eq(200)
           expect(JSON.parse(response.body)['active']).to be_falsy
         end
+
+        it "won't set the new user's locale to the admin's locale" do
+          SiteSetting.allow_user_locale = true
+          admin.update!(locale: :fr)
+
+          post "/u.json", params: post_user_params.merge(active: true, api_key: api_key.key)
+          expect(response.status).to eq(200)
+
+          json = JSON.parse(response.body)
+          new_user = User.find(json["user_id"])
+          expect(new_user.locale).not_to eq("fr")
+        end
       end
     end

GitHub sha: bae7b75e

2 Likes

:+1: