Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
signup: Preserve spectator view after social signup.
Shubham Padia068e548a63d4
diff --git a/templates/confirmation/confirm_preregistrationuser.html b/templates/confirmation/confirm_preregistrationuser.htmlindex f4b16a7957f..27a8819c115 100644--- a/templates/confirmation/confirm_preregistrationuser.html+++ b/templates/confirmation/confirm_preregistrationuser.html@@ -18,6 +18,9 @@ post to another view which executes in our code to produce the desired form.<input type="hidden" value="{{ key }}" name="key"/><input type="hidden" value="1" name="from_confirmation"/><input type="hidden" value="{% if full_name %}{{ full_name }}{% endif %}" name="full_name"/>+ {% if next %}+ <input type="hidden" value="{{ next }}" name="next"/>+ {% endif %}</form>{% endblock %}diff --git a/templates/zerver/create_user/accounts_home.html b/templates/zerver/create_user/accounts_home.htmlindex 30c39357f4a..5cdcd29843d 100644--- a/templates/zerver/create_user/accounts_home.html+++ b/templates/zerver/create_user/accounts_home.html@@ -80,6 +80,9 @@ page can be easily identified in it's respective JavaScript file -->{% for backend in external_authentication_methods %}<div class="login-social"><form class="form-inline" action="{{ backend.signup_url }}" method="get">+ {% if next %}+ <input type="hidden" name="next" value="{{ next }}" />+ {% endif %}<input type='hidden' name='multiuse_object_key' value='{{ multiuse_object_key }}' /><button id="register_auth_button_{{ backend.name }}" class="login-social-button full-width"{% if backend.display_icon %} style="background-image:url({{ backend.display_icon }})" {% endif %}>diff --git a/templates/zerver/login.html b/templates/zerver/login.htmlindex 4667fc52e4e..a8ec5ea3dc4 100644--- a/templates/zerver/login.html+++ b/templates/zerver/login.html@@ -155,7 +155,7 @@ page can be easily identified in it's respective JavaScript file. --><div class="actions signup-link-wrapper">{{ _("Don't have an account?")}}{% if not register_link_disabled %}- <a class="register-link float-right" href="/register/">{{ _('Sign up') }}</a>+ <a class="register-link float-right" href="/register/{% if next %}?next={{ next|urlencode }}{% endif %}">{{ _('Sign up') }}</a>{% endif %}</div>{% endif %}diff --git a/web/src/onboarding_steps.ts b/web/src/onboarding_steps.tsindex 041a8a0604d..a0163dec086 100644--- a/web/src/onboarding_steps.ts+++ b/web/src/onboarding_steps.ts@@ -4,6 +4,7 @@ import type * as z from "zod/mini";import render_navigation_tour_video_modal from "../templates/navigation_tour_video_modal.hbs";+import * as browser_history from "./browser_history.ts";import * as channel from "./channel.ts";import * as compose_recipient from "./compose_recipient.ts";import * as dialog_widget from "./dialog_widget.ts";@@ -80,6 +81,17 @@ function narrow_to_dm_with_welcome_bot_new_user((onboarding_step) => onboarding_step.name === "narrow_to_dm_with_welcome_bot_new_user",)) {+ post_onboarding_step_as_read("narrow_to_dm_with_welcome_bot_new_user");++ if (!browser_history.is_current_hash_home_view()) {+ // If this account was created with a `next` parameter to+ // take the user to a specific Zulip view, that takes+ // precedence over sending the user to the welcome bot DM+ // view. The user will hopefully still make their way+ // there, as it is still an unread DM conversation.+ return;+ }+show_message_view([{@@ -89,7 +101,6 @@ function narrow_to_dm_with_welcome_bot_new_user(],{trigger: "sidebar"},);- post_onboarding_step_as_read("narrow_to_dm_with_welcome_bot_new_user");}}diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.pyindex b361ef074c1..0fe33389c08 100644--- a/zerver/lib/test_classes.py+++ b/zerver/lib/test_classes.py@@ -900,6 +900,7 @@ Output:enable_marketing_emails: bool | None = None,email_address_visibility: int | None = None,is_demo_organization: bool = False,+ next: str = "",**extra: str,) -> "TestHttpResponse":"""@@ -936,6 +937,8 @@ Output:payload["password"] = passwordif realm_in_root_domain is not None:payload["realm_in_root_domain"] = realm_in_root_domain+ if next:+ payload["next"] = nextreturn self.client_post("/accounts/register/",payload,diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.pyindex 1f474eb6286..6bb1d996134 100644--- a/zerver/tests/test_auth_backends.py+++ b/zerver/tests/test_auth_backends.py@@ -8318,16 +8318,22 @@ class TestMaybeSendToRegistration(ZulipTestCase):def is_valid(self) -> bool:return True+ redirect_to = "/#narrow/channel/7-test-here"with mock.patch("zerver.views.auth.HomepageForm", return_value=Form()):self.assertEqual(PreregistrationUser.objects.all().count(), 0)result = maybe_send_to_registration(- request, self.example_email("hamlet"), is_signup=True+ request,+ self.example_email("hamlet"),+ is_signup=True,+ redirect_to=redirect_to,)self.assertEqual(result.status_code, 302)confirmation = Confirmation.objects.all().first()assert confirmation is not Noneconfirmation_key = confirmation.confirmation_keyself.assertIn("do_confirm/" + confirmation_key, result["Location"])+ parsed = urlsplit(result["Location"])+ self.assertEqual(parse_qs(parsed.query).get("next"), [redirect_to])self.assertEqual(PreregistrationUser.objects.all().count(), 1)response = self.client_get(result["Location"])diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.pyindex 43f797b533e..89848ae6a7e 100644--- a/zerver/tests/test_signup.py+++ b/zerver/tests/test_signup.py@@ -16,7 +16,7 @@ from django.template.response import TemplateResponsefrom django.test import Client, override_settingsfrom django.utils import translation-from confirmation.models import Confirmation, one_click_unsubscribe_link+from confirmation.models import Confirmation, create_confirmation_link, one_click_unsubscribe_linkfrom zerver.actions.create_realm import do_change_realm_subdomain, do_create_realmfrom zerver.actions.create_user import add_new_user_history, do_create_userfrom zerver.actions.default_streams import do_add_default_stream, do_create_default_stream_group@@ -84,7 +84,11 @@ from zerver.models.realms import get_realmfrom zerver.models.recipients import get_direct_message_group_user_idsfrom zerver.models.streams import get_streamfrom zerver.models.users import get_system_bot, get_user, get_user_by_delivery_email-from zerver.views.auth import redirect_and_log_into_subdomain, start_two_factor_auth+from zerver.views.auth import (+ create_preregistration_user,+ redirect_and_log_into_subdomain,+ start_two_factor_auth,+)from zerver.views.development.registration import confirmation_keyfrom zproject.backends import ExternalAuthDataDict, ExternalAuthResult, email_auth_enabled@@ -1807,6 +1811,39 @@ class UserSignUpTest(ZulipTestCase):result = self.verify_signup(email=email, password=password)assert isinstance(result, UserProfile)+ def test_signup_redirects_to_next(self) -> None:+ email = "[email protected]"+ next_url = "/#narrow/channel/7-test-here"+ realm = get_realm("zulip")++ prereg_user = create_preregistration_user(+ email, realm, password_required=False, full_name="Next User"+ )+ confirmation_url = create_confirmation_link(prereg_user, Confirmation.USER_REGISTRATION)+ result = self.client_get(confirmation_url)+ self.assertEqual(result.status_code, 200)++ confirmation_key = confirmation_url.split("/")[-1]+ result = self.client_post(+ "/accounts/register/",+ {+ "key": confirmation_key,+ "from_confirmation": "1",+ "full_name": "Next User",+ "next": next_url,+ },+ )+ self.assertEqual(result.status_code, 200)+ self.assert_in_success_response(+ [f'<input type="hidden" name="next" value="{next_url}" />'], result+ )++ result = self.submit_reg_form_for_user(+ email, None, full_name="Next User", next=next_url, key=confirmation_key+ )+ self.assertEqual(result.status_code, 302)+ self.assertEqual(result["Location"], realm.url + next_url)+def test_signup_with_email_address_race(self) -> None:"""The check for if an email is in use can race with other userdiff --git a/zerver/views/auth.py b/zerver/views/auth.pyindex a86df196056..d902bc31d49 100644--- a/zerver/views/auth.py+++ b/zerver/views/auth.py@@ -177,6 +177,7 @@ def maybe_send_to_registration(mobile_flow_otp: str | None = None,multiuse_object_key: str = "",params_to_store_in_authenticated_session: dict[str, str] | None = None,+ redirect_to: str | None = None,role: int | None = None,) -> HttpResponse:"""Given a successful authentication for an email address (i.e. we've@@ -337,6 +338,10 @@ def maybe_send_to_registration(prereg_user.save()confirmation_link = create_confirmation_link(prereg_user, Confirmation.USER_REGISTRATION)+ if redirect_to:+ confirmation_link = append_url_query_string(+ confirmation_link, urlencode({"next": redirect_to})+ )if is_signup:return redirect(confirmation_link)@@ -379,6 +384,7 @@ def register_remote_user(request: HttpRequest, result: ExternalAuthResult) -> Ht"multiuse_object_key","full_name_validated","params_to_store_in_authenticated_session",+ "redirect_to",]for key in dict(kwargs):if key not in kwargs_to_pass:diff --git a/zerver/views/registration.py b/zerver/views/registration.pyindex 5d865b18e9d..d1882a53dac 100644--- a/zerver/views/registration.py+++ b/zerver/views/registration.py@@ -157,7 +157,11 @@ logger = logging.getLogger("zulip.registration")@typed_endpointdef get_prereg_key_and_redirect(- request: HttpRequest, *, confirmation_key: PathOnly[str], full_name: str | None = None+ request: HttpRequest,+ *,+ confirmation_key: PathOnly[str],+ full_name: str | None = None,+ next: str = "",) -> HttpResponse:"""The purpose of this little endpoint is primarily to take a GET@@ -194,6 +198,7 @@ def get_prereg_key_and_redirect("key": confirmation_key,"full_name": full_name,"registration_url": registration_url,+ "next": next,},)@@ -284,6 +289,7 @@ def registration_helper(form_full_name: Annotated[str | None, ApiParamConfig("full_name")] = None,from_confirmation: str | None = None,key: str = "",+ next: str = "",slack_access_token: str | None = None,source_realm_id: Annotated[NonNegativeInt | None, non_negative_int_or_none_validator()] = None,start_slack_import: Json[bool] = False,@@ -784,7 +790,7 @@ def registration_helper(user_profile = userif not realm_creation:# Since we'll have created a user, we now just log them in.- return login_and_go_to_home(request, user_profile)+ return login_and_redirect(request, user_profile, next)# With realm_creation=True, we're going to return further down,# after finishing up the creation process.@@ -886,7 +892,7 @@ def registration_helper(return redirect("/")assert isinstance(auth_result, UserProfile)- return login_and_go_to_home(request, auth_result)+ return login_and_redirect(request, auth_result, next)default_email_address_visibility = Noneif realm is not None:@@ -922,6 +928,7 @@ def registration_helper("email_address_visibility_options_dict": UserProfile.EMAIL_ADDRESS_VISIBILITY_ID_TO_NAME_MAP,"how_realm_creator_found_zulip_options": RealmAuditLog.HOW_REALM_CREATOR_FOUND_ZULIP_OPTIONS.items(),}+ context["next"] = nextif realm_creation:# Add context for realm creation part of the form.@@ -930,7 +937,9 @@ def registration_helper(return TemplateResponse(request, "zerver/create_user/register.html", context=context)-def login_and_go_to_home(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:+def login_and_redirect(+ request: HttpRequest, user_profile: UserProfile, next: str = ""+) -> HttpResponse:mobile_flow_otp = get_expirable_session_var(request.session, "registration_mobile_flow_otp", delete=True)@@ -953,9 +962,14 @@ def login_and_go_to_home(request: HttpRequest, user_profile: UserProfile) -> Htt)do_login(request, user_profile)- # Using 'mark_sanitized' to work around false positive where Pysa thinks- # that 'user_profile' is user-controlled- return HttpResponseRedirect(mark_sanitized(user_profile.realm.url) + reverse("home"))+ if next:+ redirect_to = get_safe_redirect_to(next, user_profile.realm.url)+ else:+ # Using 'mark_sanitized' to work around false positive where Pysa thinks+ # that 'user_profile' is user-controlled+ redirect_to = mark_sanitized(user_profile.realm.url) + reverse("home")++ return HttpResponseRedirect(redirect_to)def prepare_activation_url(@@ -1764,6 +1778,7 @@ def accounts_home(current_url=request.get_full_path,multiuse_object_key=multiuse_object_key,from_multiuse_invite=from_multiuse_invite,+ next=request.GET.get("next", ""),)return render(request, "zerver/create_user/accounts_home.html", context=context)