Skip to content

Commit

Permalink
Redirect to account page if editing own user email
Browse files Browse the repository at this point in the history
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 email address.
  • Loading branch information
floehopper committed Nov 10, 2023
1 parent ee54552 commit 8cfa8f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/controllers/users/emails_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Users::EmailsController < 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

Expand Down Expand Up @@ -51,4 +52,8 @@ def user_params
def permitted_user_params
@permitted_user_params ||= params.require(:user).permit(:email).to_h
end

def redirect_to_account_page_if_acting_on_own_user
redirect_to edit_account_email_path if current_user == @user
end
end
9 changes: 8 additions & 1 deletion test/controllers/users/emails_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Users::EmailsControllerTest < 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 email field" do
Expand Down Expand Up @@ -53,6 +54,12 @@ class Users::EmailsControllerTest < 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
Expand Down

0 comments on commit 8cfa8f3

Please sign in to comment.