Ejemplo n.º 1
0
def login_context(request: HttpRequest) -> Dict[str, Any]:
    realm = get_realm_from_request(request)

    if realm is None:
        realm_description = None
        realm_invite_required = False
        realm_web_public_access_enabled = False
    else:
        realm_description = get_realm_rendered_description(realm)
        realm_invite_required = realm.invite_required
        # We offer web-public access only if the realm has actual web
        # public streams configured, in addition to having it enabled.
        realm_web_public_access_enabled = realm.allow_web_public_streams_access(
        )

    context: Dict[str, Any] = {
        "realm_invite_required": realm_invite_required,
        "realm_description": realm_description,
        "require_email_format_usernames":
        require_email_format_usernames(realm),
        "password_auth_enabled": password_auth_enabled(realm),
        "two_factor_authentication_enabled":
        settings.TWO_FACTOR_AUTHENTICATION_ENABLED,
        "realm_web_public_access_enabled": realm_web_public_access_enabled,
    }

    if realm is not None and realm.description:
        context["OPEN_GRAPH_TITLE"] = realm.name
        context["OPEN_GRAPH_DESCRIPTION"] = get_realm_text_description(realm)

    # Add the keys for our standard authentication backends.
    no_auth_enabled = True
    for auth_backend_name in AUTH_BACKEND_NAME_MAP:
        name_lower = auth_backend_name.lower()
        key = f"{name_lower}_auth_enabled"
        is_enabled = auth_enabled_helper([auth_backend_name], realm)
        context[key] = is_enabled
        if is_enabled:
            no_auth_enabled = False

    context["external_authentication_methods"] = get_external_method_dicts(
        realm)
    context["no_auth_enabled"] = no_auth_enabled

    # Include another copy of external_authentication_methods in page_params for use
    # by the desktop client. We expand it with IDs of the <button> elements corresponding
    # to the authentication methods.
    context["page_params"] = dict(
        external_authentication_methods=get_external_method_dicts(realm), )
    for auth_dict in context["page_params"]["external_authentication_methods"]:
        auth_dict["button_id_suffix"] = "auth_button_{}".format(
            auth_dict["name"])

    return context
Ejemplo n.º 2
0
def login_context(request: HttpRequest) -> Dict[str, Any]:
    realm = get_realm_from_request(request)

    if realm is None:
        realm_description = None
        realm_invite_required = False
    else:
        realm_description = get_realm_rendered_description(realm)
        realm_invite_required = realm.invite_required

    context: Dict[str, Any] = {
        'realm_invite_required':
        realm_invite_required,
        'realm_description':
        realm_description,
        'require_email_format_usernames':
        require_email_format_usernames(realm),
        'password_auth_enabled':
        password_auth_enabled(realm),
        'any_social_backend_enabled':
        any_social_backend_enabled(realm),
        'two_factor_authentication_enabled':
        settings.TWO_FACTOR_AUTHENTICATION_ENABLED,
    }

    if realm is not None and realm.description:
        context['OPEN_GRAPH_TITLE'] = realm.name
        context['OPEN_GRAPH_DESCRIPTION'] = get_realm_text_description(realm)

    # Add the keys for our standard authentication backends.
    no_auth_enabled = True
    for auth_backend_name in AUTH_BACKEND_NAME_MAP:
        name_lower = auth_backend_name.lower()
        key = f"{name_lower}_auth_enabled"
        is_enabled = auth_enabled_helper([auth_backend_name], realm)
        context[key] = is_enabled
        if is_enabled:
            no_auth_enabled = False

    context['external_authentication_methods'] = get_external_method_dicts(
        realm)
    context['no_auth_enabled'] = no_auth_enabled

    # Include another copy of external_authentication_methods in page_params for use
    # by the desktop client. We expand it with IDs of the <button> elements corresponding
    # to the authentication methods.
    context['page_params'] = dict(
        external_authentication_methods=get_external_method_dicts(realm), )
    for auth_dict in context['page_params']['external_authentication_methods']:
        auth_dict['button_id_suffix'] = "auth_button_{}".format(
            auth_dict['name'])

    return context
