Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

auth: Identify pending invitations by their Confirmation type.

Mateusz Manderacb9d66082df2
diff --git a/zerver/models/prereg_users.py b/zerver/models/prereg_users.py
index d859aef2cc8..d5b0a607377 100644
--- a/zerver/models/prereg_users.py
+++ b/zerver/models/prereg_users.py
@@ -138,18 +138,27 @@ class PreregistrationUser(models.Model):
def filter_to_valid_prereg_users(
query: QuerySet[PreregistrationUser],
+ invitations_only: bool = False,
) -> QuerySet[PreregistrationUser]:
"""
- If invite_expires_in_days is specified, we return only those PreregistrationUser
- objects that were created at most that many days in the past.
+ Filters out PreregistrationUser objects that have been used or
+ revoked, or whose confirmation link has expired.
"""
used_value = confirmation_settings.STATUS_USED
revoked_value = confirmation_settings.STATUS_REVOKED
query = query.exclude(status__in=[used_value, revoked_value])
- return query.filter(
- Q(confirmation__expiry_date=None) | Q(confirmation__expiry_date__gte=timezone_now())
+
+ confirmation_q = Q(confirmation__expiry_date=None) | Q(
+ confirmation__expiry_date__gte=timezone_now()
)
+ if invitations_only:
+ # Imported here to avoid a circular import.
+ from confirmation.models import Confirmation
+
+ confirmation_q &= Q(confirmation__type=Confirmation.INVITATION)
+
+ return query.filter(confirmation_q)
class MultiuseInvite(models.Model):
diff --git a/zerver/views/auth.py b/zerver/views/auth.py
index 403c568d1de..18457a96c19 100644
--- a/zerver/views/auth.py
+++ b/zerver/views/auth.py
@@ -280,15 +280,22 @@ def maybe_send_to_registration(
# intended to have, when they were invited.
invited_as = existing_prereg_user.invited_as
- # A valid email invitation for this user permits them to sign up
- # even if the organization requires invitations to join. Only
- # invitations have referred_by set; PreregistrationUser rows
- # created by the user's own earlier signup attempts do not, and
- # must not bypass the invite_required check.
+ # A valid pending email invitation for this user permits them to
+ # sign up even if the organization requires invitations to join;
+ # PreregistrationUser rows left over from the user's own earlier
+ # signup attempts are not invitations, and must not bypass the
+ # invite_required check.
has_pending_invitation = filter_to_valid_prereg_users(
PreregistrationUser.objects.filter(
- email__iexact=email, realm=realm, referred_by__isnull=False
- )
+ email__iexact=email,
+ realm=realm,
+ # Only invitations have referred_by set. This condition is
+ # redundant with invitations_only below; we keep both as
+ # defense in depth to ensure we only look at the intended
+ # PreregistrationUser set.
+ referred_by__isnull=False,
+ ),
+ invitations_only=True,
).exists()
form = HomepageForm(