2017-01-07 21:46:03 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-04-24 03:47:28 +02:00
|
|
|
from typing import Any, List, Dict, Mapping, Optional
|
2017-01-07 21:46:03 +01:00
|
|
|
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.conf import settings
|
2017-08-25 01:11:30 +02:00
|
|
|
from django.contrib.auth import authenticate, get_backends
|
2018-01-30 06:05:25 +01:00
|
|
|
from django.urls import reverse
|
2017-01-07 21:46:03 +01:00
|
|
|
from django.http import HttpResponseRedirect, HttpResponseForbidden, HttpResponse, HttpRequest
|
2017-03-16 14:28:08 +01:00
|
|
|
from django.shortcuts import redirect, render
|
2017-01-07 21:46:03 +01:00
|
|
|
from django.template import RequestContext, loader
|
|
|
|
from django.utils.timezone import now
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.core import validators
|
2017-10-02 08:32:09 +02:00
|
|
|
from zerver.context_processors import get_realm_from_request
|
2017-10-27 00:27:59 +02:00
|
|
|
from zerver.models import UserProfile, Realm, Stream, MultiuseInvite, \
|
2017-10-03 01:20:21 +02:00
|
|
|
name_changes_disabled, email_to_username, email_allowed_for_realm, \
|
2018-12-07 00:05:57 +01:00
|
|
|
get_realm, get_user_by_delivery_email, get_default_stream_groups, DisposableEmailError, \
|
2018-12-30 11:06:12 +01:00
|
|
|
DomainNotAllowedForRealmError, get_source_profile, EmailContainsPlusError, \
|
|
|
|
PreregistrationUser
|
2017-07-11 05:01:32 +02:00
|
|
|
from zerver.lib.send_email import send_email, FromAddress
|
2017-02-10 23:04:46 +01:00
|
|
|
from zerver.lib.events import do_events_register
|
2017-01-07 21:46:03 +01:00
|
|
|
from zerver.lib.actions import do_change_password, do_change_full_name, do_change_is_admin, \
|
2017-08-02 05:20:50 +02:00
|
|
|
do_activate_user, do_create_user, do_create_realm, \
|
2018-08-11 16:26:46 +02:00
|
|
|
email_not_system_bot, validate_email_for_realm, \
|
2017-11-20 04:14:17 +01:00
|
|
|
do_set_user_display_setting, lookup_default_stream_groups, bulk_add_subscriptions
|
2017-01-07 21:46:03 +01:00
|
|
|
from zerver.forms import RegistrationForm, HomepageForm, RealmCreationForm, \
|
2018-08-25 14:06:17 +02:00
|
|
|
CreateUserForm, FindMyTeamForm, RealmRedirectForm
|
2017-01-07 21:46:03 +01:00
|
|
|
from django_auth_ldap.backend import LDAPBackend, _LDAPUser
|
|
|
|
from zerver.decorator import require_post, has_request_variables, \
|
2017-08-25 07:22:39 +02:00
|
|
|
JsonableError, REQ, do_login
|
2017-10-25 21:48:37 +02:00
|
|
|
from zerver.lib.onboarding import setup_initial_streams, \
|
2018-01-12 09:10:43 +01:00
|
|
|
send_initial_realm_messages, setup_realm_internal_bots
|
2017-01-07 21:46:03 +01:00
|
|
|
from zerver.lib.response import json_success
|
2017-10-19 07:42:03 +02:00
|
|
|
from zerver.lib.subdomains import get_subdomain, is_root_domain_available
|
2017-05-04 15:19:50 +02:00
|
|
|
from zerver.lib.timezone import get_all_timezones
|
2018-06-19 10:55:56 +02:00
|
|
|
from zerver.lib.users import get_accounts_for_email
|
2018-08-11 16:26:46 +02:00
|
|
|
from zerver.lib.zephyr import compute_mit_user_fullname
|
2018-08-25 14:06:17 +02:00
|
|
|
from zerver.views.auth import create_preregistration_user, redirect_and_log_into_subdomain, \
|
|
|
|
redirect_to_deactivation_notice, get_safe_redirect_to
|
|
|
|
|
auth: Improve interactions between LDAPAuthBackend and EmailAuthBackend.
Previously, if you had LDAPAuthBackend enabled, we basically blocked
any other auth backends from working at all, by requiring the user's
login flow include verifying the user's LDAP password.
We still want to enforce that in the case that the account email
matches LDAP_APPEND_DOMAIN, but there's a reasonable corner case:
Having effectively guest users from outside the LDAP domain.
We don't want to allow creating a Zulip-level password for a user
inside the LDAP domain, so we still verify the LDAP password in that
flow, but if the email is allowed to register (due to invite or
whatever) but is outside the LDAP domain for the organization, we
allow it to create an account and set a password.
For the moment, this solution only covers EmailAuthBackend. It's
likely that just extending the list of other backends we check for in
the new conditional on `email_auth_backend` would be correct, but we
haven't done any testing for those cases, and with auth code paths,
it's better to disallow than allow untested code paths.
Fixes #9422.
2018-05-29 06:52:06 +02:00
|
|
|
from zproject.backends import ldap_auth_enabled, password_auth_enabled, ZulipLDAPAuthBackend, \
|
|
|
|
ZulipLDAPException, email_auth_enabled
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2017-08-10 22:34:17 +02:00
|
|
|
from confirmation.models import Confirmation, RealmCreationKey, ConfirmationKeyException, \
|
2018-01-29 20:54:49 +01:00
|
|
|
validate_key, create_confirmation_link, get_object_from_key, \
|
2017-08-10 22:34:17 +02:00
|
|
|
render_confirmation_key_error
|
2017-01-07 21:46:03 +01:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import requests
|
2017-08-17 18:28:21 +02:00
|
|
|
import smtplib
|
2017-01-07 21:46:03 +01:00
|
|
|
import ujson
|
|
|
|
|
2017-11-05 05:30:31 +01:00
|
|
|
import urllib
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2017-11-30 05:07:18 +01:00
|
|
|
def check_prereg_key_and_redirect(request: HttpRequest, confirmation_key: str) -> HttpResponse:
|
|
|
|
# If the key isn't valid, show the error message on the original URL
|
|
|
|
confirmation = Confirmation.objects.filter(confirmation_key=confirmation_key).first()
|
|
|
|
if confirmation is None or confirmation.type not in [
|
|
|
|
Confirmation.USER_REGISTRATION, Confirmation.INVITATION, Confirmation.REALM_CREATION]:
|
|
|
|
return render_confirmation_key_error(
|
|
|
|
request, ConfirmationKeyException(ConfirmationKeyException.DOES_NOT_EXIST))
|
|
|
|
try:
|
|
|
|
get_object_from_key(confirmation_key, confirmation.type)
|
|
|
|
except ConfirmationKeyException as exception:
|
|
|
|
return render_confirmation_key_error(request, exception)
|
|
|
|
|
|
|
|
# confirm_preregistrationuser.html just extracts the confirmation_key
|
|
|
|
# (and GET parameters) and redirects to /accounts/register, so that the
|
|
|
|
# user can enter their information on a cleaner URL.
|
|
|
|
return render(request, 'confirmation/confirm_preregistrationuser.html',
|
|
|
|
context={
|
|
|
|
'key': confirmation_key,
|
|
|
|
'full_name': request.GET.get("full_name", None)})
|
|
|
|
|
2017-01-07 21:46:03 +01:00
|
|
|
@require_post
|
2017-11-27 09:28:57 +01:00
|
|
|
def accounts_register(request: HttpRequest) -> HttpResponse:
|
2017-01-07 21:46:03 +01:00
|
|
|
key = request.POST['key']
|
|
|
|
confirmation = Confirmation.objects.get(confirmation_key=key)
|
|
|
|
prereg_user = confirmation.content_object
|
|
|
|
email = prereg_user.email
|
|
|
|
realm_creation = prereg_user.realm_creation
|
2017-08-04 08:09:25 +02:00
|
|
|
password_required = prereg_user.password_required
|
2018-12-30 11:06:12 +01:00
|
|
|
is_realm_admin = prereg_user.invited_as == PreregistrationUser.INVITE_AS['REALM_ADMIN'] or realm_creation
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2018-04-06 19:33:02 +02:00
|
|
|
try:
|
|
|
|
validators.validate_email(email)
|
|
|
|
except ValidationError:
|
|
|
|
return render(request, "zerver/invalid_email.html", context={"invalid_email": True})
|
|
|
|
|
2017-11-08 22:02:59 +01:00
|
|
|
if realm_creation:
|
2017-01-07 21:46:03 +01:00
|
|
|
# For creating a new realm, there is no existing realm or domain
|
|
|
|
realm = None
|
|
|
|
else:
|
2017-10-02 08:32:09 +02:00
|
|
|
realm = get_realm(get_subdomain(request))
|
registration: Enforce realm is None only if realm_creation.
Commit d4ee3023 and its parent have the history behind this code.
Since d4ee3023^, all new PreregistrationUser objects, except those for
realm creation, have a non-None `realm`. Since d4ee3023, any legacy
PreregistrationUsers, with a `realm` of None despite not being for
realm creation, are treated as expired. Now, we ignore them
completely, and remove any that exist from the database.
The user-visible effect is to change the error message for
registration (or invitation) links created before d4ee3023^ to be
"link does not exist", rather than "link expired".
This change will at most affect users upgrading straight from 1.7 or
earlier to 1.8 (rather than from 1.7.1), but I think that's not much
of a concern (such installations are probably long-running
installations, without many live registration or invitation links).
[greg: tweaked commit message]
2017-12-01 22:37:33 +01:00
|
|
|
if realm is None or realm != prereg_user.realm:
|
2017-12-02 01:07:52 +01:00
|
|
|
return render_confirmation_key_error(
|
|
|
|
request, ConfirmationKeyException(ConfirmationKeyException.DOES_NOT_EXIST))
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2018-03-14 12:54:05 +01:00
|
|
|
try:
|
|
|
|
email_allowed_for_realm(email, realm)
|
|
|
|
except DomainNotAllowedForRealmError:
|
2018-03-05 20:19:07 +01:00
|
|
|
return render(request, "zerver/invalid_email.html",
|
|
|
|
context={"realm_name": realm.name, "closed_domain": True})
|
2018-03-14 13:25:26 +01:00
|
|
|
except DisposableEmailError:
|
2018-03-05 20:19:07 +01:00
|
|
|
return render(request, "zerver/invalid_email.html",
|
|
|
|
context={"realm_name": realm.name, "disposable_emails_not_allowed": True})
|
2018-06-20 13:08:07 +02:00
|
|
|
except EmailContainsPlusError:
|
|
|
|
return render(request, "zerver/invalid_email.html",
|
|
|
|
context={"realm_name": realm.name, "email_contains_plus": True})
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2017-12-02 01:13:17 +01:00
|
|
|
if realm.deactivated:
|
|
|
|
# The user is trying to register for a deactivated realm. Advise them to
|
|
|
|
# contact support.
|
|
|
|
return redirect_to_deactivation_notice()
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2017-11-22 20:22:11 +01:00
|
|
|
try:
|
|
|
|
validate_email_for_realm(realm, email)
|
|
|
|
except ValidationError: # nocoverage # We need to add a test for this.
|
|
|
|
return HttpResponseRedirect(reverse('django.contrib.auth.views.login') + '?email=' +
|
|
|
|
urllib.parse.quote_plus(email))
|
2017-01-07 21:46:03 +01:00
|
|
|
|
|
|
|
name_validated = False
|
|
|
|
full_name = None
|
|
|
|
|
|
|
|
if request.POST.get('from_confirmation'):
|
|
|
|
try:
|
|
|
|
del request.session['authenticated_full_name']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
if realm is not None and realm.is_zephyr_mirror_realm:
|
|
|
|
# For MIT users, we can get an authoritative name from Hesiod.
|
|
|
|
# Technically we should check that this is actually an MIT
|
|
|
|
# realm, but we can cross that bridge if we ever get a non-MIT
|
|
|
|
# zephyr mirroring realm.
|
|
|
|
hesiod_name = compute_mit_user_fullname(email)
|
|
|
|
form = RegistrationForm(
|
2017-06-15 19:24:38 +02:00
|
|
|
initial={'full_name': hesiod_name if "@" not in hesiod_name else ""},
|
|
|
|
realm_creation=realm_creation)
|
2017-01-07 21:46:03 +01:00
|
|
|
name_validated = True
|
|
|
|
elif settings.POPULATE_PROFILE_VIA_LDAP:
|
|
|
|
for backend in get_backends():
|
|
|
|
if isinstance(backend, LDAPBackend):
|
auth: Improve interactions between LDAPAuthBackend and EmailAuthBackend.
Previously, if you had LDAPAuthBackend enabled, we basically blocked
any other auth backends from working at all, by requiring the user's
login flow include verifying the user's LDAP password.
We still want to enforce that in the case that the account email
matches LDAP_APPEND_DOMAIN, but there's a reasonable corner case:
Having effectively guest users from outside the LDAP domain.
We don't want to allow creating a Zulip-level password for a user
inside the LDAP domain, so we still verify the LDAP password in that
flow, but if the email is allowed to register (due to invite or
whatever) but is outside the LDAP domain for the organization, we
allow it to create an account and set a password.
For the moment, this solution only covers EmailAuthBackend. It's
likely that just extending the list of other backends we check for in
the new conditional on `email_auth_backend` would be correct, but we
haven't done any testing for those cases, and with auth code paths,
it's better to disallow than allow untested code paths.
Fixes #9422.
2018-05-29 06:52:06 +02:00
|
|
|
try:
|
2018-05-31 23:04:47 +02:00
|
|
|
ldap_username = backend.django_to_ldap_username(email)
|
auth: Improve interactions between LDAPAuthBackend and EmailAuthBackend.
Previously, if you had LDAPAuthBackend enabled, we basically blocked
any other auth backends from working at all, by requiring the user's
login flow include verifying the user's LDAP password.
We still want to enforce that in the case that the account email
matches LDAP_APPEND_DOMAIN, but there's a reasonable corner case:
Having effectively guest users from outside the LDAP domain.
We don't want to allow creating a Zulip-level password for a user
inside the LDAP domain, so we still verify the LDAP password in that
flow, but if the email is allowed to register (due to invite or
whatever) but is outside the LDAP domain for the organization, we
allow it to create an account and set a password.
For the moment, this solution only covers EmailAuthBackend. It's
likely that just extending the list of other backends we check for in
the new conditional on `email_auth_backend` would be correct, but we
haven't done any testing for those cases, and with auth code paths,
it's better to disallow than allow untested code paths.
Fixes #9422.
2018-05-29 06:52:06 +02:00
|
|
|
except ZulipLDAPException:
|
|
|
|
logging.warning("New account email %s could not be found in LDAP" % (email,))
|
|
|
|
form = RegistrationForm(realm_creation=realm_creation)
|
|
|
|
break
|
|
|
|
|
2018-05-31 23:04:47 +02:00
|
|
|
ldap_attrs = _LDAPUser(backend, ldap_username).attrs
|
|
|
|
|
2017-01-07 21:46:03 +01:00
|
|
|
try:
|
|
|
|
ldap_full_name = ldap_attrs[settings.AUTH_LDAP_USER_ATTR_MAP['full_name']][0]
|
|
|
|
request.session['authenticated_full_name'] = ldap_full_name
|
|
|
|
name_validated = True
|
|
|
|
# We don't use initial= here, because if the form is
|
|
|
|
# complete (that is, no additional fields need to be
|
|
|
|
# filled out by the user) we want the form to validate,
|
|
|
|
# so they can be directly registered without having to
|
|
|
|
# go through this interstitial.
|
2017-06-15 19:24:38 +02:00
|
|
|
form = RegistrationForm({'full_name': ldap_full_name},
|
|
|
|
realm_creation=realm_creation)
|
2017-01-07 21:46:03 +01:00
|
|
|
# FIXME: This will result in the user getting
|
|
|
|
# validation errors if they have to enter a password.
|
|
|
|
# Not relevant for ONLY_SSO, though.
|
|
|
|
break
|
|
|
|
except TypeError:
|
|
|
|
# Let the user fill out a name and/or try another backend
|
2017-06-15 19:24:38 +02:00
|
|
|
form = RegistrationForm(realm_creation=realm_creation)
|
2017-01-07 21:46:03 +01:00
|
|
|
elif 'full_name' in request.POST:
|
|
|
|
form = RegistrationForm(
|
2017-06-15 19:24:38 +02:00
|
|
|
initial={'full_name': request.POST.get('full_name')},
|
|
|
|
realm_creation=realm_creation
|
2017-01-07 21:46:03 +01:00
|
|
|
)
|
|
|
|
else:
|
2017-06-15 19:24:38 +02:00
|
|
|
form = RegistrationForm(realm_creation=realm_creation)
|
2017-01-07 21:46:03 +01:00
|
|
|
else:
|
|
|
|
postdata = request.POST.copy()
|
|
|
|
if name_changes_disabled(realm):
|
|
|
|
# If we populate profile information via LDAP and we have a
|
|
|
|
# verified name from you on file, use that. Otherwise, fall
|
|
|
|
# back to the full name in the request.
|
|
|
|
try:
|
|
|
|
postdata.update({'full_name': request.session['authenticated_full_name']})
|
|
|
|
name_validated = True
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2017-06-15 19:24:38 +02:00
|
|
|
form = RegistrationForm(postdata, realm_creation=realm_creation)
|
2017-08-04 08:09:25 +02:00
|
|
|
if not (password_auth_enabled(realm) and password_required):
|
2017-01-07 21:46:03 +01:00
|
|
|
form['password'].field.required = False
|
|
|
|
|
|
|
|
if form.is_valid():
|
|
|
|
if password_auth_enabled(realm):
|
|
|
|
password = form.cleaned_data['password']
|
|
|
|
else:
|
|
|
|
# SSO users don't need no passwords
|
|
|
|
password = None
|
|
|
|
|
|
|
|
if realm_creation:
|
|
|
|
string_id = form.cleaned_data['realm_subdomain']
|
|
|
|
realm_name = form.cleaned_data['realm_name']
|
2017-08-24 04:52:34 +02:00
|
|
|
realm = do_create_realm(string_id, realm_name)
|
2017-07-17 06:44:59 +02:00
|
|
|
setup_initial_streams(realm)
|
2018-01-12 09:10:43 +01:00
|
|
|
setup_realm_internal_bots(realm)
|
2017-05-25 18:22:28 +02:00
|
|
|
assert(realm is not None)
|
2017-01-07 21:46:03 +01:00
|
|
|
|
|
|
|
full_name = form.cleaned_data['full_name']
|
|
|
|
short_name = email_to_username(email)
|
2017-10-26 20:31:43 +02:00
|
|
|
default_stream_group_names = request.POST.getlist('default_stream_group')
|
|
|
|
default_stream_groups = lookup_default_stream_groups(default_stream_group_names, realm)
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2017-11-04 05:23:22 +01:00
|
|
|
timezone = ""
|
2017-05-04 15:19:50 +02:00
|
|
|
if 'timezone' in request.POST and request.POST['timezone'] in get_all_timezones():
|
|
|
|
timezone = request.POST['timezone']
|
|
|
|
|
2018-05-22 18:13:51 +02:00
|
|
|
if 'source_realm' in request.POST and request.POST["source_realm"] != "on":
|
|
|
|
source_profile = get_source_profile(email, request.POST["source_realm"])
|
|
|
|
else:
|
|
|
|
source_profile = None
|
|
|
|
|
2017-11-28 03:01:44 +01:00
|
|
|
if not realm_creation:
|
|
|
|
try:
|
2018-12-07 00:05:57 +01:00
|
|
|
existing_user_profile = get_user_by_delivery_email(email, realm) # type: Optional[UserProfile]
|
2017-11-28 03:01:44 +01:00
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
existing_user_profile = None
|
|
|
|
else:
|
2017-08-25 07:05:27 +02:00
|
|
|
existing_user_profile = None
|
|
|
|
|
2017-10-24 19:38:31 +02:00
|
|
|
return_data = {} # type: Dict[str, bool]
|
|
|
|
if ldap_auth_enabled(realm):
|
|
|
|
# If the user was authenticated using an external SSO
|
|
|
|
# mechanism like Google or GitHub auth, then authentication
|
|
|
|
# will have already been done before creating the
|
|
|
|
# PreregistrationUser object with password_required=False, and
|
|
|
|
# so we don't need to worry about passwords.
|
|
|
|
#
|
|
|
|
# If instead the realm is using EmailAuthBackend, we will
|
|
|
|
# set their password above.
|
|
|
|
#
|
|
|
|
# But if the realm is using LDAPAuthBackend, we need to verify
|
|
|
|
# their LDAP password (which will, as a side effect, create
|
|
|
|
# the user account) here using authenticate.
|
|
|
|
auth_result = authenticate(request,
|
|
|
|
username=email,
|
|
|
|
password=password,
|
2017-11-17 23:56:45 +01:00
|
|
|
realm=realm,
|
2017-10-24 19:38:31 +02:00
|
|
|
return_data=return_data)
|
auth: Improve interactions between LDAPAuthBackend and EmailAuthBackend.
Previously, if you had LDAPAuthBackend enabled, we basically blocked
any other auth backends from working at all, by requiring the user's
login flow include verifying the user's LDAP password.
We still want to enforce that in the case that the account email
matches LDAP_APPEND_DOMAIN, but there's a reasonable corner case:
Having effectively guest users from outside the LDAP domain.
We don't want to allow creating a Zulip-level password for a user
inside the LDAP domain, so we still verify the LDAP password in that
flow, but if the email is allowed to register (due to invite or
whatever) but is outside the LDAP domain for the organization, we
allow it to create an account and set a password.
For the moment, this solution only covers EmailAuthBackend. It's
likely that just extending the list of other backends we check for in
the new conditional on `email_auth_backend` would be correct, but we
haven't done any testing for those cases, and with auth code paths,
it's better to disallow than allow untested code paths.
Fixes #9422.
2018-05-29 06:52:06 +02:00
|
|
|
if auth_result is not None:
|
|
|
|
# Since we'll have created a user, we now just log them in.
|
|
|
|
return login_and_go_to_home(request, auth_result)
|
|
|
|
|
|
|
|
if return_data.get("outside_ldap_domain") and email_auth_enabled(realm):
|
|
|
|
# If both the LDAP and Email auth backends are
|
|
|
|
# enabled, and the user's email is outside the LDAP
|
|
|
|
# domain, then the intent is to create a user in the
|
|
|
|
# realm with their email outside the LDAP organization
|
|
|
|
# (with e.g. a password stored in the Zulip database,
|
|
|
|
# not LDAP). So we fall through and create the new
|
|
|
|
# account.
|
|
|
|
#
|
|
|
|
# It's likely that we can extend this block to the
|
|
|
|
# Google and GitHub auth backends with no code changes
|
|
|
|
# other than here.
|
|
|
|
pass
|
|
|
|
else:
|
2017-10-24 19:38:31 +02:00
|
|
|
# TODO: This probably isn't going to give a
|
|
|
|
# user-friendly error message, but it doesn't
|
|
|
|
# particularly matter, because the registration form
|
|
|
|
# is hidden for most users.
|
|
|
|
return HttpResponseRedirect(reverse('django.contrib.auth.views.login') + '?email=' +
|
|
|
|
urllib.parse.quote_plus(email))
|
|
|
|
|
auth: Improve interactions between LDAPAuthBackend and EmailAuthBackend.
Previously, if you had LDAPAuthBackend enabled, we basically blocked
any other auth backends from working at all, by requiring the user's
login flow include verifying the user's LDAP password.
We still want to enforce that in the case that the account email
matches LDAP_APPEND_DOMAIN, but there's a reasonable corner case:
Having effectively guest users from outside the LDAP domain.
We don't want to allow creating a Zulip-level password for a user
inside the LDAP domain, so we still verify the LDAP password in that
flow, but if the email is allowed to register (due to invite or
whatever) but is outside the LDAP domain for the organization, we
allow it to create an account and set a password.
For the moment, this solution only covers EmailAuthBackend. It's
likely that just extending the list of other backends we check for in
the new conditional on `email_auth_backend` would be correct, but we
haven't done any testing for those cases, and with auth code paths,
it's better to disallow than allow untested code paths.
Fixes #9422.
2018-05-29 06:52:06 +02:00
|
|
|
if existing_user_profile is not None and existing_user_profile.is_mirror_dummy:
|
2017-03-19 01:42:40 +01:00
|
|
|
user_profile = existing_user_profile
|
|
|
|
do_activate_user(user_profile)
|
|
|
|
do_change_password(user_profile, password)
|
2017-04-07 07:28:28 +02:00
|
|
|
do_change_full_name(user_profile, full_name, user_profile)
|
2017-05-04 15:19:50 +02:00
|
|
|
do_set_user_display_setting(user_profile, 'timezone', timezone)
|
2017-10-27 20:22:19 +02:00
|
|
|
# TODO: When we clean up the `do_activate_user` code path,
|
|
|
|
# make it respect invited_as_admin / is_realm_admin.
|
2017-01-07 21:46:03 +01:00
|
|
|
else:
|
|
|
|
user_profile = do_create_user(email, password, realm, full_name, short_name,
|
2017-10-15 18:34:47 +02:00
|
|
|
prereg_user=prereg_user, is_realm_admin=is_realm_admin,
|
2017-01-07 21:46:03 +01:00
|
|
|
tos_version=settings.TOS_VERSION,
|
2017-05-04 15:19:50 +02:00
|
|
|
timezone=timezone,
|
2017-10-12 19:36:14 +02:00
|
|
|
newsletter_data={"IP": request.META['REMOTE_ADDR']},
|
2018-05-22 18:13:51 +02:00
|
|
|
default_stream_groups=default_stream_groups,
|
2018-06-29 09:31:00 +02:00
|
|
|
source_profile=source_profile,
|
|
|
|
realm_creation=realm_creation)
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2017-08-05 08:48:20 +02:00
|
|
|
if realm_creation:
|
2017-11-20 04:14:17 +01:00
|
|
|
bulk_add_subscriptions([realm.signup_notifications_stream], [user_profile])
|
2017-07-17 06:21:53 +02:00
|
|
|
send_initial_realm_messages(realm)
|
2017-06-14 19:55:07 +02:00
|
|
|
|
2017-01-07 21:46:03 +01:00
|
|
|
# Because for realm creation, registration happens on the
|
|
|
|
# root domain, we need to log them into the subdomain for
|
|
|
|
# their new realm.
|
|
|
|
return redirect_and_log_into_subdomain(realm, full_name, email)
|
|
|
|
|
|
|
|
# This dummy_backend check below confirms the user is
|
|
|
|
# authenticating to the correct subdomain.
|
|
|
|
auth_result = authenticate(username=user_profile.email,
|
2017-10-03 02:29:20 +02:00
|
|
|
realm=realm,
|
2017-01-07 21:46:03 +01:00
|
|
|
return_data=return_data,
|
|
|
|
use_dummy_backend=True)
|
|
|
|
if return_data.get('invalid_subdomain'):
|
|
|
|
# By construction, this should never happen.
|
|
|
|
logging.error("Subdomain mismatch in registration %s: %s" % (
|
|
|
|
realm.subdomain, user_profile.email,))
|
|
|
|
return redirect('/')
|
2017-08-23 01:14:45 +02:00
|
|
|
|
2017-10-24 19:37:14 +02:00
|
|
|
return login_and_go_to_home(request, auth_result)
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2017-03-16 14:28:08 +01:00
|
|
|
return render(
|
|
|
|
request,
|
2017-01-07 21:46:03 +01:00
|
|
|
'zerver/register.html',
|
2017-03-16 14:28:08 +01:00
|
|
|
context={'form': form,
|
|
|
|
'email': email,
|
|
|
|
'key': key,
|
|
|
|
'full_name': request.session.get('authenticated_full_name', None),
|
|
|
|
'lock_name': name_validated and name_changes_disabled(realm),
|
|
|
|
# password_auth_enabled is normally set via our context processor,
|
|
|
|
# but for the registration form, there is no logged in user yet, so
|
|
|
|
# we have to set it here.
|
|
|
|
'creating_new_team': realm_creation,
|
2017-08-09 22:09:38 +02:00
|
|
|
'password_required': password_auth_enabled(realm) and password_required,
|
2017-03-16 14:28:08 +01:00
|
|
|
'password_auth_enabled': password_auth_enabled(realm),
|
2017-10-19 07:42:03 +02:00
|
|
|
'root_domain_available': is_root_domain_available(),
|
2017-10-12 19:36:14 +02:00
|
|
|
'default_stream_groups': get_default_stream_groups(realm),
|
2018-06-19 10:55:56 +02:00
|
|
|
'accounts': get_accounts_for_email(email),
|
2017-03-23 00:15:06 +01:00
|
|
|
'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),
|
|
|
|
'MAX_REALM_SUBDOMAIN_LENGTH': str(Realm.MAX_REALM_SUBDOMAIN_LENGTH)
|
2017-03-16 14:28:08 +01:00
|
|
|
}
|
|
|
|
)
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def login_and_go_to_home(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
2017-10-24 19:37:14 +02:00
|
|
|
do_login(request, user_profile)
|
|
|
|
return HttpResponseRedirect(user_profile.realm.uri + reverse('zerver.views.home.home'))
|
|
|
|
|
2018-01-26 20:50:22 +01:00
|
|
|
def prepare_activation_url(email: str, request: HttpRequest,
|
|
|
|
realm_creation: bool=False,
|
|
|
|
streams: Optional[List[Stream]]=None) -> str:
|
2017-01-07 21:46:03 +01:00
|
|
|
"""
|
|
|
|
Send an email with a confirmation link to the provided e-mail so the user
|
|
|
|
can complete their registration.
|
|
|
|
"""
|
|
|
|
prereg_user = create_preregistration_user(email, request, realm_creation)
|
2017-08-10 22:33:14 +02:00
|
|
|
|
|
|
|
if streams is not None:
|
2018-01-31 08:22:07 +01:00
|
|
|
prereg_user.streams.set(streams)
|
2017-08-10 22:33:14 +02:00
|
|
|
|
2017-11-30 01:06:25 +01:00
|
|
|
confirmation_type = Confirmation.USER_REGISTRATION
|
|
|
|
if realm_creation:
|
|
|
|
confirmation_type = Confirmation.REALM_CREATION
|
|
|
|
|
|
|
|
activation_url = create_confirmation_link(prereg_user, request.get_host(), confirmation_type)
|
2017-06-11 00:58:00 +02:00
|
|
|
if settings.DEVELOPMENT and realm_creation:
|
2017-06-11 00:39:58 +02:00
|
|
|
request.session['confirmation_key'] = {'confirmation_key': activation_url.split('/')[-1]}
|
2018-01-26 20:50:22 +01:00
|
|
|
return activation_url
|
|
|
|
|
2018-12-14 08:41:42 +01:00
|
|
|
def send_confirm_registration_email(email: str, activation_url: str, language: str) -> None:
|
2018-12-03 23:26:51 +01:00
|
|
|
send_email('zerver/emails/confirm_registration', to_emails=[email],
|
2018-06-11 08:18:16 +02:00
|
|
|
from_address=FromAddress.tokenized_no_reply_address(),
|
2018-12-14 08:41:42 +01:00
|
|
|
language=language, context={'activate_url': activation_url})
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def redirect_to_email_login_url(email: str) -> HttpResponseRedirect:
|
2017-01-07 21:46:03 +01:00
|
|
|
login_url = reverse('django.contrib.auth.views.login')
|
2017-08-28 08:13:15 +02:00
|
|
|
email = urllib.parse.quote_plus(email)
|
|
|
|
redirect_url = login_url + '?already_registered=' + email
|
2017-01-07 21:46:03 +01:00
|
|
|
return HttpResponseRedirect(redirect_url)
|
|
|
|
|
2018-04-24 03:47:28 +02:00
|
|
|
def create_realm(request: HttpRequest, creation_key: Optional[str]=None) -> HttpResponse:
|
2018-01-29 20:54:49 +01:00
|
|
|
try:
|
|
|
|
key_record = validate_key(creation_key)
|
|
|
|
except RealmCreationKey.Invalid:
|
2018-01-29 20:06:55 +01:00
|
|
|
return render(request, "zerver/realm_creation_failed.html",
|
|
|
|
context={'message': _('The organization creation link has expired'
|
|
|
|
' or is not valid.')})
|
2017-01-07 21:46:03 +01:00
|
|
|
if not settings.OPEN_REALM_CREATION:
|
2018-01-29 20:54:49 +01:00
|
|
|
if key_record is None:
|
2017-03-16 14:28:08 +01:00
|
|
|
return render(request, "zerver/realm_creation_failed.html",
|
2018-08-10 02:16:49 +02:00
|
|
|
context={'message': _('New organization creation disabled')})
|
2017-01-07 21:46:03 +01:00
|
|
|
|
|
|
|
# When settings.OPEN_REALM_CREATION is enabled, anyone can create a new realm,
|
2018-11-09 18:51:12 +01:00
|
|
|
# with a few restrictions on their email address.
|
2017-01-07 21:46:03 +01:00
|
|
|
if request.method == 'POST':
|
|
|
|
form = RealmCreationForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
email = form.cleaned_data['email']
|
2018-01-26 20:50:22 +01:00
|
|
|
activation_url = prepare_activation_url(email, request, realm_creation=True)
|
2018-01-26 21:12:58 +01:00
|
|
|
if key_record is not None and key_record.presume_email_valid:
|
|
|
|
# The user has a token created from the server command line;
|
|
|
|
# skip confirming the email is theirs, taking their word for it.
|
|
|
|
# This is essential on first install if the admin hasn't stopped
|
|
|
|
# to configure outbound email up front, or it isn't working yet.
|
|
|
|
key_record.delete()
|
|
|
|
return HttpResponseRedirect(activation_url)
|
|
|
|
|
2017-08-17 19:59:17 +02:00
|
|
|
try:
|
2018-12-14 08:41:42 +01:00
|
|
|
send_confirm_registration_email(email, activation_url, request.LANGUAGE_CODE)
|
2017-08-17 19:59:17 +02:00
|
|
|
except smtplib.SMTPException as e:
|
|
|
|
logging.error('Error in create_realm: %s' % (str(e),))
|
|
|
|
return HttpResponseRedirect("/config-error/smtp")
|
|
|
|
|
2018-01-29 20:54:49 +01:00
|
|
|
if key_record is not None:
|
|
|
|
key_record.delete()
|
2018-08-24 10:01:42 +02:00
|
|
|
return HttpResponseRedirect(reverse('new_realm_send_confirm', kwargs={'email': email}))
|
2017-01-07 21:46:03 +01:00
|
|
|
else:
|
|
|
|
form = RealmCreationForm()
|
2017-03-16 14:28:08 +01:00
|
|
|
return render(request,
|
|
|
|
'zerver/create_realm.html',
|
|
|
|
context={'form': form, 'current_url': request.get_full_path},
|
|
|
|
)
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2017-11-01 22:14:19 +01:00
|
|
|
# This is used only by the casper test in 00-realm-creation.js.
|
2017-11-27 09:28:57 +01:00
|
|
|
def confirmation_key(request: HttpRequest) -> HttpResponse:
|
2017-01-07 21:46:03 +01:00
|
|
|
return json_success(request.session.get('confirmation_key'))
|
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def accounts_home(request: HttpRequest, multiuse_object: Optional[MultiuseInvite]=None) -> HttpResponse:
|
2017-10-02 08:32:09 +02:00
|
|
|
realm = get_realm(get_subdomain(request))
|
2017-11-23 02:59:51 +01:00
|
|
|
|
|
|
|
if realm is None:
|
|
|
|
return HttpResponseRedirect(reverse('zerver.views.registration.find_account'))
|
|
|
|
if realm.deactivated:
|
2017-08-24 09:58:44 +02:00
|
|
|
return redirect_to_deactivation_notice()
|
|
|
|
|
2017-08-10 22:34:17 +02:00
|
|
|
from_multiuse_invite = False
|
|
|
|
streams_to_subscribe = None
|
|
|
|
|
|
|
|
if multiuse_object:
|
|
|
|
realm = multiuse_object.realm
|
|
|
|
streams_to_subscribe = multiuse_object.streams.all()
|
|
|
|
from_multiuse_invite = True
|
|
|
|
|
2017-01-07 21:46:03 +01:00
|
|
|
if request.method == 'POST':
|
2017-08-10 22:34:17 +02:00
|
|
|
form = HomepageForm(request.POST, realm=realm, from_multiuse_invite=from_multiuse_invite)
|
2017-01-07 21:46:03 +01:00
|
|
|
if form.is_valid():
|
|
|
|
email = form.cleaned_data['email']
|
2018-01-26 20:50:22 +01:00
|
|
|
activation_url = prepare_activation_url(email, request, streams=streams_to_subscribe)
|
2017-08-17 18:28:21 +02:00
|
|
|
try:
|
2018-12-14 08:41:42 +01:00
|
|
|
send_confirm_registration_email(email, activation_url, request.LANGUAGE_CODE)
|
2017-08-17 18:28:21 +02:00
|
|
|
except smtplib.SMTPException as e:
|
|
|
|
logging.error('Error in accounts_home: %s' % (str(e),))
|
|
|
|
return HttpResponseRedirect("/config-error/smtp")
|
|
|
|
|
2018-08-24 10:01:42 +02:00
|
|
|
return HttpResponseRedirect(reverse('signup_send_confirm', kwargs={'email': email}))
|
2017-08-25 07:05:27 +02:00
|
|
|
|
|
|
|
email = request.POST['email']
|
2017-01-07 21:46:03 +01:00
|
|
|
try:
|
2017-08-25 07:05:27 +02:00
|
|
|
validate_email_for_realm(realm, email)
|
2017-01-07 21:46:03 +01:00
|
|
|
except ValidationError:
|
|
|
|
return redirect_to_email_login_url(email)
|
|
|
|
else:
|
|
|
|
form = HomepageForm(realm=realm)
|
2017-03-16 14:28:08 +01:00
|
|
|
return render(request,
|
|
|
|
'zerver/accounts_home.html',
|
2017-08-10 22:34:17 +02:00
|
|
|
context={'form': form, 'current_url': request.get_full_path,
|
|
|
|
'from_multiuse_invite': from_multiuse_invite},
|
2017-03-16 14:28:08 +01:00
|
|
|
)
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def accounts_home_from_multiuse_invite(request: HttpRequest, confirmation_key: str) -> HttpResponse:
|
2017-08-10 22:34:17 +02:00
|
|
|
multiuse_object = None
|
|
|
|
try:
|
2017-11-01 21:07:39 +01:00
|
|
|
multiuse_object = get_object_from_key(confirmation_key, Confirmation.MULTIUSE_INVITE)
|
2017-09-27 03:34:58 +02:00
|
|
|
# Required for oAuth2
|
|
|
|
request.session["multiuse_object_key"] = confirmation_key
|
2017-08-10 22:34:17 +02:00
|
|
|
except ConfirmationKeyException as exception:
|
|
|
|
realm = get_realm_from_request(request)
|
|
|
|
if realm is None or realm.invite_required:
|
|
|
|
return render_confirmation_key_error(request, exception)
|
|
|
|
return accounts_home(request, multiuse_object=multiuse_object)
|
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def generate_204(request: HttpRequest) -> HttpResponse:
|
2017-01-07 21:46:03 +01:00
|
|
|
return HttpResponse(content=None, status=204)
|
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def find_account(request: HttpRequest) -> HttpResponse:
|
2018-12-19 20:53:14 +01:00
|
|
|
from zerver.context_processors import common_context
|
2017-08-28 23:27:16 +02:00
|
|
|
url = reverse('zerver.views.registration.find_account')
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2018-04-24 03:47:28 +02:00
|
|
|
emails = [] # type: List[str]
|
2017-01-07 21:46:03 +01:00
|
|
|
if request.method == 'POST':
|
|
|
|
form = FindMyTeamForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
emails = form.cleaned_data['emails']
|
2018-12-19 20:53:14 +01:00
|
|
|
for user in UserProfile.objects.filter(
|
2017-08-25 08:14:55 +02:00
|
|
|
email__in=emails, is_active=True, is_bot=False, realm__deactivated=False):
|
2018-12-19 20:53:14 +01:00
|
|
|
context = common_context(user)
|
|
|
|
context.update({
|
|
|
|
'email': user.email,
|
|
|
|
})
|
|
|
|
send_email('zerver/emails/find_team', to_user_ids=[user.id], context=context)
|
2017-01-07 21:46:03 +01:00
|
|
|
|
|
|
|
# Note: Show all the emails in the result otherwise this
|
|
|
|
# feature can be used to ascertain which email addresses
|
|
|
|
# are associated with Zulip.
|
|
|
|
data = urllib.parse.urlencode({'emails': ','.join(emails)})
|
|
|
|
return redirect(url + "?" + data)
|
|
|
|
else:
|
|
|
|
form = FindMyTeamForm()
|
|
|
|
result = request.GET.get('emails')
|
2017-08-25 08:30:33 +02:00
|
|
|
# The below validation is perhaps unnecessary, in that we
|
|
|
|
# shouldn't get able to get here with an invalid email unless
|
|
|
|
# the user hand-edits the URLs.
|
2017-01-07 21:46:03 +01:00
|
|
|
if result:
|
|
|
|
for email in result.split(','):
|
|
|
|
try:
|
|
|
|
validators.validate_email(email)
|
|
|
|
emails.append(email)
|
|
|
|
except ValidationError:
|
|
|
|
pass
|
|
|
|
|
2017-03-16 14:28:08 +01:00
|
|
|
return render(request,
|
2017-08-28 23:27:16 +02:00
|
|
|
'zerver/find_account.html',
|
2017-03-16 14:28:08 +01:00
|
|
|
context={'form': form, 'current_url': lambda: url,
|
|
|
|
'emails': emails},)
|
2018-08-25 14:06:17 +02:00
|
|
|
|
|
|
|
def realm_redirect(request: HttpRequest) -> HttpResponse:
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = RealmRedirectForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
subdomain = form.cleaned_data['subdomain']
|
|
|
|
realm = get_realm(subdomain)
|
|
|
|
redirect_to = get_safe_redirect_to(request.GET.get("next", ""), realm.uri)
|
|
|
|
return HttpResponseRedirect(redirect_to)
|
|
|
|
else:
|
|
|
|
form = RealmRedirectForm()
|
|
|
|
|
|
|
|
return render(request, 'zerver/realm_redirect.html', context={'form': form})
|