Ejemplo n.º 3
0
def enqueue_welcome_emails(user: UserProfile,
                           realm_creation: bool = False) -> None:
    from zerver.context_processors import common_context
    if settings.WELCOME_EMAIL_SENDER is not None:
        # line break to avoid triggering lint rule
        from_name = settings.WELCOME_EMAIL_SENDER['name']
        from_address = settings.WELCOME_EMAIL_SENDER['email']
    else:
        from_name = None
        from_address = FromAddress.SUPPORT

    other_account_count = UserProfile.objects.filter(
        email__iexact=user.email).exclude(id=user.id).count()
    unsubscribe_link = one_click_unsubscribe_link(user, "welcome")
    context = common_context(user)
    context.update({
        'unsubscribe_link': unsubscribe_link,
        'keyboard_shortcuts_link': user.realm.uri + '/help/keyboard-shortcuts',
        'realm_name': user.realm.name,
        'realm_creation': realm_creation,
        'email': user.email,
        'is_realm_admin': user.is_realm_admin,
    })
    if user.is_realm_admin:
        context['getting_started_link'] = (
            user.realm.uri +
            '/help/getting-your-organization-started-with-zulip')
    else:
        context['getting_started_link'] = "https://zulipchat.com"

    from zproject.backends import email_belongs_to_ldap, require_email_format_usernames
    if email_belongs_to_ldap(
            user.realm,
            user.email) and not require_email_format_usernames(user.realm):
        context["ldap_username"] = True

    send_future_email("zerver/emails/followup_day1",
                      user.realm,
                      to_user_id=user.id,
                      from_name=from_name,
                      from_address=from_address,
                      context=context)

    if other_account_count == 0:
        send_future_email("zerver/emails/followup_day2",
                          user.realm,
                          to_user_id=user.id,
                          from_name=from_name,
                          from_address=from_address,
                          context=context,
                          delay=followup_day2_email_delay(user))
Ejemplo n.º 4
0
def login_context(request: HttpRequest) -> Dict[str, Any]:
    realm = get_realm_from_request(request)

    if realm is None:
        realm_description = None
        realm_invite_required = False
    else:
        realm_description = get_realm_rendered_description(realm)
        realm_invite_required = realm.invite_required

    context = {
        'realm_invite_required': realm_invite_required,
        'realm_description': realm_description,
        'require_email_format_usernames': require_email_format_usernames(realm),
        'password_auth_enabled': password_auth_enabled(realm),
        'any_oauth_backend_enabled': any_oauth_backend_enabled(realm),
        'two_factor_authentication_enabled': settings.TWO_FACTOR_AUTHENTICATION_ENABLED,
    }  # type: Dict[str, Any]

    # Add the keys for our standard authentication backends.
    no_auth_enabled = True
    social_backends = []
    for auth_backend_name in AUTH_BACKEND_NAME_MAP:
        name_lower = auth_backend_name.lower()
        key = "%s_auth_enabled" % (name_lower,)
        is_enabled = auth_enabled_helper([auth_backend_name], realm)
        context[key] = is_enabled
        if is_enabled:
            no_auth_enabled = False

        # Now add the enabled social backends to the social_backends
        # list used to generate buttons for login/register pages.
        backend = AUTH_BACKEND_NAME_MAP[auth_backend_name]
        if not is_enabled or backend not in SOCIAL_AUTH_BACKENDS:
            continue
        social_backends.append({
            'name': backend.name,
            'display_name': backend.auth_backend_name,
            'login_url': reverse('login-social', args=(backend.name,)),
            'signup_url': reverse('signup-social', args=(backend.name,)),
            'sort_order': backend.sort_order,
        })
    context['social_backends'] = sorted(social_backends, key=lambda x: x['sort_order'], reverse=True)
    context['no_auth_enabled'] = no_auth_enabled

    return context
Ejemplo n.º 5
0
def login_context(request: HttpRequest) -> Dict[str, Any]:
    realm = get_realm_from_request(request)

    if realm is None:
        realm_description = None
        realm_invite_required = False
    else:
        realm_description = get_realm_rendered_description(realm)
        realm_invite_required = realm.invite_required

    context = {
        'realm_invite_required':
        realm_invite_required,
        'realm_description':
        realm_description,
        'require_email_format_usernames':
        require_email_format_usernames(realm),
        'password_auth_enabled':
        password_auth_enabled(realm),
        'any_social_backend_enabled':
        any_social_backend_enabled(realm),
        'two_factor_authentication_enabled':
        settings.TWO_FACTOR_AUTHENTICATION_ENABLED,
    }  # type: Dict[str, Any]

    if realm is not None and realm.description:
        context['OPEN_GRAPH_TITLE'] = realm.name
        context['OPEN_GRAPH_DESCRIPTION'] = get_realm_text_description(realm)

    # Add the keys for our standard authentication backends.
    no_auth_enabled = True
    for auth_backend_name in AUTH_BACKEND_NAME_MAP:
        name_lower = auth_backend_name.lower()
        key = "%s_auth_enabled" % (name_lower, )
        is_enabled = auth_enabled_helper([auth_backend_name], realm)
        context[key] = is_enabled
        if is_enabled:
            no_auth_enabled = False

    context['external_authentication_methods'] = get_external_method_dicts(
        realm)
    context['no_auth_enabled'] = no_auth_enabled

    return context
