2017-01-07 21:46:03 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-07-17 06:44:59 +02:00
|
|
|
from typing import Any, List, Dict, Mapping, Optional, Text
|
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
|
2017-01-07 21:46:03 +01:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
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, \
|
2017-11-28 03:01:44 +01:00
|
|
|
get_realm, get_user, get_default_stream_groups
|
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, \
|
2017-11-22 20:05:53 +01:00
|
|
|
email_not_system_bot, compute_mit_user_fullname, 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, \
|
|
|
|
CreateUserForm, FindMyTeamForm
|
|
|
|
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, \
|
2017-11-20 04:14:17 +01:00
|
|
|
send_initial_realm_messages
|
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
|
2017-10-27 00:27:59 +02:00
|
|
|
from zerver.views.auth import create_preregistration_user, \
|
|
|
|
redirect_and_log_into_subdomain, \
|
|
|
|
redirect_to_deactivation_notice
|
2017-10-24 19:38:31 +02:00
|
|
|
from zproject.backends import ldap_auth_enabled, password_auth_enabled, ZulipLDAPAuthBackend
|
2017-01-07 21:46:03 +01:00
|
|
|
|
2017-08-10 22:34:17 +02:00
|
|
|
from confirmation.models import Confirmation, RealmCreationKey, ConfirmationKeyException, \
|
|
|
|
check_key_is_valid, create_confirmation_link, get_object_from_key, \
|
|
|
|
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
|
2017-10-15 18:34:47 +02:00
|
|
|
is_realm_admin = prereg_user.invited_as_admin or realm_creation
|
2017-01-07 21:46:03 +01:00
|
|
|
|
|
|
|
validators.validate_email(email)
|
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
|
|
|
|
2017-12-02 01:13:17 +01:00
|
|
|
if not email_allowed_for_realm(email, realm):
|
|
|
|
return render(request, "zerver/closed_realm.html",
|
|
|
|
context={"closed_domain_name": realm.name})
|
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):
|
|
|
|
ldap_attrs = _LDAPUser(backend, backend.django_to_ldap_username(email)).attrs
|
|
|
|
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)
|
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-05-04 15:19:50 +02:00
|
|
|
timezone = u""
|
|
|
|
if 'timezone' in request.POST and request.POST['timezone'] in get_all_timezones():
|
|
|
|
timezone = request.POST['timezone']
|
|
|
|
|
2017-11-28 03:01:44 +01:00
|
|
|
if not realm_creation:
|
|
|
|
try:
|
|
|
|
existing_user_profile = get_user(email, realm)
|
|
|
|
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)
|
|
|
|
if auth_result is None:
|
|
|
|
# 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))
|
|
|
|
|
|
|
|
# Since we'll have created a user, we now just log them in.
|
|
|
|
return login_and_go_to_home(request, auth_result)
|
|
|
|
elif 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']},
|
2017-10-26 20:31:43 +02:00
|
|
|
default_stream_groups=default_stream_groups)
|
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),
|
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
|
|
|
|
|
|
|
# Mark the user as having been just created, so no "new login" email is sent
|
|
|
|
user_profile.just_registered = True
|
|
|
|
do_login(request, user_profile)
|
|
|
|
return HttpResponseRedirect(user_profile.realm.uri + reverse('zerver.views.home.home'))
|
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def send_registration_completion_email(email: str, request: HttpRequest,
|
|
|
|
realm_creation: bool=False,
|
|
|
|
streams: Optional[List[Stream]]=None) -> None:
|
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:
|
|
|
|
prereg_user.streams = streams
|
|
|
|
prereg_user.save()
|
|
|
|
|
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-07-11 05:01:32 +02:00
|
|
|
send_email('zerver/emails/confirm_registration', to_email=email, from_address=FromAddress.NOREPLY,
|
2017-06-11 00:39:58 +02:00
|
|
|
context={'activate_url': activation_url})
|
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]}
|
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)
|
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def create_realm(request: HttpRequest, creation_key: Optional[Text]=None) -> HttpResponse:
|
2017-01-07 21:46:03 +01:00
|
|
|
if not settings.OPEN_REALM_CREATION:
|
|
|
|
if creation_key is None:
|
2017-03-16 14:28:08 +01:00
|
|
|
return render(request, "zerver/realm_creation_failed.html",
|
|
|
|
context={'message': _('New organization creation disabled.')})
|
2017-01-07 21:46:03 +01:00
|
|
|
elif not check_key_is_valid(creation_key):
|
2017-03-16 14:28:08 +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
|
|
|
|
|
|
|
# When settings.OPEN_REALM_CREATION is enabled, anyone can create a new realm,
|
|
|
|
# subject to a few restrictions on their email address.
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = RealmCreationForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
email = form.cleaned_data['email']
|
2017-08-17 19:59:17 +02:00
|
|
|
try:
|
|
|
|
send_registration_completion_email(email, request, realm_creation=True)
|
|
|
|
except smtplib.SMTPException as e:
|
|
|
|
logging.error('Error in create_realm: %s' % (str(e),))
|
|
|
|
return HttpResponseRedirect("/config-error/smtp")
|
|
|
|
|
2017-01-07 21:46:03 +01:00
|
|
|
if (creation_key is not None and check_key_is_valid(creation_key)):
|
|
|
|
RealmCreationKey.objects.get(creation_key=creation_key).delete()
|
|
|
|
return HttpResponseRedirect(reverse('send_confirm', kwargs={'email': email}))
|
|
|
|
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']
|
2017-08-17 18:28:21 +02:00
|
|
|
try:
|
2017-08-10 22:34:17 +02:00
|
|
|
send_registration_completion_email(email, request, streams=streams_to_subscribe)
|
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")
|
|
|
|
|
2017-01-07 21:46:03 +01:00
|
|
|
return HttpResponseRedirect(reverse('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:
|
2017-08-28 23:27:16 +02:00
|
|
|
url = reverse('zerver.views.registration.find_account')
|
2017-01-07 21:46:03 +01:00
|
|
|
|
|
|
|
emails = [] # type: List[Text]
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = FindMyTeamForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
emails = form.cleaned_data['emails']
|
2017-08-25 08:14:55 +02:00
|
|
|
for user_profile in UserProfile.objects.filter(
|
|
|
|
email__in=emails, is_active=True, is_bot=False, realm__deactivated=False):
|
2017-07-11 05:01:32 +02:00
|
|
|
send_email('zerver/emails/find_team', to_user_id=user_profile.id,
|
|
|
|
context={'user_profile': user_profile})
|
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},)
|