From 957034f00779d0fb20f47b69d09998ddbf685a0e Mon Sep 17 00:00:00 2001 From: James Mead Date: Fri, 10 Nov 2023 13:19:26 +0000 Subject: [PATCH] Redirect to account page if editing own user name c.f. `redirect_to_account_page_if_acting_on_own_user` before action in `UsersController`. In this case it makes sense to redirect to the specific account page for editing name. I missed this in #2497. --- app/controllers/users/names_controller.rb | 5 +++++ test/controllers/users/names_controller_test.rb | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/controllers/users/names_controller.rb b/app/controllers/users/names_controller.rb index aebc60795..15e935cfd 100644 --- a/app/controllers/users/names_controller.rb +++ b/app/controllers/users/names_controller.rb @@ -4,6 +4,7 @@ class Users::NamesController < ApplicationController before_action :authenticate_user! before_action :load_user before_action :authorize_user + before_action :redirect_to_account_page_if_acting_on_own_user, only: %i[edit] def edit; end @@ -29,4 +30,8 @@ def authorize_user def user_params params.require(:user).permit(*current_user.permitted_params.intersection([:name])) end + + def redirect_to_account_page_if_acting_on_own_user + redirect_to edit_account_email_path if current_user == @user + end end diff --git a/test/controllers/users/names_controller_test.rb b/test/controllers/users/names_controller_test.rb index 5a0cedbeb..1c98af675 100644 --- a/test/controllers/users/names_controller_test.rb +++ b/test/controllers/users/names_controller_test.rb @@ -4,7 +4,8 @@ class Users::NamesControllerTest < ActionController::TestCase context "GET edit" do context "signed in as Admin user" do setup do - sign_in(create(:admin_user)) + @admin = create(:admin_user) + sign_in(@admin) end should "display form with name field" do @@ -39,6 +40,12 @@ class Users::NamesControllerTest < ActionController::TestCase assert_not_authorised end + + should "redirect to account edit email page if admin is acting on their own user" do + get :edit, params: { user_id: @admin } + + assert_redirected_to edit_account_email_path + end end context "signed in as Normal user" do