From cc39b6860b1abd128b641572bb5e79da6ce90ffb Mon Sep 17 00:00:00 2001 From: PieterCK Date: Mon, 24 Mar 2025 18:06:17 +0700 Subject: [PATCH] email_change: Use HTML error for user deactivated error. Currently when a deactivated user tries to access the change email link (generated when their account still active), a JSON error message will be shown. This adds a new portico error page for user deactivated errors. Now, `confirm_email_change` renders a portico error page when the user trying to change their email is deactivated. Fixes #20227. --- .../portico_error_pages/user_deactivated.html | 20 +++++++++++++++++++ zerver/tests/test_email_change.py | 2 ++ zerver/views/user_settings.py | 13 +++++++++--- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 templates/zerver/portico_error_pages/user_deactivated.html diff --git a/templates/zerver/portico_error_pages/user_deactivated.html b/templates/zerver/portico_error_pages/user_deactivated.html new file mode 100644 index 0000000000..c7bf1912f9 --- /dev/null +++ b/templates/zerver/portico_error_pages/user_deactivated.html @@ -0,0 +1,20 @@ +{% extends "zerver/portico_error_pages/portico_error_page.html" %} + +{% block title %} +{{ _("Account is deactivated") }} | Zulip +{% endblock %} + +{% block error_page_content %} + +
+
+

{{ _("Account is deactivated") }}

+

+ {% trans %} + Your Zulip account on {{ realm_url }} + has been deactivated, and you will no longer be able to log in. + {% endtrans %} +

+
+
+{% endblock %} diff --git a/zerver/tests/test_email_change.py b/zerver/tests/test_email_change.py index ba99fb3926..e589ba820b 100644 --- a/zerver/tests/test_email_change.py +++ b/zerver/tests/test_email_change.py @@ -155,6 +155,8 @@ class EmailChangeTestCase(ZulipTestCase): do_deactivate_user(user_profile, acting_user=None) response = self.client_get(activation_url) self.assertEqual(response.status_code, 401) + error_page_title = "Account is deactivated | Zulip" + self.assert_in_response(error_page_title, response) do_reactivate_user(user_profile, acting_user=None) self.login_user(user_profile) diff --git a/zerver/views/user_settings.py b/zerver/views/user_settings.py index 039d1866ab..481e8a6c4a 100644 --- a/zerver/views/user_settings.py +++ b/zerver/views/user_settings.py @@ -61,7 +61,6 @@ AVATAR_CHANGES_DISABLED_ERROR = gettext_lazy("Avatar changes are disabled in thi def validate_email_change_request(user_profile: UserProfile, new_email: str) -> None: if not user_profile.is_active: - # TODO: Make this into a user-facing error, not JSON raise UserDeactivatedError if user_profile.realm.email_changes_disabled and not user_profile.is_realm_admin: @@ -110,8 +109,16 @@ def confirm_email_change(request: HttpRequest, confirmation_key: str) -> HttpRes if user_profile.realm.deactivated: return redirect_to_deactivation_notice() - - validate_email_change_request(user_profile, new_email) + try: + validate_email_change_request(user_profile, new_email) + except UserDeactivatedError: + context = {"realm_url": user_profile.realm.url} + return render( + request, + "zerver/portico_error_pages/user_deactivated.html", + context=context, + status=401, + ) do_change_user_delivery_email(user_profile, new_email, acting_user=user_profile) user_profile = UserProfile.objects.get(id=email_change_object.user_profile_id)