Ejemplo n.º 6
0
def zulip_default_context(request):
    # type: (HttpRequest) -> Dict[str, Any]
    """Context available to all Zulip Jinja2 templates that have a request
    passed in.  Designed to provide the long list of variables at the
    bottom of this function in a wide range of situations: logged-in
    or logged-out, subdomains or not, etc.

    The main variable in the below is whether we know what realm the
    user is trying to interact with.
    """
    realm = get_realm_from_request(request)

    if realm is None:
        realm_uri = settings.ROOT_DOMAIN_URI
        realm_name = None
        realm_icon = None
        realm_description = None
        realm_invite_required = False
    else:
        realm_uri = realm.uri
        realm_name = realm.name
        realm_icon = get_realm_icon_url(realm)
        realm_description_raw = realm.description or "The coolest place in the universe."
        realm_description = convert(realm_description_raw, message_realm=realm)
        realm_invite_required = realm.invite_required

    register_link_disabled = settings.REGISTER_LINK_DISABLED
    login_link_disabled = settings.LOGIN_LINK_DISABLED
    about_link_disabled = settings.ABOUT_LINK_DISABLED
    find_team_link_disabled = settings.FIND_TEAM_LINK_DISABLED

    if settings.ROOT_DOMAIN_LANDING_PAGE and get_subdomain(request) == "":
        register_link_disabled = True
        login_link_disabled = True
        about_link_disabled = True
        find_team_link_disabled = False

    apps_page_url = 'https://zulipchat.com/apps/'
    if settings.ZILENCER_ENABLED:
        apps_page_url = '/apps/'

    user_is_authenticated = False
    if hasattr(request, 'user') and hasattr(request.user, 'is_authenticated'):
        user_is_authenticated = request.user.is_authenticated.value

    if settings.DEVELOPMENT:
        secrets_path = "zproject/dev-secrets.conf"
        settings_path = "zproject/dev_settings.py"
        settings_comments_path = "zproject/prod_settings_template.py"
    else:
        secrets_path = "/etc/zulip/zulip-secrets.conf"
        settings_path = "/etc/zulip/settings.py"
        settings_comments_path = "/etc/zulip/settings.py"

    if hasattr(request, "client") and request.client.name == "ZulipElectron":
        platform = "ZulipElectron"  # nocoverage
    else:
        platform = "ZulipWeb"

    return {
        'root_domain_landing_page':
        settings.ROOT_DOMAIN_LANDING_PAGE,
        'custom_logo_url':
        settings.CUSTOM_LOGO_URL,
        'register_link_disabled':
        register_link_disabled,
        'login_link_disabled':
        login_link_disabled,
        'about_link_disabled':
        about_link_disabled,
        'terms_of_service':
        settings.TERMS_OF_SERVICE,
        'privacy_policy':
        settings.PRIVACY_POLICY,
        'login_url':
        settings.HOME_NOT_LOGGED_IN,
        'only_sso':
        settings.ONLY_SSO,
        'external_api_path':
        settings.EXTERNAL_API_PATH,
        'external_api_uri':
        settings.EXTERNAL_API_URI,
        'external_host':
        settings.EXTERNAL_HOST,
        'external_uri_scheme':
        settings.EXTERNAL_URI_SCHEME,
        'realm_invite_required':
        realm_invite_required,
        'realm_uri':
        realm_uri,
        'realm_name':
        realm_name,
        'realm_icon':
        realm_icon,
        'realm_description':
        realm_description,
        'root_domain_uri':
        settings.ROOT_DOMAIN_URI,
        'api_site_required':
        settings.EXTERNAL_API_PATH != "api.zulip.com",
        'email_gateway_example':
        settings.EMAIL_GATEWAY_EXAMPLE,
        'apps_page_url':
        apps_page_url,
        'open_realm_creation':
        settings.OPEN_REALM_CREATION,
        'password_auth_enabled':
        password_auth_enabled(realm),
        'dev_auth_enabled':
        dev_auth_enabled(realm),
        'google_auth_enabled':
        google_auth_enabled(realm),
        'github_auth_enabled':
        github_auth_enabled(realm),
        'email_auth_enabled':
        email_auth_enabled(realm),
        'require_email_format_usernames':
        require_email_format_usernames(realm),
        'any_oauth_backend_enabled':
        any_oauth_backend_enabled(realm),
        'no_auth_enabled':
        not auth_enabled_helper(list(AUTH_BACKEND_NAME_MAP.keys()), realm),
        'development_environment':
        settings.DEVELOPMENT,
        'support_email':
        FromAddress.SUPPORT,
        'find_team_link_disabled':
        find_team_link_disabled,
        'password_min_length':
        settings.PASSWORD_MIN_LENGTH,
        'password_min_guesses':
        settings.PASSWORD_MIN_GUESSES,
        'zulip_version':
        ZULIP_VERSION,
        'user_is_authenticated':
        user_is_authenticated,
        'settings_path':
        settings_path,
        'secrets_path':
        secrets_path,
        'settings_comments_path':
        settings_comments_path,
        'platform':
        platform,
    }
