mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	signup: Create get_accounts_for_email function.
This commit is contained in:
		@@ -102,15 +102,15 @@ Form is validated both client-side using jquery-validate (see signup.js) and ser
 | 
			
		||||
                    <label for="id_email" class="inline-block label-title">{{ _('Email') }}</label>
 | 
			
		||||
                </div>
 | 
			
		||||
 | 
			
		||||
                {% if membership_realms %}
 | 
			
		||||
                {% if accounts %}
 | 
			
		||||
                <div class="input-box">
 | 
			
		||||
                    <label for="source_realm" class="inline-block">{{ _('Import settings from an existing Zulip account') }}</label>
 | 
			
		||||
                </div>
 | 
			
		||||
                <div id="source_realm_select_section" class="input-group m-10 inline-block">
 | 
			
		||||
                    <select class="select" name="source_realm" id="source_realm_select">
 | 
			
		||||
                        <option value="on" {% if ("source_realm" in form.data and form.data["source_realm"] == "on") or "source_realm" not in form.data %} selected {% endif %}>None</option>
 | 
			
		||||
                        {% for membership_realm in membership_realms %}
 | 
			
		||||
                        <option value="{{ membership_realm.string_id }}" {% if "source_realm" in form.data and membership_realm.string_id == form.data["source_realm"] %} selected {% endif %}>{{ membership_realm.name }}</option>
 | 
			
		||||
                        {% for account in accounts %}
 | 
			
		||||
                        <option value="{{ account.string_id }}" data-full-name="{{account.full_name}}" data-avatar="{{account.avatar}}" {% if "source_realm" in form.data and account.string_id == form.data["source_realm"] %} selected {% endif %}>{{ account.realm_name }}</option>
 | 
			
		||||
                        {% endfor %}
 | 
			
		||||
                    </select>
 | 
			
		||||
                </div>
 | 
			
		||||
 
 | 
			
		||||
@@ -2,10 +2,12 @@ from typing import Dict, List, Optional
 | 
			
		||||
 | 
			
		||||
from django.db.models.query import QuerySet
 | 
			
		||||
from django.utils.translation import ugettext as _
 | 
			
		||||
from django.conf import settings
 | 
			
		||||
 | 
			
		||||
from zerver.lib.cache import generic_bulk_cached_fetch, user_profile_cache_key_id, \
 | 
			
		||||
    user_profile_by_id_cache_key
 | 
			
		||||
from zerver.lib.request import JsonableError
 | 
			
		||||
from zerver.lib.avatar import avatar_url
 | 
			
		||||
from zerver.models import UserProfile, Service, Realm, \
 | 
			
		||||
    get_user_profile_by_id, query_for_ids, get_user_profile_by_id_in_realm
 | 
			
		||||
 | 
			
		||||
@@ -164,3 +166,16 @@ def access_user_by_id(user_profile: UserProfile, user_id: int,
 | 
			
		||||
    if not user_profile.can_admin_user(target):
 | 
			
		||||
        raise JsonableError(_("Insufficient permission"))
 | 
			
		||||
    return target
 | 
			
		||||
 | 
			
		||||
def get_accounts_for_email(email: str) -> List[Dict[str, Optional[str]]]:
 | 
			
		||||
    if settings.PRODUCTION:  # nocoverage
 | 
			
		||||
        return []
 | 
			
		||||
    profiles = UserProfile.objects.select_related('realm').filter(email__iexact=email.strip(),
 | 
			
		||||
                                                                  is_active=True,
 | 
			
		||||
                                                                  is_bot=False,
 | 
			
		||||
                                                                  realm__deactivated=False)
 | 
			
		||||
    return [{"realm_name": profile.realm.name,
 | 
			
		||||
             "string_id": profile.realm.string_id,
 | 
			
		||||
             "full_name": profile.full_name,
 | 
			
		||||
             "avatar": avatar_url(profile)}
 | 
			
		||||
            for profile in profiles]
 | 
			
		||||
 
 | 
			
		||||
@@ -1589,12 +1589,6 @@ def active_non_guest_user_ids(realm_id: int) -> List[int]:
 | 
			
		||||
    ).values_list('id', flat=True)
 | 
			
		||||
    return list(query)
 | 
			
		||||
 | 
			
		||||