Ejemplo n.º 7
0
def zulip_default_context(request: HttpRequest) -> Dict[str, Any]:
    """Context available to all Zulip Jinja2 templates that have a request
    passed in.  Designed to provide the long list of variables at the
    bottom of this function in a wide range of situations: logged-in
    or logged-out, subdomains or not, etc.

    The main variable in the below is whether we know what realm the
    user is trying to interact with.
    """
    realm = get_realm_from_request(request)

    if realm is None:
        realm_uri = settings.ROOT_DOMAIN_URI
        realm_name = None
        realm_icon = None
        realm_description = None
        realm_invite_required = False
    else:
        realm_uri = realm.uri
        realm_name = realm.name
        realm_icon = get_realm_icon_url(realm)
        realm_description_raw = realm.description or "The coolest place in the universe."
        realm_description = convert(realm_description_raw, message_realm=realm)
        realm_invite_required = realm.invite_required

    register_link_disabled = settings.REGISTER_LINK_DISABLED
    login_link_disabled = settings.LOGIN_LINK_DISABLED
    find_team_link_disabled = settings.FIND_TEAM_LINK_DISABLED
    allow_search_engine_indexing = False

    if (settings.ROOT_DOMAIN_LANDING_PAGE
            and get_subdomain(request) == Realm.SUBDOMAIN_FOR_ROOT_DOMAIN):
        register_link_disabled = True
        login_link_disabled = True
        find_team_link_disabled = False
        allow_search_engine_indexing = True

    apps_page_url = 'https://zulipchat.com/apps/'
    if settings.ZILENCER_ENABLED:
        apps_page_url = '/apps/'

    user_is_authenticated = False
    if hasattr(request, 'user') and hasattr(request.user, 'is_authenticated'):
        user_is_authenticated = request.user.is_authenticated.value

    if settings.DEVELOPMENT:
        secrets_path = "zproject/dev-secrets.conf"
        settings_path = "zproject/dev_settings.py"
        settings_comments_path = "zproject/prod_settings_template.py"
    else:
        secrets_path = "/etc/zulip/zulip-secrets.conf"
        settings_path = "/etc/zulip/settings.py"
        settings_comments_path = "/etc/zulip/settings.py"

    if hasattr(request, "client") and request.client.name == "ZulipElectron":
        platform = "ZulipElectron"  # nocoverage
    else:
        platform = "ZulipWeb"

    return {
        'root_domain_landing_page': settings.ROOT_DOMAIN_LANDING_PAGE,
        'custom_logo_url': settings.CUSTOM_LOGO_URL,
        'register_link_disabled': register_link_disabled,
        'login_link_disabled': login_link_disabled,
        'terms_of_service': settings.TERMS_OF_SERVICE,
        'privacy_policy': settings.PRIVACY_POLICY,
        'login_url': settings.HOME_NOT_LOGGED_IN,
        'only_sso': settings.ONLY_SSO,
        'custom_profile_fields_enabled': settings.DEVELOPMENT,
        'external_host': settings.EXTERNAL_HOST,
        'external_uri_scheme': settings.EXTERNAL_URI_SCHEME,
        'realm_invite_required': realm_invite_required,
        'realm_uri': realm_uri,
        'realm_name': realm_name,
        'realm_icon': realm_icon,
        'realm_description': realm_description,
        'root_domain_uri': settings.ROOT_DOMAIN_URI,
        'apps_page_url': apps_page_url,
        'open_realm_creation': settings.OPEN_REALM_CREATION,
        'password_auth_enabled': password_auth_enabled(realm),
        'dev_auth_enabled': dev_auth_enabled(realm),
        'google_auth_enabled': google_auth_enabled(realm),
        'github_auth_enabled': github_auth_enabled(realm),
        'email_auth_enabled': email_auth_enabled(realm),
        'require_email_format_usernames': require_email_format_usernames(realm),
        'any_oauth_backend_enabled': any_oauth_backend_enabled(realm),
        'no_auth_enabled': not auth_enabled_helper(list(AUTH_BACKEND_NAME_MAP.keys()), realm),
        'development_environment': settings.DEVELOPMENT,
        'support_email': FromAddress.SUPPORT,
        'find_team_link_disabled': find_team_link_disabled,
        'password_min_length': settings.PASSWORD_MIN_LENGTH,
        'password_min_guesses': settings.PASSWORD_MIN_GUESSES,
        'jitsi_server_url': settings.JITSI_SERVER_URL,
        'two_factor_authentication_enabled': settings.TWO_FACTOR_AUTHENTICATION_ENABLED,
        'zulip_version': ZULIP_VERSION,
        'user_is_authenticated': user_is_authenticated,
        'settings_path': settings_path,
        'secrets_path': secrets_path,
        'settings_comments_path': settings_comments_path,
        'platform': platform,
        'allow_search_engine_indexing': allow_search_engine_indexing,
    }
Ejemplo n.º 8
0
def zulip_default_context(request: HttpRequest) -> Dict[str, Any]:
    """Context available to all Zulip Jinja2 templates that have a request
    passed in.  Designed to provide the long list of variables at the
    bottom of this function in a wide range of situations: logged-in
    or logged-out, subdomains or not, etc.

    The main variable in the below is whether we know what realm the
    user is trying to interact with.
    """
    realm = get_realm_from_request(request)

    if realm is None:
        realm_uri = settings.ROOT_DOMAIN_URI
        realm_name = None
        realm_icon = None
        realm_description = None
        realm_invite_required = False
        realm_plan_type = 0
    else:
        realm_uri = realm.uri
        realm_name = realm.name
        realm_icon = get_realm_icon_url(realm)
        realm_description_raw = realm.description or "The coolest place in the universe."
        realm_description = bugdown_convert(realm_description_raw, message_realm=realm)
        realm_invite_required = realm.invite_required
        realm_plan_type = realm.plan_type

    register_link_disabled = settings.REGISTER_LINK_DISABLED
    login_link_disabled = settings.LOGIN_LINK_DISABLED
    find_team_link_disabled = settings.FIND_TEAM_LINK_DISABLED
    allow_search_engine_indexing = False

    if (settings.ROOT_DOMAIN_LANDING_PAGE
            and get_subdomain(request) == Realm.SUBDOMAIN_FOR_ROOT_DOMAIN):
        register_link_disabled = True
        login_link_disabled = True
        find_team_link_disabled = False
        allow_search_engine_indexing = True

    apps_page_url = 'https://zulipchat.com/apps/'
    if settings.ZILENCER_ENABLED:
        apps_page_url = '/apps/'

    user_is_authenticated = False
    if hasattr(request, 'user') and hasattr(request.user, 'is_authenticated'):
        user_is_authenticated = request.user.is_authenticated.value

    if settings.DEVELOPMENT:
        secrets_path = "zproject/dev-secrets.conf"
        settings_path = "zproject/dev_settings.py"
        settings_comments_path = "zproject/prod_settings_template.py"
    else:
        secrets_path = "/etc/zulip/zulip-secrets.conf"
        settings_path = "/etc/zulip/settings.py"
        settings_comments_path = "/etc/zulip/settings.py"

    # We can't use request.client here because we might not be using
    # an auth decorator that sets it, but we can call its helper to
    # get the same result.
    platform = get_client_name(request, True)

    context = {
        'root_domain_landing_page': settings.ROOT_DOMAIN_LANDING_PAGE,
        'custom_logo_url': settings.CUSTOM_LOGO_URL,
        'register_link_disabled': register_link_disabled,
        'login_link_disabled': login_link_disabled,
        'terms_of_service': settings.TERMS_OF_SERVICE,
        'privacy_policy': settings.PRIVACY_POLICY,
        'login_url': settings.HOME_NOT_LOGGED_IN,
        'only_sso': settings.ONLY_SSO,
        'external_host': settings.EXTERNAL_HOST,
        'external_uri_scheme': settings.EXTERNAL_URI_SCHEME,
        'realm_invite_required': realm_invite_required,
        'realm_uri': realm_uri,
        'realm_name': realm_name,
        'realm_icon': realm_icon,
        'realm_description': realm_description,
        'realm_plan_type': realm_plan_type,
        'root_domain_uri': settings.ROOT_DOMAIN_URI,
        'apps_page_url': apps_page_url,
        'open_realm_creation': settings.OPEN_REALM_CREATION,
        'password_auth_enabled': password_auth_enabled(realm),
        'require_email_format_usernames': require_email_format_usernames(realm),
        'any_oauth_backend_enabled': any_oauth_backend_enabled(realm),
        'no_auth_enabled': not auth_enabled_helper(list(AUTH_BACKEND_NAME_MAP.keys()), realm),
        'development_environment': settings.DEVELOPMENT,
        'support_email': FromAddress.SUPPORT,
        'find_team_link_disabled': find_team_link_disabled,
        'password_min_length': settings.PASSWORD_MIN_LENGTH,
        'password_min_guesses': settings.PASSWORD_MIN_GUESSES,
        'jitsi_server_url': settings.JITSI_SERVER_URL,
        'two_factor_authentication_enabled': settings.TWO_FACTOR_AUTHENTICATION_ENABLED,
        'zulip_version': ZULIP_VERSION,
        'latest_release_version': LATEST_RELEASE_VERSION,
        'latest_major_version': LATEST_MAJOR_VERSION,
        'latest_release_announcement': LATEST_RELEASE_ANNOUNCEMENT,
        'user_is_authenticated': user_is_authenticated,
        'settings_path': settings_path,
        'secrets_path': secrets_path,
        'settings_comments_path': settings_comments_path,
        'platform': platform,
        'allow_search_engine_indexing': allow_search_engine_indexing,
    }

    # Add the keys for our standard authentication backends.
    for auth_backend_name in AUTH_BACKEND_NAME_MAP:
        name_lower = auth_backend_name.lower()
        key = "%s_auth_enabled" % (name_lower,)
        context[key] = auth_enabled_helper([auth_backend_name], realm)

    social_backends = []
    for backend in SOCIAL_AUTH_BACKENDS:
        if not auth_enabled_helper([backend.auth_backend_name], realm):
            continue
        social_backends.append({
            'name': backend.name,
            'display_name': backend.auth_backend_name,
            'login_url': reverse('login-social', args=(backend.name,)),
            'signup_url': reverse('signup-social', args=(backend.name,)),
            'sort_order': backend.sort_order,
        })
    context['social_backends'] = sorted(social_backends, key=lambda x: x['sort_order'])

    return context