def get_membership_realms(email: str) -> List[Realm]:
 | 
			
		||||
    if settings.PRODUCTION:  # nocoverage
 | 
			
		||||
        return []
 | 
			
		||||
    profiles = UserProfile.objects.select_related('realm').filter(email__iexact=email.strip())
 | 
			
		||||
    return [profile.realm for profile in profiles]
 | 
			
		||||
 | 
			
		||||
def get_source_profile(email: str, string_id: str) -> Optional[UserProfile]:
 | 
			
		||||
    realm = get_realm(string_id)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -20,8 +20,8 @@ from zerver.lib.test_runner import slow
 | 
			
		||||
from zerver.models import UserProfile, Recipient, \
 | 
			
		||||
    Realm, RealmDomain, UserActivity, UserHotspot, \
 | 
			
		||||
    get_user, get_realm, get_client, get_stream, get_stream_recipient, \
 | 
			
		||||
    get_membership_realms, get_source_profile, \
 | 
			
		||||
    Message, get_context_for_message, ScheduledEmail, check_valid_user_ids
 | 
			
		||||
    get_source_profile, Message, get_context_for_message, \
 | 
			
		||||
    ScheduledEmail, check_valid_user_ids
 | 
			
		||||
 | 
			
		||||
from zerver.lib.avatar import avatar_url
 | 
			
		||||
from zerver.lib.email_mirror import create_missed_message_address
 | 
			
		||||