Ejemplo n.º 9
0
def zulip_default_context(request: HttpRequest) -> Dict[str, Any]:
    """Context available to all Zulip Jinja2 templates that have a request
    passed in.  Designed to provide the long list of variables at the
    bottom of this function in a wide range of situations: logged-in
    or logged-out, subdomains or not, etc.

    The main variable in the below is whether we know what realm the
    user is trying to interact with.
    """
    realm = get_realm_from_request(request)

    if realm is None:
        realm_uri = settings.ROOT_DOMAIN_URI
        realm_name = None
        realm_icon = None
        realm_logo = None
        realm_description = None
        realm_invite_required = False
        realm_plan_type = 0
    else:
        realm_uri = realm.uri
        realm_name = realm.name
        realm_icon = get_realm_icon_url(realm)
        realm_logo = get_realm_logo_url(realm)
        realm_description_raw = realm.description or "The coolest place in the universe."
        realm_description = bugdown_convert(realm_description_raw,
                                            message_realm=realm)
        realm_invite_required = realm.invite_required
        realm_plan_type = realm.plan_type

    register_link_disabled = settings.REGISTER_LINK_DISABLED
    login_link_disabled = settings.LOGIN_LINK_DISABLED
    find_team_link_disabled = settings.FIND_TEAM_LINK_DISABLED
    allow_search_engine_indexing = False

    if (settings.ROOT_DOMAIN_LANDING_PAGE
            and get_subdomain(request) == Realm.SUBDOMAIN_FOR_ROOT_DOMAIN):
        register_link_disabled = True
        login_link_disabled = True
        find_team_link_disabled = False
        allow_search_engine_indexing = True

    apps_page_url = 'https://zulipchat.com/apps/'
    if settings.ZILENCER_ENABLED:
        apps_page_url = '/apps/'

    user_is_authenticated = False
    if hasattr(request, 'user') and hasattr(request.user, 'is_authenticated'):
        user_is_authenticated = request.user.is_authenticated.value

    if settings.DEVELOPMENT:
        secrets_path = "zproject/dev-secrets.conf"
        settings_path = "zproject/dev_settings.py"
        settings_comments_path = "zproject/prod_settings_template.py"
    else:
        secrets_path = "/etc/zulip/zulip-secrets.conf"
        settings_path = "/etc/zulip/settings.py"
        settings_comments_path = "/etc/zulip/settings.py"

    if hasattr(request, "client") and request.client.name == "ZulipElectron":
        platform = "ZulipElectron"  # nocoverage
    else:
        platform = "ZulipWeb"

    context = {
        'root_domain_landing_page':
        settings.ROOT_DOMAIN_LANDING_PAGE,
        'custom_logo_url':
        settings.CUSTOM_LOGO_URL,
        'register_link_disabled':
        register_link_disabled,
        'login_link_disabled':
        login_link_disabled,
        'terms_of_service':
        settings.TERMS_OF_SERVICE,
        'privacy_policy':
        settings.PRIVACY_POLICY,
        'login_url':
        settings.HOME_NOT_LOGGED_IN,
        'only_sso':
        settings.ONLY_SSO,
        'external_host':
        settings.EXTERNAL_HOST,
        'external_uri_scheme':
        settings.EXTERNAL_URI_SCHEME,
        'realm_invite_required':
        realm_invite_required,
        'realm_uri':
        realm_uri,
        'realm_name':
        realm_name,
        'realm_icon':
        realm_icon,
        'realm_logo':
        realm_logo,
        'realm_description':
        realm_description,
        'realm_plan_type':
        realm_plan_type,
        'root_domain_uri':
        settings.ROOT_DOMAIN_URI,
        'apps_page_url':
        apps_page_url,
        'open_realm_creation':
        settings.OPEN_REALM_CREATION,
        'password_auth_enabled':
        password_auth_enabled(realm),
        'require_email_format_usernames':
        require_email_format_usernames(realm),
        'any_oauth_backend_enabled':
        any_oauth_backend_enabled(realm),
        'no_auth_enabled':
        not auth_enabled_helper(list(AUTH_BACKEND_NAME_MAP.keys()), realm),
        'development_environment':
        settings.DEVELOPMENT,
        'support_email':
        FromAddress.SUPPORT,
        'find_team_link_disabled':
        find_team_link_disabled,
        'password_min_length':
        settings.PASSWORD_MIN_LENGTH,
        'password_min_guesses':
        settings.PASSWORD_MIN_GUESSES,
        'jitsi_server_url':
        settings.JITSI_SERVER_URL,
        'two_factor_authentication_enabled':
        settings.TWO_FACTOR_AUTHENTICATION_ENABLED,
        'zulip_version':
        ZULIP_VERSION,
        'latest_release_version':
        LATEST_RELEASE_VERSION,
        'latest_major_version':
        LATEST_MAJOR_VERSION,
        'latest_release_announcement':
        LATEST_RELEASE_ANNOUNCEMENT,
        'user_is_authenticated':
        user_is_authenticated,
        'settings_path':
        settings_path,
        'secrets_path':
        secrets_path,
        'settings_comments_path':
        settings_comments_path,
        'platform':
        platform,
        'allow_search_engine_indexing':
        allow_search_engine_indexing,
    }

    # Add the keys for our standard authentication backends.
    for auth_backend_name in AUTH_BACKEND_NAME_MAP:
        name_lower = auth_backend_name.lower()
        key = "%s_auth_enabled" % (name_lower, )
        context[key] = auth_enabled_helper([auth_backend_name], realm)
    return context
Ejemplo n.º 10
0
def zulip_default_context(request: HttpRequest) -> Dict[str, Any]:
    """Context available to all Zulip Jinja2 templates that have a request
    passed in.  Designed to provide the long list of variables at the
    bottom of this function in a wide range of situations: logged-in
    or logged-out, subdomains or not, etc.

    The main variable in the below is whether we know what realm the
    user is trying to interact with.
    """
    realm = get_realm_from_request(request)

    if realm is None:
        realm_uri = settings.ROOT_DOMAIN_URI
        realm_name = None
        realm_icon = None
        realm_description = None
        realm_invite_required = False
        realm_plan_type = 0
    else:
        realm_uri = realm.uri
        realm_name = realm.name
        realm_icon = get_realm_icon_url(realm)
        realm_description = get_realm_rendered_description(realm)
        realm_invite_required = realm.invite_required
        realm_plan_type = realm.plan_type

    register_link_disabled = settings.REGISTER_LINK_DISABLED
    login_link_disabled = settings.LOGIN_LINK_DISABLED
    find_team_link_disabled = settings.FIND_TEAM_LINK_DISABLED
    allow_search_engine_indexing = False

    if (settings.ROOT_DOMAIN_LANDING_PAGE
            and get_subdomain(request) == Realm.SUBDOMAIN_FOR_ROOT_DOMAIN):
        register_link_disabled = True
        login_link_disabled = True
        find_team_link_disabled = False
        allow_search_engine_indexing = True

    apps_page_url = 'https://zulipchat.com/apps/'
    if settings.ZILENCER_ENABLED:
        apps_page_url = '/apps/'

    user_is_authenticated = False
    if hasattr(request, 'user') and hasattr(request.user, 'is_authenticated'):
        user_is_authenticated = request.user.is_authenticated.value

    if settings.DEVELOPMENT:
        secrets_path = "zproject/dev-secrets.conf"
        settings_path = "zproject/dev_settings.py"
        settings_comments_path = "zproject/prod_settings_template.py"
    else:
        secrets_path = "/etc/zulip/zulip-secrets.conf"
        settings_path = "/etc/zulip/settings.py"
        settings_comments_path = "/etc/zulip/settings.py"

    # We can't use request.client here because we might not be using
    # an auth decorator that sets it, but we can call its helper to
    # get the same result.
    platform = get_client_name(request, True)

    context = {
        'root_domain_landing_page': settings.ROOT_DOMAIN_LANDING_PAGE,
        'custom_logo_url': settings.CUSTOM_LOGO_URL,
        'register_link_disabled': register_link_disabled,
        'login_link_disabled': login_link_disabled,
        'terms_of_service': settings.TERMS_OF_SERVICE,
        'privacy_policy': settings.PRIVACY_POLICY,
        'login_url': settings.HOME_NOT_LOGGED_IN,
        'only_sso': settings.ONLY_SSO,
        'external_host': settings.EXTERNAL_HOST,
        'external_uri_scheme': settings.EXTERNAL_URI_SCHEME,
        'realm_invite_required': realm_invite_required,
        'realm_uri': realm_uri,
        'realm_name': realm_name,
        'realm_icon': realm_icon,
        'realm_description': realm_description,
        'realm_plan_type': realm_plan_type,
        'root_domain_uri': settings.ROOT_DOMAIN_URI,
        'apps_page_url': apps_page_url,
        'open_realm_creation': settings.OPEN_REALM_CREATION,
        'password_auth_enabled': password_auth_enabled(realm),
        'require_email_format_usernames':
        require_email_format_usernames(realm),
        'any_oauth_backend_enabled': any_oauth_backend_enabled(realm),
        'development_environment': settings.DEVELOPMENT,
        'support_email': FromAddress.SUPPORT,
        'find_team_link_disabled': find_team_link_disabled,
        'password_min_length': settings.PASSWORD_MIN_LENGTH,
        'password_min_guesses': settings.PASSWORD_MIN_GUESSES,
        'jitsi_server_url': settings.JITSI_SERVER_URL,
        'two_factor_authentication_enabled':
        settings.TWO_FACTOR_AUTHENTICATION_ENABLED,
        'zulip_version': ZULIP_VERSION,
        'latest_release_version': LATEST_RELEASE_VERSION,
        'latest_major_version': LATEST_MAJOR_VERSION,
        'latest_release_announcement': LATEST_RELEASE_ANNOUNCEMENT,
        'user_is_authenticated': user_is_authenticated,
        'settings_path': settings_path,
        'secrets_path': secrets_path,
        'settings_comments_path': settings_comments_path,
        'platform': platform,
        'allow_search_engine_indexing': allow_search_engine_indexing,
    }

    # Add the keys for our standard authentication backends.
    no_auth_enabled = True
    social_backends = []
    for auth_backend_name in AUTH_BACKEND_NAME_MAP:
        name_lower = auth_backend_name.lower()
        key = "%s_auth_enabled" % (name_lower, )
        is_enabled = auth_enabled_helper([auth_backend_name], realm)
        context[key] = is_enabled
        if is_enabled:
            no_auth_enabled = False

        # Now add the enabled social backends to the social_backends
        # list used to generate buttons for login/register pages.
        backend = AUTH_BACKEND_NAME_MAP[auth_backend_name]
        if not is_enabled or backend not in SOCIAL_AUTH_BACKENDS:
            continue
        social_backends.append({
            'name':
            backend.name,
            'display_name':
            backend.auth_backend_name,
            'login_url':
            reverse('login-social', args=(backend.name, )),
            'signup_url':
            reverse('signup-social', args=(backend.name, )),
            'sort_order':
            backend.sort_order,
        })
    context['social_backends'] = sorted(social_backends,
                                        key=lambda x: x['sort_order'],
                                        reverse=True)
    context["no_auth_enabled"] = no_auth_enabled

    return context