@@ -38,7 +38,8 @@ from zerver.lib.actions import (
 | 
			
		||||
from zerver.lib.create_user import copy_user_settings
 | 
			
		||||
from zerver.lib.topic_mutes import add_topic_mute
 | 
			
		||||
from zerver.lib.stream_topic import StreamTopicTarget
 | 
			
		||||
from zerver.lib.users import user_ids_to_users, access_user_by_id
 | 
			
		||||
from zerver.lib.users import user_ids_to_users, access_user_by_id, \
 | 
			
		||||
    get_accounts_for_email
 | 
			
		||||
 | 
			
		||||
from django.conf import settings
 | 
			
		||||
 | 
			
		||||
@@ -396,26 +397,35 @@ class UserProfileTest(ZulipTestCase):
 | 
			
		||||
        self.assertEqual(result[cordelia].email, cordelia)
 | 
			
		||||
        self.assertEqual(result[webhook_bot].email, webhook_bot)
 | 
			
		||||
 | 
			
		||||
    def test_get_membership_realms(self) -> None:
 | 
			
		||||
        zulip_realm = get_realm("zulip")
 | 
			
		||||
    def test_get_accounts_for_email(self) -> None:
 | 
			
		||||
        def check_account_present_in_accounts(user: UserProfile, accounts: List[Dict[str, Optional[str]]]) -> None:
 | 
			
		||||
            for account in accounts:
 | 
			
		||||
                realm = user.realm
 | 
			
		||||
                if account["avatar"] == avatar_url(user) and account["full_name"] == user.full_name \
 | 
			
		||||
                        and account["realm_name"] == realm.name and account["string_id"] == realm.string_id:
 | 
			
		||||
                    return
 | 
			
		||||
            raise AssertionError("Account not found")
 | 
			
		||||
 | 
			
		||||
        lear_realm = get_realm("lear")
 | 
			
		||||
        cordelia_in_zulip = self.example_user("cordelia")
 | 
			
		||||
        cordelia_in_lear = get_user("cordelia@zulip.com", lear_realm)
 | 
			
		||||
 | 
			
		||||
        email = "cordelia@zulip.com"
 | 
			
		||||
        realms = get_membership_realms(email)
 | 
			
		||||
        self.assert_length(realms, 2)
 | 
			
		||||
        self.assertIn(zulip_realm, realms)
 | 
			
		||||
        self.assertIn(lear_realm, realms)
 | 
			
		||||
        accounts = get_accounts_for_email(email)
 | 
			
		||||
        self.assert_length(accounts, 2)
 | 
			
		||||
        check_account_present_in_accounts(cordelia_in_zulip, accounts)
 | 
			
		||||
        check_account_present_in_accounts(cordelia_in_lear, accounts)
 | 
			
		||||
 | 
			
		||||
        email = "CORDelia@zulip.com"
 | 
			
		||||
        realms = get_membership_realms(email)
 | 
			
		||||
        self.assert_length(realms, 2)
 | 
			
		||||
        self.assertIn(zulip_realm, realms)
 | 
			
		||||
        self.assertIn(lear_realm, realms)
 | 
			
		||||
        accounts = get_accounts_for_email(email)
 | 
			
		||||
        self.assert_length(accounts, 2)
 | 
			
		||||
        check_account_present_in_accounts(cordelia_in_zulip, accounts)
 | 
			
		||||
        check_account_present_in_accounts(cordelia_in_lear, accounts)
 | 
			
		||||
 | 
			
		||||
        email = "IAGO@ZULIP.COM"
 | 
			
		||||
        realms = get_membership_realms(email)
 | 
			
		||||
        self.assert_length(realms, 1)
 | 
			
		||||
        self.assertIn(zulip_realm, realms)
 | 
			
		||||
        accounts = get_accounts_for_email(email)
 | 
			
		||||
        self.assert_length(accounts, 1)
 | 
			
		||||
        check_account_present_in_accounts(self.example_user("iago"), accounts)
 | 
			
		||||
 | 
			
		||||
    def test_get_source_profile(self) -> None:
 | 
			
		||||
        iago = get_source_profile("iago@zulip.com", "zulip")
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ from zerver.context_processors import get_realm_from_request
 | 
			
		||||
from zerver.models import UserProfile, Realm, Stream, MultiuseInvite, \
 | 
			
		||||
    name_changes_disabled, email_to_username, email_allowed_for_realm, \
 | 
			
		||||
    get_realm, get_user, get_default_stream_groups, DisposableEmailError, \
 | 
			
		||||
    DomainNotAllowedForRealmError, get_membership_realms, get_source_profile
 | 
			
		||||
    DomainNotAllowedForRealmError, get_source_profile
 | 
			
		||||
from zerver.lib.send_email import send_email, FromAddress
 | 
			
		||||
from zerver.lib.events import do_events_register
 | 
			
		||||
from zerver.lib.actions import do_change_password, do_change_full_name, do_change_is_admin, \
 | 
			
		||||
@@ -32,6 +32,7 @@ from zerver.lib.onboarding import setup_initial_streams, \
 | 
			
		||||
from zerver.lib.response import json_success
 | 
			
		||||
from zerver.lib.subdomains import get_subdomain, is_root_domain_available
 | 
			
		||||
from zerver.lib.timezone import get_all_timezones
 | 
			
		||||
from zerver.lib.users import get_accounts_for_email
 | 
			
		||||
from zerver.views.auth import create_preregistration_user, \
 | 
			
		||||
    redirect_and_log_into_subdomain, \
 | 
			
		||||
    redirect_to_deactivation_notice
 | 
			
		||||
@@ -320,7 +321,7 @@ def accounts_register(request: HttpRequest) -> HttpResponse:
 | 
			
		||||
                 'password_auth_enabled': password_auth_enabled(realm),
 | 
			
		||||
                 'root_domain_available': is_root_domain_available(),
 | 
			
		||||
                 'default_stream_groups': get_default_stream_groups(realm),
 | 
			
		||||
                 'membership_realms': get_membership_realms(email),
 | 
			
		||||
                 'accounts': get_accounts_for_email(email),
 | 
			
		||||
                 'MAX_REALM_NAME_LENGTH': str(Realm.MAX_REALM_NAME_LENGTH),
 | 
			
		||||
                 'MAX_NAME_LENGTH': str(UserProfile.MAX_NAME_LENGTH),
 | 
			
		||||
                 'MAX_PASSWORD_LENGTH': str(form.MAX_PASSWORD_LENGTH),
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user