2016-06-23 11:32:45 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2016-07-03 19:13:25 +02:00
|
|
|
from typing import Any, List, Dict, Optional, Callable, Tuple, Iterable, Sequence
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2016-06-23 11:32:45 +02:00
|
|
|
from django.utils import translation
|
2016-05-25 15:02:02 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
2012-09-19 19:39:34 +02:00
|
|
|
from django.conf import settings
|
2013-11-21 01:30:20 +01:00
|
|
|
from django.contrib.auth import authenticate, login, get_backends
|
2012-08-28 18:44:51 +02:00
|
|
|
from django.core.urlresolvers import reverse
|
2016-06-05 04:58:33 +02:00
|
|
|
from django.http import HttpResponseRedirect, HttpResponseForbidden, HttpResponse, HttpRequest
|
2016-04-21 08:48:33 +02:00
|
|
|
from django.shortcuts import redirect
|
2013-01-08 23:26:40 +01:00
|
|
|
from django.template import RequestContext, loader
|
2013-11-04 17:22:58 +01:00
|
|
|
from django.utils.timezone import now
|
2013-08-16 22:55:50 +02:00
|
|
|
from django.utils.cache import patch_cache_control
|
2012-09-29 00:49:34 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2012-12-11 23:42:32 +01:00
|
|
|
from django.core import validators
|
2013-03-05 23:45:02 +01:00
|
|
|
from django.contrib.auth.views import login as django_login_page, \
|
|
|
|
logout_then_login as django_logout_then_login
|
2015-08-18 20:32:56 +02:00
|
|
|
from django.forms.models import model_to_dict
|
2015-08-21 01:59:39 +02:00
|
|
|
from django.core.mail import send_mail
|
2015-01-29 08:59:41 +01:00
|
|
|
from django.middleware.csrf import get_token
|
2015-08-18 20:32:56 +02:00
|
|
|
from zerver.models import Message, UserProfile, Stream, Subscription, Huddle, \
|
|
|
|
Recipient, Realm, UserMessage, DefaultStream, RealmEmoji, RealmAlias, \
|
2015-11-23 14:35:16 +01:00
|
|
|
RealmFilter, \
|
2016-07-27 02:56:36 +02:00
|
|
|
PreregistrationUser, get_client, UserActivity, \
|
2015-11-23 14:35:16 +01:00
|
|
|
get_stream, UserPresence, get_recipient, \
|
2013-12-12 18:36:32 +01:00
|
|
|
split_email_to_domain, resolve_email_to_domain, email_to_username, get_realm, \
|
2016-06-10 19:46:53 +02:00
|
|
|
completely_open, get_unique_open_realm, remote_user_to_email, email_allowed_for_realm, \
|
|
|
|
get_cross_realm_users
|
2015-11-23 17:09:21 +01:00
|
|
|
from zerver.lib.actions import do_change_password, do_change_full_name, do_change_is_admin, \
|
2016-06-03 01:02:58 +02:00
|
|
|
do_activate_user, do_create_user, do_create_realm, set_default_streams, \
|
2015-11-24 05:26:33 +01:00
|
|
|
internal_send_message, update_user_presence, do_events_register, \
|
2016-07-26 23:26:39 +02:00
|
|
|
do_change_enable_offline_email_notifications, \
|
2016-07-26 23:16:20 +02:00
|
|
|
do_change_enable_digest_emails, \
|
2016-06-21 21:34:41 +02:00
|
|
|
get_default_subs, user_email_is_unique, do_invite_users, do_refer_friend, \
|
|
|
|
compute_mit_user_fullname, do_set_muted_topics, clear_followup_emails_queue, \
|
|
|
|
do_update_pointer, realm_user_count
|
2013-10-16 17:24:52 +02:00
|
|
|
from zerver.lib.push_notifications import num_push_devices_for_user
|
2016-06-03 01:02:58 +02:00
|
|
|
from zerver.forms import RegistrationForm, HomepageForm, RealmCreationForm, ToSForm, \
|
2016-07-22 13:44:53 +02:00
|
|
|
CreateUserForm, OurAuthenticationForm
|
|
|
|
from zerver.lib.actions import is_inactive
|
2013-12-12 23:15:19 +01:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2013-11-21 01:30:20 +01:00
|
|
|
from django_auth_ldap.backend import LDAPBackend, _LDAPUser
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib import bugdown
|
2015-11-23 17:29:37 +01:00
|
|
|
from zerver.lib.validator import check_string, check_list, check_bool
|
2015-11-23 14:35:16 +01:00
|
|
|
from zerver.decorator import require_post, authenticated_json_post_view, \
|
2014-02-13 23:47:57 +01:00
|
|
|
has_request_variables, authenticated_json_view, to_non_negative_int, \
|
2016-07-26 23:16:20 +02:00
|
|
|
JsonableError, get_user_profile_by_email, REQ, \
|
2016-04-21 23:48:34 +02:00
|
|
|
zulip_login_required
|
2015-11-24 05:26:33 +01:00
|
|
|
from zerver.lib.avatar import avatar_url
|
2015-11-23 14:35:16 +01:00
|
|
|
from zerver.lib.response import json_success, json_error
|
2015-11-23 17:29:37 +01:00
|
|
|
from zerver.lib.utils import statsd, generate_random_token
|
2016-07-05 02:18:40 +02:00
|
|
|
from zerver.lib.str_utils import force_str
|
2016-06-21 03:32:23 +02:00
|
|
|
from zproject.backends import password_auth_enabled, dev_auth_enabled, google_auth_enabled
|
2013-01-23 23:24:44 +01:00
|
|
|
|
2016-06-22 21:16:02 +02:00
|
|
|
from confirmation.models import Confirmation, RealmCreationKey, check_key_is_valid
|
2012-09-28 22:47:05 +02:00
|
|
|
|
2015-01-29 08:59:41 +01:00
|
|
|
import requests
|
|
|
|
|
2013-07-18 17:22:24 +02:00
|
|
|
import subprocess
|
2013-08-09 20:26:35 +02:00
|
|
|
import calendar
|
2012-08-28 18:44:51 +02:00
|
|
|
import datetime
|
2013-06-18 23:55:55 +02:00
|
|
|
import ujson
|
2012-08-28 18:44:51 +02:00
|
|
|
import simplejson
|
2012-09-07 19:20:04 +02:00
|
|
|
import re
|
2016-06-05 04:58:33 +02:00
|
|
|
from six import text_type
|
2016-01-24 03:39:44 +01:00
|
|
|
from six.moves import urllib
|
2012-10-26 22:02:51 +02:00
|
|
|
import base64
|
2013-05-06 17:14:59 +02:00
|
|
|
import time
|
2013-05-20 19:09:18 +02:00
|
|
|
import logging
|
2014-03-28 00:49:20 +01:00
|
|
|
import jwt
|
2015-01-29 08:59:41 +01:00
|
|
|
import hashlib
|
|
|
|
import hmac
|
2016-06-23 11:32:45 +02:00
|
|
|
import os
|
2012-10-16 21:15:01 +02:00
|
|
|
|
2016-04-21 08:48:33 +02:00
|
|
|
from zproject.jinja2 import render_to_response
|
2013-10-17 19:21:18 +02:00
|
|
|
|
2014-03-27 23:50:37 +01:00
|
|
|
def name_changes_disabled(realm):
|
2016-06-11 00:21:23 +02:00
|
|
|
# type: (Optional[Realm]) -> bool
|
|
|
|
if realm is None:
|
|
|
|
return settings.NAME_CHANGES_DISABLED
|
2015-08-20 23:05:56 +02:00
|
|
|
return settings.NAME_CHANGES_DISABLED or realm.name_changes_disabled
|
2014-03-27 23:50:37 +01:00
|
|
|
|
2012-09-28 22:47:05 +02:00
|
|
|
@require_post
|
2012-10-16 21:42:40 +02:00
|
|
|
def accounts_register(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2012-09-28 22:47:05 +02:00
|
|
|
key = request.POST['key']
|
2012-10-29 19:08:18 +01:00
|
|
|
confirmation = Confirmation.objects.get(confirmation_key=key)
|
2012-12-11 23:42:32 +01:00
|
|
|
prereg_user = confirmation.content_object
|
|
|
|
email = prereg_user.email
|
2016-06-03 01:02:58 +02:00
|
|
|
realm_creation = prereg_user.realm_creation
|
2014-01-07 19:13:45 +01:00
|
|
|
try:
|
|
|
|
existing_user_profile = get_user_profile_by_email(email)
|
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
existing_user_profile = None
|
2012-10-29 19:08:18 +01:00
|
|
|
|
2013-07-18 18:48:56 +02:00
|
|
|
validators.validate_email(email)
|
2016-06-03 01:02:58 +02:00
|
|
|
# If OPEN_REALM_CREATION is enabled all user sign ups should go through the
|
|
|
|
# special URL with domain name so that REALM can be identified if multiple realms exist
|
2015-12-28 21:37:24 +01:00
|
|
|
unique_open_realm = get_unique_open_realm()
|
2016-06-03 01:02:58 +02:00
|
|
|
if unique_open_realm is not None:
|
2015-12-28 21:37:24 +01:00
|
|
|
realm = unique_open_realm
|
|
|
|
domain = realm.domain
|
2016-07-27 02:56:36 +02:00
|
|
|
elif prereg_user.referred_by:
|
2015-12-28 21:37:24 +01:00
|
|
|
# If someone invited you, you are joining their realm regardless
|
|
|
|
# of your e-mail address.
|
2015-08-20 02:38:32 +02:00
|
|
|
realm = prereg_user.referred_by.realm
|
|
|
|
domain = realm.domain
|
2016-01-12 16:24:34 +01:00
|
|
|
if not email_allowed_for_realm(email, realm):
|
2015-08-20 02:38:32 +02:00
|
|
|
return render_to_response("zerver/closed_realm.html", {"closed_domain_name": realm.name})
|
2016-07-27 02:56:36 +02:00
|
|
|
elif prereg_user.realm:
|
2013-08-02 20:32:56 +02:00
|
|
|
# You have a realm set, even though nobody referred you. This
|
|
|
|
# happens if you sign up through a special URL for an open
|
|
|
|
# realm.
|
|
|
|
domain = prereg_user.realm.domain
|
2015-12-28 21:37:24 +01:00
|
|
|
realm = get_realm(domain)
|
2013-02-06 22:26:04 +01:00
|
|
|
else:
|
2013-11-22 23:48:00 +01:00
|
|
|
domain = resolve_email_to_domain(email)
|
2015-12-28 21:37:24 +01:00
|
|
|
realm = get_realm(domain)
|
2012-09-25 22:58:59 +02:00
|
|
|
|
2014-01-07 19:04:26 +01:00
|
|
|
if realm and realm.deactivated:
|
|
|
|
# The user is trying to register for a deactivated realm. Advise them to
|
|
|
|
# contact support.
|
|
|
|
return render_to_response("zerver/deactivated.html",
|
2015-09-30 02:58:50 +02:00
|
|
|
{"deactivated_domain_name": realm.name,
|
|
|
|
"zulip_administrator": settings.ZULIP_ADMINISTRATOR})
|
2014-01-07 19:04:26 +01:00
|
|
|
|
2012-09-29 00:49:34 +02:00
|
|
|
try:
|
2014-01-07 19:13:45 +01:00
|
|
|
if existing_user_profile is not None and existing_user_profile.is_mirror_dummy:
|
|
|
|
# Mirror dummy users to be activated must be inactive
|
2013-02-12 20:52:42 +01:00
|
|
|
is_inactive(email)
|
2012-10-29 19:08:18 +01:00
|
|
|
else:
|
|
|
|
# Other users should not already exist at all.
|
2013-07-08 17:57:04 +02:00
|
|
|
user_email_is_unique(email)
|
2012-09-29 00:49:34 +02:00
|
|
|
except ValidationError:
|
2016-07-08 18:57:01 +02:00
|
|
|
return HttpResponseRedirect(reverse('django.contrib.auth.views.login') + '?email=' +
|
|
|
|
urllib.parse.quote_plus(email))
|
2012-09-29 00:49:34 +02:00
|
|
|
|
2013-11-21 04:57:23 +01:00
|
|
|
name_validated = False
|
|
|
|
full_name = None
|
|
|
|
|
2012-10-02 22:20:07 +02:00
|
|
|
if request.POST.get('from_confirmation'):
|
2013-11-21 04:57:23 +01:00
|
|
|
try:
|
|
|
|
del request.session['authenticated_full_name']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2016-07-27 01:45:29 +02:00
|
|
|
if realm is not None and realm.is_zephyr_mirror_realm and domain == "mit.edu":
|
|
|
|
# for MIT users, we can get an authoritative name from Hesiod
|
2013-08-15 18:43:42 +02:00
|
|
|
hesiod_name = compute_mit_user_fullname(email)
|
|
|
|
form = RegistrationForm(
|
|
|
|
initial={'full_name': hesiod_name if "@" not in hesiod_name else ""})
|
2013-11-21 04:57:23 +01:00
|
|
|
name_validated = True
|
2013-11-21 01:30:20 +01:00
|
|
|
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
|
2013-11-21 04:57:23 +01:00
|
|
|
try:
|
2016-07-17 19:40:12 +02:00
|
|
|
ldap_full_name = ldap_attrs[settings.AUTH_LDAP_USER_ATTR_MAP['full_name']][0]
|
|
|
|
request.session['authenticated_full_name'] = ldap_full_name
|
2013-11-21 04:57:23 +01:00
|
|
|
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.
|
2016-07-17 19:40:12 +02:00
|
|
|
form = RegistrationForm({'full_name': ldap_full_name})
|
2013-11-21 04:57:23 +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
|
|
|
|
form = RegistrationForm()
|
2015-01-29 09:00:23 +01:00
|
|
|
elif 'full_name' in request.POST:
|
|
|
|
form = RegistrationForm(
|
|
|
|
initial={'full_name': request.POST.get('full_name')}
|
|
|
|
)
|
2013-08-15 18:43:42 +02:00
|
|
|
else:
|
|
|
|
form = RegistrationForm()
|
2012-10-02 22:20:07 +02:00
|
|
|
else:
|
2013-11-21 04:57:23 +01:00
|
|
|
postdata = request.POST.copy()
|
2014-03-27 23:50:37 +01:00
|
|
|
if name_changes_disabled(realm):
|
2013-11-21 04:57:23 +01:00
|
|
|
# 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
|
|
|
|
form = RegistrationForm(postdata)
|
2014-03-28 00:45:03 +01:00
|
|
|
if not password_auth_enabled(realm):
|
|
|
|
form['password'].field.required = False
|
2013-11-21 04:57:23 +01:00
|
|
|
|
|
|
|
if form.is_valid():
|
2014-03-28 00:45:03 +01:00
|
|
|
if password_auth_enabled(realm):
|
2013-11-21 04:57:23 +01:00
|
|
|
password = form.cleaned_data['password']
|
|
|
|
else:
|
|
|
|
# SSO users don't need no passwords
|
|
|
|
password = None
|
|
|
|
|
2016-06-03 01:02:58 +02:00
|
|
|
if realm_creation:
|
|
|
|
domain = split_email_to_domain(email)
|
2016-06-25 02:47:16 +02:00
|
|
|
realm = do_create_realm(domain, form.cleaned_data['realm_name'])[0]
|
2016-06-03 01:02:58 +02:00
|
|
|
set_default_streams(realm, settings.DEFAULT_NEW_REALM_STREAMS)
|
|
|
|
|
2013-11-21 04:57:23 +01:00
|
|
|
full_name = form.cleaned_data['full_name']
|
|
|
|
short_name = email_to_username(email)
|
|
|
|
first_in_realm = len(UserProfile.objects.filter(realm=realm, is_bot=False)) == 0
|
|
|
|
|
|
|
|
# FIXME: sanitize email addresses and fullname
|
2014-01-07 19:13:45 +01:00
|
|
|
if existing_user_profile is not None and existing_user_profile.is_mirror_dummy:
|
2013-11-21 04:57:23 +01:00
|
|
|
try:
|
2014-01-07 19:13:45 +01:00
|
|
|
user_profile = existing_user_profile
|
2013-12-09 22:37:04 +01:00
|
|
|
do_activate_user(user_profile)
|
|
|
|
do_change_password(user_profile, password)
|
|
|
|
do_change_full_name(user_profile, full_name)
|
2013-11-21 04:57:23 +01:00
|
|
|
except UserProfile.DoesNotExist:
|
2015-10-13 21:33:54 +02:00
|
|
|
user_profile = do_create_user(email, password, realm, full_name, short_name,
|
|
|
|
prereg_user=prereg_user,
|
|
|
|
newsletter_data={"IP": request.META['REMOTE_ADDR']})
|
2013-11-21 04:57:23 +01:00
|
|
|
else:
|
2015-10-13 21:33:54 +02:00
|
|
|
user_profile = do_create_user(email, password, realm, full_name, short_name,
|
|
|
|
prereg_user=prereg_user,
|
|
|
|
newsletter_data={"IP": request.META['REMOTE_ADDR']})
|
2012-11-17 03:57:46 +01:00
|
|
|
|
2013-11-21 04:57:23 +01:00
|
|
|
# This logs you in using the ZulipDummyBackend, since honestly nothing
|
|
|
|
# more fancy than this is required.
|
2013-12-09 23:30:28 +01:00
|
|
|
login(request, authenticate(username=user_profile.email, use_dummy_backend=True))
|
2013-07-11 21:40:52 +02:00
|
|
|
|
2013-11-21 04:57:23 +01:00
|
|
|
if first_in_realm:
|
2014-01-22 20:04:34 +01:00
|
|
|
do_change_is_admin(user_profile, True)
|
2013-11-21 04:57:23 +01:00
|
|
|
return HttpResponseRedirect(reverse('zerver.views.initial_invite_page'))
|
|
|
|
else:
|
|
|
|
return HttpResponseRedirect(reverse('zerver.views.home'))
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
return render_to_response('zerver/register.html',
|
2013-04-23 23:46:12 +02:00
|
|
|
{'form': form,
|
|
|
|
'company_name': domain,
|
|
|
|
'email': email,
|
|
|
|
'key': key,
|
2013-11-21 04:57:23 +01:00
|
|
|
'full_name': request.session.get('authenticated_full_name', None),
|
2014-03-28 00:45:03 +01:00
|
|
|
'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.
|
2016-06-03 01:02:58 +02:00
|
|
|
'creating_new_team': realm_creation,
|
2014-03-28 00:45:03 +01:00
|
|
|
'password_auth_enabled': password_auth_enabled(realm),
|
2013-04-23 23:46:12 +02:00
|
|
|
},
|
2016-04-21 08:48:33 +02:00
|
|
|
request=request)
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2016-04-21 23:48:34 +02:00
|
|
|
@zulip_login_required
|
2013-01-08 23:26:40 +01:00
|
|
|
def accounts_accept_terms(request):
|
2016-07-30 05:20:05 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2013-04-23 23:46:12 +02:00
|
|
|
email = request.user.email
|
2013-11-22 23:48:00 +01:00
|
|
|
domain = resolve_email_to_domain(email)
|
2013-01-08 23:26:40 +01:00
|
|
|
if request.method == "POST":
|
|
|
|
form = ToSForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
full_name = form.cleaned_data['full_name']
|
|
|
|
send_mail('Terms acceptance for ' + full_name,
|
2013-07-29 23:03:31 +02:00
|
|
|
loader.render_to_string('zerver/tos_accept_body.txt',
|
2013-01-08 23:26:40 +01:00
|
|
|
{'name': full_name,
|
|
|
|
'email': email,
|
|
|
|
'ip': request.META['REMOTE_ADDR'],
|
2015-12-15 03:47:39 +01:00
|
|
|
'browser': request.META.get('HTTP_USER_AGENT', "Unspecified")}),
|
2013-10-04 19:59:25 +02:00
|
|
|
settings.EMAIL_HOST_USER,
|
2013-07-24 23:41:24 +02:00
|
|
|
["all@zulip.com"])
|
2013-03-29 17:39:53 +01:00
|
|
|
do_change_full_name(request.user, full_name)
|
2013-01-08 23:26:40 +01:00
|
|
|
return redirect(home)
|
|
|
|
|
|
|
|
else:
|
|
|
|
form = ToSForm()
|
2013-07-29 23:03:31 +02:00
|
|
|
return render_to_response('zerver/accounts_accept_terms.html',
|
2013-07-18 18:48:56 +02:00
|
|
|
{ 'form': form, 'company_name': domain, 'email': email },
|
2016-04-21 08:48:33 +02:00
|
|
|
request=request)
|
2013-01-08 23:26:40 +01:00
|
|
|
|
2013-08-23 20:49:06 +02:00
|
|
|
from zerver.lib.ccache import make_ccache
|
|
|
|
|
|
|
|
@authenticated_json_view
|
|
|
|
@has_request_variables
|
|
|
|
def webathena_kerberos_login(request, user_profile,
|
|
|
|
cred=REQ(default=None)):
|
2016-07-30 00:42:48 +02:00
|
|
|
# type: (HttpRequest, UserProfile, text_type) -> HttpResponse
|
2013-08-23 20:49:06 +02:00
|
|
|
if cred is None:
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("Could not find Kerberos credential"))
|
2016-07-27 01:45:29 +02:00
|
|
|
if not user_profile.realm.webathena_enabled:
|
|
|
|
return json_error(_("Webathena login not enabled"))
|
2013-08-23 20:49:06 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
parsed_cred = ujson.loads(cred)
|
|
|
|
user = parsed_cred["cname"]["nameString"][0]
|
2013-08-28 21:58:34 +02:00
|
|
|
if user == "golem":
|
|
|
|
# Hack for an mit.edu user whose Kerberos username doesn't
|
|
|
|
# match what he zephyrs as
|
|
|
|
user = "ctl"
|
2013-08-23 20:49:06 +02:00
|
|
|
assert(user == user_profile.email.split("@")[0])
|
|
|
|
ccache = make_ccache(parsed_cred)
|
|
|
|
except Exception:
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("Invalid Kerberos cache"))
|
2013-08-23 20:49:06 +02:00
|
|
|
|
|
|
|
# TODO: Send these data via (say) rabbitmq
|
|
|
|
try:
|
2013-10-04 20:23:48 +02:00
|
|
|
subprocess.check_call(["ssh", "zulip@zmirror2.zulip.net", "--",
|
2013-10-04 19:19:57 +02:00
|
|
|
"/home/zulip/zulip/bots/process_ccache",
|
2016-07-30 00:42:48 +02:00
|
|
|
force_str(user),
|
|
|
|
force_str(user_profile.api_key),
|
2016-07-05 02:18:40 +02:00
|
|
|
force_str(base64.b64encode(ccache))])
|
2013-08-23 20:49:06 +02:00
|
|
|
except Exception:
|
|
|
|
logging.exception("Error updating the user's ccache")
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("We were unable to setup mirroring for you"))
|
2013-08-23 20:49:06 +02:00
|
|
|
|
|
|
|
return json_success()
|
|
|
|
|
2013-04-17 17:24:07 +02:00
|
|
|
def api_endpoint_docs(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2013-07-29 23:03:31 +02:00
|
|
|
raw_calls = open('templates/zerver/api_content.json', 'r').read()
|
2013-06-18 23:55:55 +02:00
|
|
|
calls = ujson.loads(raw_calls)
|
2013-04-09 22:20:46 +02:00
|
|
|
langs = set()
|
|
|
|
for call in calls:
|
2014-01-03 18:37:37 +01:00
|
|
|
call["endpoint"] = "%s/v1/%s" % (settings.EXTERNAL_API_URI, call["endpoint"])
|
2016-07-08 18:57:01 +02:00
|
|
|
call["example_request"]["curl"] = call["example_request"]["curl"].replace("https://api.zulip.com",
|
|
|
|
settings.EXTERNAL_API_URI)
|
2013-08-08 17:45:25 +02:00
|
|
|
response = call['example_response']
|
2016-05-10 01:55:43 +02:00
|
|
|
if '\n' not in response:
|
2013-08-08 17:45:25 +02:00
|
|
|
# For 1-line responses, pretty-print them
|
|
|
|
extended_response = response.replace(", ", ",\n ")
|
|
|
|
else:
|
|
|
|
extended_response = response
|
|
|
|
call['rendered_response'] = bugdown.convert("~~~ .py\n" + extended_response + "\n~~~\n", "default")
|
2013-04-09 22:20:46 +02:00
|
|
|
for example_type in ('request', 'response'):
|
|
|
|
for lang in call.get('example_' + example_type, []):
|
|
|
|
langs.add(lang)
|
|
|
|
return render_to_response(
|
2013-07-29 23:03:31 +02:00
|
|
|
'zerver/api_endpoints.html', {
|
2013-04-09 22:20:46 +02:00
|
|
|
'content': calls,
|
|
|
|
'langs': langs,
|
|
|
|
},
|
2016-04-21 08:48:33 +02:00
|
|
|
request=request)
|
2013-04-09 22:20:46 +02:00
|
|
|
|
2012-12-11 23:42:32 +01:00
|
|
|
@authenticated_json_post_view
|
|
|
|
@has_request_variables
|
2016-06-05 04:58:33 +02:00
|
|
|
def json_invite_users(request, user_profile, invitee_emails_raw=REQ("invitee_emails")):
|
|
|
|
# type: (HttpRequest, UserProfile, str) -> HttpResponse
|
|
|
|
if not invitee_emails_raw:
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("You must specify at least one email address."))
|
2012-12-11 23:42:32 +01:00
|
|
|
|
2016-06-05 04:58:33 +02:00
|
|
|
invitee_emails = set(re.split(r'[, \n]', invitee_emails_raw))
|
2012-12-11 23:42:32 +01:00
|
|
|
|
|
|
|
stream_names = request.POST.getlist('stream')
|
|
|
|
if not stream_names:
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("You must specify at least one stream for invitees to join."))
|
2012-12-11 23:42:32 +01:00
|
|
|
|
2014-01-15 22:21:43 +01:00
|
|
|
# We unconditionally sub you to the notifications stream if it
|
|
|
|
# exists and is public.
|
|
|
|
notifications_stream = user_profile.realm.notifications_stream
|
|
|
|
if notifications_stream and not notifications_stream.invite_only:
|
|
|
|
stream_names.append(notifications_stream.name)
|
|
|
|
|
2016-06-27 13:52:20 +02:00
|
|
|
streams = [] # type: List[Stream]
|
2012-12-11 23:42:32 +01:00
|
|
|
for stream_name in stream_names:
|
2013-01-14 21:47:17 +01:00
|
|
|
stream = get_stream(stream_name, user_profile.realm)
|
|
|
|
if stream is None:
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("Stream does not exist: %s. No invites were sent.") % (stream_name,))
|
2013-01-14 21:47:17 +01:00
|
|
|
streams.append(stream)
|
2012-12-11 23:42:32 +01:00
|
|
|
|
2013-07-08 17:57:04 +02:00
|
|
|
ret_error, error_data = do_invite_users(user_profile, invitee_emails, streams)
|
2013-01-03 00:19:35 +01:00
|
|
|
|
2013-07-08 17:57:04 +02:00
|
|
|
if ret_error is not None:
|
|
|
|
return json_error(data=error_data, msg=ret_error)
|
2013-02-06 17:27:40 +01:00
|
|
|
else:
|
|
|
|
return json_success()
|
2012-12-11 23:42:32 +01:00
|
|
|
|
2013-08-07 17:59:45 +02:00
|
|
|
def create_homepage_form(request, user_info=None):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, Optional[Dict[str, Any]]) -> HomepageForm
|
2013-08-07 17:59:45 +02:00
|
|
|
if user_info:
|
|
|
|
return HomepageForm(user_info, domain=request.session.get("domain"))
|
|
|
|
# An empty fields dict is not treated the same way as not
|
|
|
|
# providing it.
|
|
|
|
return HomepageForm(domain=request.session.get("domain"))
|
|
|
|
|
2013-11-04 23:08:39 +01:00
|
|
|
def maybe_send_to_registration(request, email, full_name=''):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, text_type, text_type) -> HttpResponse
|
2013-11-04 23:08:39 +01:00
|
|
|
form = create_homepage_form(request, user_info={'email': email})
|
|
|
|
request.verified_email = None
|
|
|
|
if form.is_valid():
|
|
|
|
# Construct a PreregistrationUser object and send the user over to
|
|
|
|
# the confirmation view.
|
2013-11-14 06:05:55 +01:00
|
|
|
prereg_user = None
|
|
|
|
if settings.ONLY_SSO:
|
|
|
|
try:
|
|
|
|
prereg_user = PreregistrationUser.objects.filter(email__iexact=email).latest("invited_at")
|
|
|
|
except PreregistrationUser.DoesNotExist:
|
|
|
|
prereg_user = create_preregistration_user(email, request)
|
|
|
|
else:
|
|
|
|
prereg_user = create_preregistration_user(email, request)
|
|
|
|
|
2013-11-04 23:08:39 +01:00
|
|
|
return redirect("".join((
|
2014-03-28 00:42:32 +01:00
|
|
|
settings.EXTERNAL_URI_SCHEME,
|
2015-01-31 04:46:48 +01:00
|
|
|
request.get_host(),
|
2013-11-16 02:48:34 +01:00
|
|
|
"/",
|
2013-11-04 23:08:39 +01:00
|
|
|
# Split this so we only get the part after the /
|
|
|
|
Confirmation.objects.get_link_for_object(prereg_user).split("/", 3)[3],
|
2013-11-05 22:13:59 +01:00
|
|
|
'?full_name=',
|
2013-11-04 23:08:39 +01:00
|
|
|
# urllib does not handle Unicode, so coerece to encoded byte string
|
|
|
|
# Explanation: http://stackoverflow.com/a/5605354/90777
|
2016-01-24 03:39:44 +01:00
|
|
|
urllib.parse.quote_plus(full_name.encode('utf8')))))
|
2013-11-04 23:08:39 +01:00
|
|
|
else:
|
2016-07-26 11:05:59 +02:00
|
|
|
url = reverse('register')
|
2016-06-29 06:52:41 +02:00
|
|
|
return render_to_response('zerver/accounts_home.html',
|
2016-07-26 11:05:59 +02:00
|
|
|
{'form': form, 'current_url': lambda: url},
|
2016-04-21 08:48:33 +02:00
|
|
|
request=request)
|
2013-11-04 23:08:39 +01:00
|
|
|
|
2014-03-28 00:47:36 +01:00
|
|
|
def login_or_register_remote_user(request, remote_username, user_profile, full_name=''):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, str, UserProfile, text_type) -> HttpResponse
|
2015-01-31 04:46:48 +01:00
|
|
|
if user_profile is None or user_profile.is_mirror_dummy:
|
2014-03-28 00:47:36 +01:00
|
|
|
# Since execution has reached here, the client specified a remote user
|
|
|
|
# but no associated user account exists. Send them over to the
|
|
|
|
# PreregistrationUser flow.
|
|
|
|
return maybe_send_to_registration(request, remote_user_to_email(remote_username), full_name)
|
|
|
|
else:
|
|
|
|
login(request, user_profile)
|
|
|
|
return HttpResponseRedirect("%s%s" % (settings.EXTERNAL_URI_SCHEME,
|
2015-02-06 05:49:33 +01:00
|
|
|
request.get_host()))
|
2014-03-28 00:47:36 +01:00
|
|
|
|
2013-11-04 23:16:46 +01:00
|
|
|
def remote_user_sso(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2013-11-04 23:16:46 +01:00
|
|
|
try:
|
|
|
|
remote_user = request.META["REMOTE_USER"]
|
2013-11-05 23:50:28 +01:00
|
|
|
except KeyError:
|
2016-05-25 15:02:02 +02:00
|
|
|
raise JsonableError(_("No REMOTE_USER set."))
|
2013-11-04 23:16:46 +01:00
|
|
|
|
2014-03-28 00:47:36 +01:00
|
|
|
user_profile = authenticate(remote_user=remote_user)
|
|
|
|
return login_or_register_remote_user(request, remote_user, user_profile)
|
2013-11-04 23:16:46 +01:00
|
|
|
|
2014-03-28 00:49:20 +01:00
|
|
|
@csrf_exempt
|
|
|
|
def remote_user_jwt(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2014-03-28 00:49:20 +01:00
|
|
|
try:
|
|
|
|
json_web_token = request.POST["json_web_token"]
|
|
|
|
payload, signing_input, header, signature = jwt.load(json_web_token)
|
|
|
|
except KeyError:
|
2016-05-25 15:02:02 +02:00
|
|
|
raise JsonableError(_("No JSON web token passed in request"))
|
2014-03-28 00:49:20 +01:00
|
|
|
except jwt.DecodeError:
|
2016-05-25 15:02:02 +02:00
|
|
|
raise JsonableError(_("Bad JSON web token"))
|
2014-03-28 00:49:20 +01:00
|
|
|
|
|
|
|
remote_user = payload.get("user", None)
|
|
|
|
if remote_user is None:
|
2016-05-25 15:02:02 +02:00
|
|
|
raise JsonableError(_("No user specified in JSON web token claims"))
|
2014-03-28 00:49:20 +01:00
|
|
|
domain = payload.get('realm', None)
|
|
|
|
if domain is None:
|
2016-05-25 15:02:02 +02:00
|
|
|
raise JsonableError(_("No domain specified in JSON web token claims"))
|
2014-03-28 00:49:20 +01:00
|
|
|
|
|
|
|
email = "%s@%s" % (remote_user, domain)
|
|
|
|
|
|
|
|
try:
|
|
|
|
jwt.verify_signature(payload, signing_input, header, signature,
|
|
|
|
settings.JWT_AUTH_KEYS[domain])
|
2014-04-07 20:01:38 +02:00
|
|
|
# We do all the authentication we need here (otherwise we'd have to
|
|
|
|
# duplicate work), but we need to call authenticate with some backend so
|
|
|
|
# that the request.backend attribute gets set.
|
|
|
|
user_profile = authenticate(username=email, use_dummy_backend=True)
|
2014-03-28 00:49:20 +01:00
|
|
|
except (jwt.DecodeError, jwt.ExpiredSignature):
|
2016-05-25 15:02:02 +02:00
|
|
|
raise JsonableError(_("Bad JSON web token signature"))
|
2014-03-28 00:49:20 +01:00
|
|
|
except KeyError:
|
2016-05-25 15:02:02 +02:00
|
|
|
raise JsonableError(_("Realm not authorized for JWT login"))
|
2014-03-28 00:49:20 +01:00
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
user_profile = None
|
|
|
|
|
|
|
|
return login_or_register_remote_user(request, email, user_profile, remote_user)
|
2013-11-04 23:16:46 +01:00
|
|
|
|
2015-01-29 08:59:41 +01:00
|
|
|
def google_oauth2_csrf(request, value):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, str) -> HttpResponse
|
2015-01-29 08:59:41 +01:00
|
|
|
return hmac.new(get_token(request).encode('utf-8'), value, hashlib.sha256).hexdigest()
|
|
|
|
|
|
|
|
def start_google_oauth2(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2015-01-29 08:59:41 +01:00
|
|
|
uri = 'https://accounts.google.com/o/oauth2/auth?'
|
|
|
|
cur_time = str(int(time.time()))
|
|
|
|
csrf_state = '{}:{}'.format(
|
|
|
|
cur_time,
|
|
|
|
google_oauth2_csrf(request, cur_time),
|
|
|
|
)
|
|
|
|
prams = {
|
|
|
|
'response_type': 'code',
|
|
|
|
'client_id': settings.GOOGLE_OAUTH2_CLIENT_ID,
|
|
|
|
'redirect_uri': ''.join((
|
|
|
|
settings.EXTERNAL_URI_SCHEME,
|
2015-02-06 05:14:04 +01:00
|
|
|
request.get_host(),
|
2015-01-29 08:59:41 +01:00
|
|
|
reverse('zerver.views.finish_google_oauth2'),
|
|
|
|
)),
|
|
|
|
'scope': 'profile email',
|
|
|
|
'state': csrf_state,
|
|
|
|
}
|
2016-01-24 03:39:44 +01:00
|
|
|
return redirect(uri + urllib.parse.urlencode(prams))
|
2015-01-29 08:59:41 +01:00
|
|
|
|
2015-10-02 03:53:54 +02:00
|
|
|
# Workaround to support the Python-requests 1.0 transition of .json
|
|
|
|
# from a property to a function
|
|
|
|
requests_json_is_function = callable(requests.Response.json)
|
|
|
|
def extract_json_response(resp):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpResponse) -> Dict[str, Any]
|
2015-10-02 03:53:54 +02:00
|
|
|
if requests_json_is_function:
|
|
|
|
return resp.json()
|
|
|
|
else:
|
|
|
|
return resp.json
|
|
|
|
|
2015-01-29 08:59:41 +01:00
|
|
|
def finish_google_oauth2(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2015-01-29 08:59:41 +01:00
|
|
|
error = request.GET.get('error')
|
|
|
|
if error == 'access_denied':
|
|
|
|
return redirect('/')
|
|
|
|
elif error is not None:
|
2016-01-27 05:19:05 +01:00
|
|
|
logging.warning('Error from google oauth2 login %r', request.GET)
|
2015-01-29 08:59:41 +01:00
|
|
|
return HttpResponse(status=400)
|
|
|
|
|
|
|
|
value, hmac_value = request.GET.get('state').split(':')
|
|
|
|
if hmac_value != google_oauth2_csrf(request, value):
|
2016-01-27 05:19:05 +01:00
|
|
|
logging.warning('Google oauth2 CSRF error')
|
|
|
|
return HttpResponse(status=400)
|
2015-01-29 08:59:41 +01:00
|
|
|
|
|
|
|
resp = requests.post(
|
|
|
|
'https://www.googleapis.com/oauth2/v3/token',
|
|
|
|
data={
|
|
|
|
'code': request.GET.get('code'),
|
|
|
|
'client_id': settings.GOOGLE_OAUTH2_CLIENT_ID,
|
|
|
|
'client_secret': settings.GOOGLE_OAUTH2_CLIENT_SECRET,
|
|
|
|
'redirect_uri': ''.join((
|
|
|
|
settings.EXTERNAL_URI_SCHEME,
|
2015-02-06 05:32:59 +01:00
|
|
|
request.get_host(),
|
2015-01-29 08:59:41 +01:00
|
|
|
reverse('zerver.views.finish_google_oauth2'),
|
|
|
|
)),
|
|
|
|
'grant_type': 'authorization_code',
|
|
|
|
},
|
|
|
|
)
|
2016-01-27 05:19:05 +01:00
|
|
|
if resp.status_code == 400:
|
|
|
|
logging.warning('User error converting Google oauth2 login to token: %r' % (resp.text,))
|
|
|
|
return HttpResponse(status=400)
|
|
|
|
elif resp.status_code != 200:
|
2016-01-27 05:12:06 +01:00
|
|
|
raise Exception('Could not convert google oauth2 code to access_token\r%r' % (resp.text,))
|
2015-10-02 03:53:54 +02:00
|
|
|
access_token = extract_json_response(resp)['access_token']
|
2015-01-29 08:59:41 +01:00
|
|
|
|
|
|
|
resp = requests.get(
|
|
|
|
'https://www.googleapis.com/plus/v1/people/me',
|
|
|
|
params={'access_token': access_token}
|
|
|
|
)
|
2016-01-27 05:19:05 +01:00
|
|
|
if resp.status_code == 400:
|
|
|
|
logging.warning('Google login failed making info API call: %r' % (resp.text,))
|
|
|
|
return HttpResponse(status=400)
|
|
|
|
elif resp.status_code != 200:
|
2016-01-27 05:12:06 +01:00
|
|
|
raise Exception('Google login failed making API call\r%r' % (resp.text,))
|
2015-10-02 03:53:54 +02:00
|
|
|
body = extract_json_response(resp)
|
2015-01-29 08:59:41 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
full_name = body['name']['formatted']
|
|
|
|
except KeyError:
|
|
|
|
# Only google+ users have a formated name. I am ignoring i18n here.
|
2015-02-06 18:56:39 +01:00
|
|
|
full_name = u'{} {}'.format(
|
2015-01-29 08:59:41 +01:00
|
|
|
body['name']['givenName'], body['name']['familyName']
|
|
|
|
)
|
|
|
|
for email in body['emails']:
|
|
|
|
if email['type'] == 'account':
|
|
|
|
break
|
|
|
|
else:
|
2015-12-01 17:11:16 +01:00
|
|
|
raise Exception('Google oauth2 account email not found %r' % (body,))
|
2015-01-29 08:59:41 +01:00
|
|
|
email_address = email['value']
|
|
|
|
user_profile = authenticate(username=email_address, use_dummy_backend=True)
|
|
|
|
return login_or_register_remote_user(request, email_address, user_profile, full_name)
|
|
|
|
|
2012-09-29 00:49:34 +02:00
|
|
|
def login_page(request, **kwargs):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, **Any) -> HttpResponse
|
2015-11-01 17:10:46 +01:00
|
|
|
extra_context = kwargs.pop('extra_context', {})
|
2015-08-19 02:58:20 +02:00
|
|
|
if dev_auth_enabled():
|
2015-12-26 02:07:49 +01:00
|
|
|
# Development environments usually have only a few users, but
|
|
|
|
# it still makes sense to limit how many users we render to
|
|
|
|
# support performance testing with DevAuthBackend.
|
|
|
|
MAX_DEV_BACKEND_USERS = 100
|
|
|
|
users_query = UserProfile.objects.select_related().filter(is_bot=False, is_active=True)
|
|
|
|
users = users_query.order_by('email')[0:MAX_DEV_BACKEND_USERS]
|
2016-02-08 03:59:38 +01:00
|
|
|
extra_context['direct_admins'] = [u.email for u in users if u.is_realm_admin]
|
|
|
|
extra_context['direct_users'] = [u.email for u in users if not u.is_realm_admin]
|
2014-01-07 19:51:18 +01:00
|
|
|
template_response = django_login_page(
|
2015-08-19 02:58:20 +02:00
|
|
|
request, authentication_form=OurAuthenticationForm,
|
|
|
|
extra_context=extra_context, **kwargs)
|
2012-09-29 00:49:34 +02:00
|
|
|
try:
|
2012-10-11 19:15:41 +02:00
|
|
|
template_response.context_data['email'] = request.GET['email']
|
2012-09-29 00:49:34 +02:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-01-07 19:51:18 +01:00
|
|
|
|
2012-09-29 00:49:34 +02:00
|
|
|
return template_response
|
|
|
|
|
2015-08-19 02:58:20 +02:00
|
|
|
def dev_direct_login(request, **kwargs):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, **Any) -> HttpResponse
|
2015-08-19 02:58:20 +02:00
|
|
|
# This function allows logging in without a password and should only be called in development environments.
|
|
|
|
# It may be called if the DevAuthBackend is included in settings.AUTHENTICATION_BACKENDS
|
2015-08-21 09:18:44 +02:00
|
|
|
if (not dev_auth_enabled()) or settings.PRODUCTION:
|
2015-08-19 02:58:20 +02:00
|
|
|
# This check is probably not required, since authenticate would fail without an enabled DevAuthBackend.
|
|
|
|
raise Exception('Direct login not supported.')
|
|
|
|
email = request.POST['direct_email']
|
|
|
|
user_profile = authenticate(username=email)
|
2016-04-21 07:19:08 +02:00
|
|
|
if user_profile is None:
|
|
|
|
raise Exception("User cannot login")
|
2015-08-19 02:58:20 +02:00
|
|
|
login(request, user_profile)
|
|
|
|
return HttpResponseRedirect("%s%s" % (settings.EXTERNAL_URI_SCHEME,
|
|
|
|
request.get_host()))
|
|
|
|
|
2016-06-01 02:28:27 +02:00
|
|
|
@csrf_exempt
|
|
|
|
@require_post
|
|
|
|
@has_request_variables
|
|
|
|
def api_dev_fetch_api_key(request, username=REQ()):
|
|
|
|
# type: (HttpRequest, str) -> HttpResponse
|
|
|
|
"""This function allows logging in without a password on the Zulip
|
|
|
|
mobile apps when connecting to a Zulip development environment. It
|
|
|
|
requires DevAuthBackend to be included in settings.AUTHENTICATION_BACKENDS.
|
|
|
|
"""
|
|
|
|
if not dev_auth_enabled() or settings.PRODUCTION:
|
|
|
|
return json_error(_("Dev environment not enabled."))
|
|
|
|
return_data = {} # type: Dict[str, bool]
|
|
|
|
user_profile = authenticate(username=username, return_data=return_data)
|
|
|
|
if return_data.get("inactive_realm") == True:
|
|
|
|
return json_error(_("Your realm has been deactivated."),
|
|
|
|
data={"reason": "realm deactivated"}, status=403)
|
|
|
|
if return_data.get("inactive_user") == True:
|
|
|
|
return json_error(_("Your account has been disabled."),
|
|
|
|
data={"reason": "user disable"}, status=403)
|
|
|
|
login(request, user_profile)
|
|
|
|
return json_success({"api_key": user_profile.api_key, "email": user_profile.email})
|
|
|
|
|
2016-06-01 02:28:43 +02:00
|
|
|
@csrf_exempt
|
|
|
|
def api_dev_get_emails(request):
|
|
|
|
# type: (HttpRequest) -> HttpResponse
|
|
|
|
if not dev_auth_enabled() or settings.PRODUCTION:
|
|
|
|
return json_error(_("Dev environment not enabled."))
|
|
|
|
MAX_DEV_BACKEND_USERS = 100 # type: int
|
|
|
|
users_query = UserProfile.objects.select_related().filter(is_bot=False, is_active=True)
|
|
|
|
users = users_query.order_by('email')[0:MAX_DEV_BACKEND_USERS]
|
|
|
|
return json_success(dict(direct_admins=[u.email for u in users if u.is_realm_admin],
|
|
|
|
direct_users=[u.email for u in users if not u.is_realm_admin]))
|
|
|
|
|
2013-07-11 21:40:52 +02:00
|
|
|
@authenticated_json_post_view
|
|
|
|
@has_request_variables
|
2013-12-17 20:09:05 +01:00
|
|
|
def json_bulk_invite_users(request, user_profile,
|
2016-06-05 04:58:33 +02:00
|
|
|
invitee_emails_list=REQ('invitee_emails',
|
|
|
|
validator=check_list(check_string))):
|
|
|
|
# type: (HttpRequest, UserProfile, List[str]) -> HttpResponse
|
|
|
|
invitee_emails = set(invitee_emails_list)
|
2013-07-11 21:40:52 +02:00
|
|
|
streams = get_default_subs(user_profile)
|
|
|
|
|
|
|
|
ret_error, error_data = do_invite_users(user_profile, invitee_emails, streams)
|
|
|
|
|
|
|
|
if ret_error is not None:
|
|
|
|
return json_error(data=error_data, msg=ret_error)
|
|
|
|
else:
|
2014-01-09 22:19:47 +01:00
|
|
|
# Report bulk invites to internal Zulip.
|
|
|
|
invited = PreregistrationUser.objects.filter(referred_by=user_profile)
|
|
|
|
internal_message = "%s <`%s`> invited %d people to Zulip." % (
|
|
|
|
user_profile.full_name, user_profile.email, invited.count())
|
|
|
|
internal_send_message(settings.NEW_USER_BOT, "stream", "signups",
|
|
|
|
user_profile.realm.domain, internal_message)
|
2013-07-11 21:40:52 +02:00
|
|
|
return json_success()
|
|
|
|
|
2016-04-21 23:48:34 +02:00
|
|
|
@zulip_login_required
|
2013-07-11 21:40:52 +02:00
|
|
|
def initial_invite_page(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2013-07-11 21:40:52 +02:00
|
|
|
user = request.user
|
|
|
|
# Only show the bulk-invite page for the first user in a realm
|
|
|
|
domain_count = len(UserProfile.objects.filter(realm=user.realm))
|
|
|
|
if domain_count > 1:
|
2013-07-29 23:03:31 +02:00
|
|
|
return redirect('zerver.views.home')
|
2013-07-11 21:40:52 +02:00
|
|
|
|
|
|
|
params = {'company_name': user.realm.domain}
|
|
|
|
|
|
|
|
if (user.realm.restricted_to_domain):
|
|
|
|
params['invite_suffix'] = user.realm.domain
|
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
return render_to_response('zerver/initial_invite_page.html', params,
|
2016-04-21 08:48:33 +02:00
|
|
|
request=request)
|
2013-07-11 21:40:52 +02:00
|
|
|
|
2013-03-05 23:45:02 +01:00
|
|
|
@require_post
|
|
|
|
def logout_then_login(request, **kwargs):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, **Any) -> HttpResponse
|
2013-03-05 23:45:02 +01:00
|
|
|
return django_logout_then_login(request, kwargs)
|
|
|
|
|
2016-06-03 01:02:58 +02:00
|
|
|
def create_preregistration_user(email, request, realm_creation=False):
|
|
|
|
# type: (text_type, HttpRequest, bool) -> HttpResponse
|
2013-08-02 20:32:56 +02:00
|
|
|
domain = request.session.get("domain")
|
2013-12-09 19:40:48 +01:00
|
|
|
if completely_open(domain):
|
|
|
|
# Clear the "domain" from the session object; it's no longer needed
|
|
|
|
request.session["domain"] = None
|
|
|
|
|
|
|
|
# The user is trying to sign up for a completely open realm,
|
|
|
|
# so create them a PreregistrationUser for that realm
|
|
|
|
return PreregistrationUser.objects.create(email=email,
|
2016-06-03 01:02:58 +02:00
|
|
|
realm=get_realm(domain),
|
|
|
|
realm_creation=realm_creation)
|
2013-12-09 19:40:48 +01:00
|
|
|
|
2016-06-03 01:02:58 +02:00
|
|
|
return PreregistrationUser.objects.create(email=email, realm_creation=realm_creation)
|
2013-08-02 20:32:56 +02:00
|
|
|
|
|
|
|
def accounts_home_with_domain(request, domain):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, str) -> HttpResponse
|
2013-08-02 20:32:56 +02:00
|
|
|
if completely_open(domain):
|
|
|
|
# You can sign up for a completely open realm through a
|
|
|
|
# special registration path that contains the domain in the
|
|
|
|
# URL. We store this information in the session rather than
|
|
|
|
# elsewhere because we don't have control over URL or form
|
|
|
|
# data for folks registering through OpenID.
|
|
|
|
request.session["domain"] = domain
|
|
|
|
return accounts_home(request)
|
|
|
|
else:
|
2013-07-29 23:03:31 +02:00
|
|
|
return HttpResponseRedirect(reverse('zerver.views.accounts_home'))
|
2013-08-02 20:32:56 +02:00
|
|
|
|
2016-06-03 01:02:58 +02:00
|
|
|
def send_registration_completion_email(email, request, realm_creation=False):
|
2016-06-25 19:04:36 +02:00
|
|
|
# type: (str, HttpRequest, bool) -> Confirmation
|
2014-01-31 20:46:45 +01:00
|
|
|
"""
|
|
|
|
Send an email with a confirmation link to the provided e-mail so the user
|
|
|
|
can complete their registration.
|
|
|
|
"""
|
2016-06-03 01:02:58 +02:00
|
|
|
prereg_user = create_preregistration_user(email, request, realm_creation)
|
2014-01-31 20:46:45 +01:00
|
|
|
context = {'support_email': settings.ZULIP_ADMINISTRATOR,
|
2016-07-19 07:31:59 +02:00
|
|
|
'verbose_support_offers': settings.VERBOSE_SUPPORT_OFFERS}
|
2016-06-23 21:53:46 +02:00
|
|
|
return Confirmation.objects.send_confirmation(prereg_user, email,
|
|
|
|
additional_context=context)
|
2014-01-31 20:46:45 +01:00
|
|
|
|
2016-07-17 19:40:12 +02:00
|
|
|
def redirect_to_email_login_url(email):
|
|
|
|
login_url = reverse('django.contrib.auth.views.login')
|
|
|
|
redirect_url = login_url + '?email=' + urllib.parse.quote_plus(email)
|
|
|
|
return HttpResponseRedirect(redirect_url)
|
|
|
|
|
2016-06-03 01:02:58 +02:00
|
|
|
"""
|
|
|
|
When settings.OPEN_REALM_CREATION is enabled public users can create new realm. For creating the realm the user should
|
|
|
|
not be the member of any current realm. The realm is created with domain same as the that of the user's email.
|
|
|
|
When there is no unique_open_realm user registrations are made by visiting /register/domain_of_the_realm.
|
|
|
|
"""
|
2016-06-22 21:16:02 +02:00
|
|
|
def create_realm(request, creation_key=None):
|
|
|
|
# type: (HttpRequest, Optional[text_type]) -> HttpResponse
|
2016-06-03 01:02:58 +02:00
|
|
|
if not settings.OPEN_REALM_CREATION:
|
2016-06-22 21:16:02 +02:00
|
|
|
if creation_key is None:
|
2016-07-08 18:57:01 +02:00
|
|
|
return render_to_response("zerver/realm_creation_failed.html",
|
|
|
|
{'message': _('New organization creation disabled.')})
|
2016-06-22 21:16:02 +02:00
|
|
|
elif not check_key_is_valid(creation_key):
|
2016-07-17 19:40:12 +02:00
|
|
|
return render_to_response("zerver/realm_creation_failed.html",
|
|
|
|
{'message': _('The organization creation link has been expired'
|
|
|
|
' or is not valid.')})
|
2016-06-03 01:02:58 +02:00
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = RealmCreationForm(request.POST, domain=request.session.get("domain"))
|
|
|
|
if form.is_valid():
|
|
|
|
email = form.cleaned_data['email']
|
2016-06-25 19:04:36 +02:00
|
|
|
confirmation_key = send_registration_completion_email(email, request, realm_creation=True).confirmation_key
|
|
|
|
if settings.DEVELOPMENT:
|
|
|
|
request.session['confirmation_key'] = {'confirmation_key': confirmation_key}
|
2016-06-22 21:16:02 +02:00
|
|
|
if (creation_key is not None and check_key_is_valid(creation_key)):
|
|
|
|
RealmCreationKey.objects.get(creation_key=creation_key).delete()
|
2016-06-03 01:02:58 +02:00
|
|
|
return HttpResponseRedirect(reverse('send_confirm', kwargs={'email': email}))
|
|
|
|
try:
|
|
|
|
email = request.POST['email']
|
|
|
|
user_email_is_unique(email)
|
|
|
|
except ValidationError:
|
|
|
|
# if the user user is already registered he can't create a new realm as a realm
|
|
|
|
# with the same domain as user's email already exists
|
2016-07-17 19:40:12 +02:00
|
|
|
return redirect_to_email_login_url(email)
|
2016-06-03 01:02:58 +02:00
|
|
|
else:
|
|
|
|
form = RealmCreationForm(domain=request.session.get("domain"))
|
|
|
|
return render_to_response('zerver/create_realm.html',
|
|
|
|
{'form': form, 'current_url': request.get_full_path},
|
|
|
|
request=request)
|
|
|
|
|
2016-06-25 19:04:36 +02:00
|
|
|
def confirmation_key(request):
|
|
|
|
return json_success(request.session.get('confirmation_key'))
|
2016-06-03 01:02:58 +02:00
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
def accounts_home(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2012-09-28 22:47:05 +02:00
|
|
|
if request.method == 'POST':
|
2013-08-07 17:59:45 +02:00
|
|
|
form = create_homepage_form(request, user_info=request.POST)
|
2012-09-28 22:47:05 +02:00
|
|
|
if form.is_valid():
|
2013-01-08 20:24:47 +01:00
|
|
|
email = form.cleaned_data['email']
|
2014-01-31 20:46:45 +01:00
|
|
|
send_registration_completion_email(email, request)
|
2013-03-28 19:21:29 +01:00
|
|
|
return HttpResponseRedirect(reverse('send_confirm', kwargs={'email': email}))
|
2012-09-29 00:49:34 +02:00
|
|
|
try:
|
|
|
|
email = request.POST['email']
|
2013-02-11 19:37:31 +01:00
|
|
|
# Note: We don't check for uniqueness
|
2013-02-12 20:52:42 +01:00
|
|
|
is_inactive(email)
|
2012-09-29 00:49:34 +02:00
|
|
|
except ValidationError:
|
2016-07-17 19:40:12 +02:00
|
|
|
return redirect_to_email_login_url(email)
|
2012-11-21 21:14:55 +01:00
|
|
|
else:
|
2013-08-07 17:59:45 +02:00
|
|
|
form = create_homepage_form(request)
|
2013-07-29 23:03:31 +02:00
|
|
|
return render_to_response('zerver/accounts_home.html',
|
2013-08-02 20:32:56 +02:00
|
|
|
{'form': form, 'current_url': request.get_full_path},
|
2016-04-21 08:48:33 +02:00
|
|
|
request=request)
|
2012-09-04 23:21:30 +02:00
|
|
|
|
2013-09-04 21:02:11 +02:00
|
|
|
def approximate_unread_count(user_profile):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (UserProfile) -> int
|
2013-09-10 10:50:07 +02:00
|
|
|
not_in_home_view_recipients = [sub.recipient.id for sub in \
|
|
|
|
Subscription.objects.filter(
|
|
|
|
user_profile=user_profile, in_home_view=False)]
|
|
|
|
|
2016-07-16 15:09:10 +02:00
|
|
|
# TODO: We may want to exclude muted messages from this count.
|
|
|
|
# It was attempted in the past, but the original attempt
|
|
|
|
# was broken. When we re-architect muting, we may
|
|
|
|
# want to to revisit this (see git issue #1019).
|
2013-09-10 10:50:07 +02:00
|
|
|
return UserMessage.objects.filter(
|
|
|
|
user_profile=user_profile, message_id__gt=user_profile.pointer).exclude(
|
|
|
|
message__recipient__type=Recipient.STREAM,
|
2013-11-21 17:26:17 +01:00
|
|
|
message__recipient__id__in=not_in_home_view_recipients).exclude(
|
|
|
|
flags=UserMessage.flags.read).count()
|
2013-08-09 20:26:35 +02:00
|
|
|
|
|
|
|
def sent_time_in_epoch_seconds(user_message):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (UserMessage) -> float
|
2013-08-09 20:26:35 +02:00
|
|
|
# user_message is a UserMessage object.
|
|
|
|
if not user_message:
|
|
|
|
return None
|
|
|
|
# We have USE_TZ = True, so our datetime objects are timezone-aware.
|
|
|
|
# Return the epoch seconds in UTC.
|
|
|
|
return calendar.timegm(user_message.message.pub_date.utctimetuple())
|
|
|
|
|
2016-06-23 11:32:45 +02:00
|
|
|
def with_language(string, language):
|
2016-07-30 05:20:05 +02:00
|
|
|
# type: (text_type, text_type) -> text_type
|
2016-06-23 11:32:45 +02:00
|
|
|
old_language = translation.get_language()
|
|
|
|
translation.activate(language)
|
|
|
|
result = _(string)
|
|
|
|
translation.activate(old_language)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def get_language_list():
|
2016-07-30 05:20:05 +02:00
|
|
|
# type: () -> List[Dict[str, Any]]
|
2016-07-13 06:33:36 +02:00
|
|
|
path = os.path.join(settings.STATIC_ROOT, 'locale', 'language_options.json')
|
2016-06-23 11:32:45 +02:00
|
|
|
with open(path, 'r') as reader:
|
|
|
|
languages = ujson.load(reader)
|
|
|
|
lang_list = []
|
|
|
|
for lang_info in languages['languages']:
|
2016-07-29 12:34:38 +02:00
|
|
|
name = lang_info['name']
|
|
|
|
lang_info['name'] = with_language(name, lang_info['code'])
|
|
|
|
if 'percent_translated' not in lang_info:
|
|
|
|
lang_info['percent_translated'] = 'N/A'
|
2016-06-23 11:32:45 +02:00
|
|
|
lang_list.append(lang_info)
|
|
|
|
|
2016-07-29 12:34:38 +02:00
|
|
|
return sorted(lang_list, key=lambda i: i['name'])
|
2016-06-23 11:32:45 +02:00
|
|
|
|
2016-07-31 10:02:42 +02:00
|
|
|
def get_available_language_codes():
|
|
|
|
# type: () -> List[text_type]
|
|
|
|
language_list = get_language_list()
|
|
|
|
codes = [language['code'] for language in language_list]
|
|
|
|
return codes
|
|
|
|
|
2016-04-21 23:48:34 +02:00
|
|
|
@zulip_login_required
|
2012-08-28 18:44:51 +02:00
|
|
|
def home(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2012-11-21 00:42:16 +01:00
|
|
|
# We need to modify the session object every two weeks or it will expire.
|
|
|
|
# This line makes reloading the page a sufficient action to keep the
|
|
|
|
# session alive.
|
|
|
|
request.session.modified = True
|
|
|
|
|
2013-03-29 17:39:53 +01:00
|
|
|
user_profile = request.user
|
2013-09-30 22:34:08 +02:00
|
|
|
request._email = request.user.email
|
2013-10-18 21:19:34 +02:00
|
|
|
request.client = get_client("website")
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2016-06-13 10:14:33 +02:00
|
|
|
narrow = [] # type: List[List[text_type]]
|
2013-12-13 20:20:28 +01:00
|
|
|
narrow_stream = None
|
2014-01-17 00:05:35 +01:00
|
|
|
narrow_topic = request.GET.get("topic")
|
2014-01-17 00:06:30 +01:00
|
|
|
if request.GET.get("stream"):
|
2013-12-13 20:20:28 +01:00
|
|
|
try:
|
2014-01-17 00:06:30 +01:00
|
|
|
narrow_stream = get_stream(request.GET.get("stream"), user_profile.realm)
|
2013-12-13 20:20:28 +01:00
|
|
|
assert(narrow_stream is not None)
|
|
|
|
assert(narrow_stream.is_public())
|
|
|
|
narrow = [["stream", narrow_stream.name]]
|
|
|
|
except Exception:
|
|
|
|
logging.exception("Narrow parsing")
|
2014-01-17 00:05:35 +01:00
|
|
|
if narrow_topic is not None:
|
|
|
|
narrow.append(["topic", narrow_topic])
|
2013-12-13 20:20:28 +01:00
|
|
|
|
2013-10-18 21:19:34 +02:00
|
|
|
register_ret = do_events_register(user_profile, request.client,
|
2013-12-13 20:20:28 +01:00
|
|
|
apply_markdown=True, narrow=narrow)
|
2013-03-28 18:09:27 +01:00
|
|
|
user_has_messages = (register_ret['max_message_id'] != -1)
|
2013-02-06 00:33:45 +01:00
|
|
|
|
2013-05-17 21:28:51 +02:00
|
|
|
# Reset our don't-spam-users-with-email counter since the
|
|
|
|
# user has since logged in
|
|
|
|
if not user_profile.last_reminder is None:
|
|
|
|
user_profile.last_reminder = None
|
2013-07-23 22:01:38 +02:00
|
|
|
user_profile.save(update_fields=["last_reminder"])
|
2013-05-17 21:28:51 +02:00
|
|
|
|
2013-04-04 22:30:28 +02:00
|
|
|
# Brand new users get the tutorial
|
|
|
|
needs_tutorial = settings.TUTORIAL_ENABLED and \
|
2013-06-27 16:41:58 +02:00
|
|
|
user_profile.tutorial_status != UserProfile.TUTORIAL_FINISHED
|
2013-03-12 04:54:17 +01:00
|
|
|
|
2014-01-14 23:53:35 +01:00
|
|
|
first_in_realm = realm_user_count(user_profile.realm) == 1
|
2014-01-09 22:53:18 +01:00
|
|
|
# If you are the only person in the realm and you didn't invite
|
|
|
|
# anyone, we'll continue to encourage you to do so on the frontend.
|
2014-01-14 23:53:35 +01:00
|
|
|
prompt_for_invites = first_in_realm and \
|
2014-01-09 22:53:18 +01:00
|
|
|
not PreregistrationUser.objects.filter(referred_by=user_profile).count()
|
|
|
|
|
2013-03-28 18:09:27 +01:00
|
|
|
if user_profile.pointer == -1 and user_has_messages:
|
2013-02-06 00:33:45 +01:00
|
|
|
# Put the new user's pointer at the bottom
|
|
|
|
#
|
|
|
|
# This improves performance, because we limit backfilling of messages
|
|
|
|
# before the pointer. It's also likely that someone joining an
|
|
|
|
# organization is interested in recent messages more than the very
|
|
|
|
# first messages on the system.
|
|
|
|
|
2013-08-09 23:01:37 +02:00
|
|
|
register_ret['pointer'] = register_ret['max_message_id']
|
2013-02-06 00:33:45 +01:00
|
|
|
user_profile.last_pointer_updater = request.session.session_key
|
2012-09-12 22:55:37 +02:00
|
|
|
|
2013-08-09 20:26:35 +02:00
|
|
|
if user_profile.pointer == -1:
|
|
|
|
latest_read = None
|
|
|
|
else:
|
2013-08-19 18:10:32 +02:00
|
|
|
try:
|
|
|
|
latest_read = UserMessage.objects.get(user_profile=user_profile,
|
|
|
|
message__id=user_profile.pointer)
|
|
|
|
except UserMessage.DoesNotExist:
|
|
|
|
# Don't completely fail if your saved pointer ID is invalid
|
|
|
|
logging.warning("%s has invalid pointer %s" % (user_profile.email, user_profile.pointer))
|
|
|
|
latest_read = None
|
|
|
|
|
2014-01-14 17:06:32 +01:00
|
|
|
desktop_notifications_enabled = user_profile.enable_desktop_notifications
|
|
|
|
if narrow_stream is not None:
|
|
|
|
desktop_notifications_enabled = False
|
|
|
|
|
2014-01-15 22:21:43 +01:00
|
|
|
if user_profile.realm.notifications_stream:
|
|
|
|
notifications_stream = user_profile.realm.notifications_stream.name
|
|
|
|
else:
|
|
|
|
notifications_stream = ""
|
|
|
|
|
2016-06-23 11:32:45 +02:00
|
|
|
# Set default language and make it persist
|
|
|
|
default_language = register_ret['default_language']
|
|
|
|
url_lang = '/{}'.format(request.LANGUAGE_CODE)
|
|
|
|
if not request.path.startswith(url_lang):
|
|
|
|
translation.activate(default_language)
|
|
|
|
|
|
|
|
request.session[translation.LANGUAGE_SESSION_KEY] = default_language
|
|
|
|
|
2013-03-25 23:49:38 +01:00
|
|
|
# Pass parameters to the client-side JavaScript code.
|
|
|
|
# These end up in a global JavaScript Object named 'page_params'.
|
2013-12-14 00:00:32 +01:00
|
|
|
page_params = dict(
|
2016-07-19 06:31:33 +02:00
|
|
|
share_the_love = settings.SHARE_THE_LOVE,
|
2016-06-20 21:58:59 +02:00
|
|
|
development_environment = settings.DEVELOPMENT,
|
2013-03-25 23:49:38 +01:00
|
|
|
debug_mode = settings.DEBUG,
|
2013-10-11 21:36:49 +02:00
|
|
|
test_suite = settings.TEST_SUITE,
|
2013-03-25 23:49:38 +01:00
|
|
|
poll_timeout = settings.POLL_TIMEOUT,
|
2013-11-13 21:31:09 +01:00
|
|
|
login_page = settings.HOME_NOT_LOGGED_IN,
|
2015-11-21 04:08:53 +01:00
|
|
|
maxfilesize = settings.MAX_FILE_UPLOAD_SIZE,
|
2016-07-05 13:05:51 +02:00
|
|
|
server_generation = settings.SERVER_GENERATION,
|
2014-03-28 00:45:03 +01:00
|
|
|
password_auth_enabled = password_auth_enabled(user_profile.realm),
|
2013-03-28 18:09:27 +01:00
|
|
|
have_initial_messages = user_has_messages,
|
2014-01-23 21:04:46 +01:00
|
|
|
subbed_info = register_ret['subscriptions'],
|
2013-06-12 21:15:32 +02:00
|
|
|
unsubbed_info = register_ret['unsubscribed'],
|
2016-07-12 23:57:16 +02:00
|
|
|
neversubbed_info = register_ret['never_subscribed'],
|
2013-10-21 19:37:52 +02:00
|
|
|
email_dict = register_ret['email_dict'],
|
2013-03-29 15:35:37 +01:00
|
|
|
people_list = register_ret['realm_users'],
|
2014-02-26 00:12:14 +01:00
|
|
|
bot_list = register_ret['realm_bots'],
|
2013-03-28 18:09:27 +01:00
|
|
|
initial_pointer = register_ret['pointer'],
|
2013-04-03 22:00:02 +02:00
|
|
|
initial_presences = register_ret['presences'],
|
2013-05-06 17:14:59 +02:00
|
|
|
initial_servertime = time.time(), # Used for calculating relative presence age
|
2013-03-25 23:49:38 +01:00
|
|
|
fullname = user_profile.full_name,
|
2013-03-28 20:43:34 +01:00
|
|
|
email = user_profile.email,
|
2013-03-25 23:49:38 +01:00
|
|
|
domain = user_profile.realm.domain,
|
2014-01-29 19:08:09 +01:00
|
|
|
realm_name = register_ret['realm_name'],
|
2015-08-20 02:38:32 +02:00
|
|
|
realm_invite_required = register_ret['realm_invite_required'],
|
2015-08-20 21:25:30 +02:00
|
|
|
realm_invite_by_admins_only = register_ret['realm_invite_by_admins_only'],
|
2016-05-12 10:28:00 +02:00
|
|
|
realm_create_stream_by_admins_only = register_ret['realm_create_stream_by_admins_only'],
|
2016-06-21 21:34:41 +02:00
|
|
|
realm_allow_message_editing = register_ret['realm_allow_message_editing'],
|
2016-07-08 02:25:55 +02:00
|
|
|
realm_message_content_edit_limit_seconds = register_ret['realm_message_content_edit_limit_seconds'],
|
2015-08-20 02:38:32 +02:00
|
|
|
realm_restricted_to_domain = register_ret['realm_restricted_to_domain'],
|
2013-03-25 23:49:38 +01:00
|
|
|
enter_sends = user_profile.enter_sends,
|
2015-08-20 23:59:44 +02:00
|
|
|
left_side_userlist = register_ret['left_side_userlist'],
|
2016-06-23 11:32:45 +02:00
|
|
|
default_language = register_ret['default_language'],
|
|
|
|
language_list = get_language_list(),
|
2013-07-26 16:51:02 +02:00
|
|
|
referrals = register_ret['referrals'],
|
2013-08-22 19:54:35 +02:00
|
|
|
realm_emoji = register_ret['realm_emoji'],
|
2013-03-25 23:49:38 +01:00
|
|
|
needs_tutorial = needs_tutorial,
|
2014-01-14 23:53:35 +01:00
|
|
|
first_in_realm = first_in_realm,
|
2014-01-09 22:53:18 +01:00
|
|
|
prompt_for_invites = prompt_for_invites,
|
2014-01-15 22:21:43 +01:00
|
|
|
notifications_stream = notifications_stream,
|
2016-06-10 19:46:53 +02:00
|
|
|
cross_realm_user_emails = list(get_cross_realm_users()),
|
2014-02-05 21:41:01 +01:00
|
|
|
|
|
|
|
# Stream message notification settings:
|
|
|
|
stream_desktop_notifications_enabled =
|
|
|
|
user_profile.enable_stream_desktop_notifications,
|
|
|
|
stream_sounds_enabled = user_profile.enable_stream_sounds,
|
|
|
|
|
|
|
|
# Private message and @-mention notification settings:
|
|
|
|
desktop_notifications_enabled = desktop_notifications_enabled,
|
2013-05-03 21:49:01 +02:00
|
|
|
sounds_enabled =
|
|
|
|
user_profile.enable_sounds,
|
2013-05-07 23:19:52 +02:00
|
|
|
enable_offline_email_notifications =
|
|
|
|
user_profile.enable_offline_email_notifications,
|
2013-10-16 17:24:52 +02:00
|
|
|
enable_offline_push_notifications =
|
|
|
|
user_profile.enable_offline_push_notifications,
|
2015-08-19 22:35:46 +02:00
|
|
|
twenty_four_hour_time = register_ret['twenty_four_hour_time'],
|
2014-02-05 21:41:01 +01:00
|
|
|
|
2013-12-02 02:00:23 +01:00
|
|
|
enable_digest_emails = user_profile.enable_digest_emails,
|
2013-03-28 18:09:27 +01:00
|
|
|
event_queue_id = register_ret['queue_id'],
|
|
|
|
last_event_id = register_ret['last_event_id'],
|
2013-05-08 15:47:37 +02:00
|
|
|
max_message_id = register_ret['max_message_id'],
|
2013-09-04 21:02:11 +02:00
|
|
|
unread_count = approximate_unread_count(user_profile),
|
2013-08-09 20:26:35 +02:00
|
|
|
furthest_read_time = sent_time_in_epoch_seconds(latest_read),
|
2016-07-19 06:44:48 +02:00
|
|
|
save_stacktraces = settings.SAVE_FRONTEND_STACKTRACES,
|
2013-09-10 11:46:18 +02:00
|
|
|
alert_words = register_ret['alert_words'],
|
2013-09-27 19:38:56 +02:00
|
|
|
muted_topics = register_ret['muted_topics'],
|
2014-01-06 23:42:02 +01:00
|
|
|
realm_filters = register_ret['realm_filters'],
|
2016-05-20 22:08:42 +02:00
|
|
|
realm_default_streams = register_ret['realm_default_streams'],
|
2016-02-08 03:59:38 +01:00
|
|
|
is_admin = user_profile.is_realm_admin,
|
2014-01-15 23:30:36 +01:00
|
|
|
can_create_streams = user_profile.can_create_streams(),
|
2014-03-27 23:50:37 +01:00
|
|
|
name_changes_disabled = name_changes_disabled(user_profile.realm),
|
2013-12-03 21:01:37 +01:00
|
|
|
has_mobile_devices = num_push_devices_for_user(user_profile) > 0,
|
2013-12-12 22:36:16 +01:00
|
|
|
autoscroll_forever = user_profile.autoscroll_forever,
|
2014-02-13 23:47:57 +01:00
|
|
|
default_desktop_notifications = user_profile.default_desktop_notifications,
|
2015-08-20 08:41:50 +02:00
|
|
|
avatar_url = avatar_url(user_profile),
|
|
|
|
mandatory_topics = user_profile.realm.mandatory_topics,
|
2015-08-20 09:11:11 +02:00
|
|
|
show_digest_email = user_profile.realm.show_digest_email,
|
2016-07-27 02:09:10 +02:00
|
|
|
presence_disabled = user_profile.realm.presence_disabled,
|
2016-07-27 01:45:29 +02:00
|
|
|
is_zephyr_mirror_realm = user_profile.realm.is_zephyr_mirror_realm,
|
2013-12-12 22:36:16 +01:00
|
|
|
)
|
2013-12-13 20:20:28 +01:00
|
|
|
if narrow_stream is not None:
|
|
|
|
# In narrow_stream context, initial pointer is just latest message
|
|
|
|
recipient = get_recipient(Recipient.STREAM, narrow_stream.id)
|
|
|
|
try:
|
|
|
|
initial_pointer = Message.objects.filter(recipient=recipient).order_by('id').reverse()[0].id
|
|
|
|
except IndexError:
|
|
|
|
initial_pointer = -1
|
|
|
|
page_params["narrow_stream"] = narrow_stream.name
|
2014-01-17 00:05:35 +01:00
|
|
|
if narrow_topic is not None:
|
|
|
|
page_params["narrow_topic"] = narrow_topic
|
2014-02-11 17:10:44 +01:00
|
|
|
page_params["narrow"] = [dict(operator=term[0], operand=term[1]) for term in narrow]
|
2013-12-13 20:20:28 +01:00
|
|
|
page_params["max_message_id"] = initial_pointer
|
|
|
|
page_params["initial_pointer"] = initial_pointer
|
|
|
|
page_params["have_initial_messages"] = (initial_pointer != -1)
|
2013-01-09 00:10:37 +01:00
|
|
|
|
2013-04-16 22:58:21 +02:00
|
|
|
statsd.incr('views.home')
|
2013-09-19 23:04:27 +02:00
|
|
|
show_invites = True
|
2013-01-03 00:19:35 +01:00
|
|
|
|
2015-08-20 21:25:30 +02:00
|
|
|
# Some realms only allow admins to invite users
|
2016-02-08 03:59:38 +01:00
|
|
|
if user_profile.realm.invite_by_admins_only and not user_profile.is_realm_admin:
|
2013-06-02 19:23:02 +02:00
|
|
|
show_invites = False
|
|
|
|
|
2014-07-15 23:21:03 +02:00
|
|
|
product_name = "Zulip"
|
|
|
|
page_params['product_name'] = product_name
|
2013-11-08 20:18:02 +01:00
|
|
|
request._log_data['extra'] = "[%s]" % (register_ret["queue_id"],)
|
2013-08-16 22:55:50 +02:00
|
|
|
response = render_to_response('zerver/index.html',
|
|
|
|
{'user_profile': user_profile,
|
2013-12-14 00:00:32 +01:00
|
|
|
'page_params' : simplejson.encoder.JSONEncoderForHTML().encode(page_params),
|
2015-12-15 03:47:39 +01:00
|
|
|
'nofontface': is_buggy_ua(request.META.get("HTTP_USER_AGENT", "Unspecified")),
|
2013-12-04 21:48:18 +01:00
|
|
|
'avatar_url': avatar_url(user_profile),
|
2013-08-16 22:55:50 +02:00
|
|
|
'show_debug':
|
|
|
|
settings.DEBUG and ('show_debug' in request.GET),
|
2016-07-04 09:33:57 +02:00
|
|
|
'pipeline': settings.PIPELINE_ENABLED,
|
2013-08-16 22:55:50 +02:00
|
|
|
'show_invites': show_invites,
|
2016-02-08 03:59:38 +01:00
|
|
|
'is_admin': user_profile.is_realm_admin,
|
2016-07-27 01:45:29 +02:00
|
|
|
'show_webathena': user_profile.realm.webathena_enabled,
|
2013-12-12 22:36:16 +01:00
|
|
|
'enable_feedback': settings.ENABLE_FEEDBACK,
|
2013-12-13 22:10:06 +01:00
|
|
|
'embedded': narrow_stream is not None,
|
2014-07-15 23:21:03 +02:00
|
|
|
'product_name': product_name
|
2013-08-16 22:55:50 +02:00
|
|
|
},
|
2016-04-21 08:48:33 +02:00
|
|
|
request=request)
|
2013-08-16 22:55:50 +02:00
|
|
|
patch_cache_control(response, no_cache=True, no_store=True, must_revalidate=True)
|
|
|
|
return response
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2016-04-21 23:48:34 +02:00
|
|
|
@zulip_login_required
|
2013-11-12 21:01:55 +01:00
|
|
|
def desktop_home(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2013-11-12 21:01:55 +01:00
|
|
|
return HttpResponseRedirect(reverse('zerver.views.home'))
|
|
|
|
|
2013-07-01 20:13:26 +02:00
|
|
|
def is_buggy_ua(agent):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (str) -> bool
|
2013-07-01 20:13:26 +02:00
|
|
|
"""Discrimiate CSS served to clients based on User Agent
|
|
|
|
|
|
|
|
Due to QTBUG-3467, @font-face is not supported in QtWebKit.
|
|
|
|
This may get fixed in the future, but for right now we can
|
|
|
|
just serve the more conservative CSS to all our desktop apps.
|
|
|
|
"""
|
2013-12-19 20:29:10 +01:00
|
|
|
return ("Humbug Desktop/" in agent or "Zulip Desktop/" in agent or "ZulipDesktop/" in agent) and \
|
2016-05-10 01:55:43 +02:00
|
|
|
"Mac" not in agent
|
2013-07-01 20:13:26 +02:00
|
|
|
|
2013-03-21 20:16:57 +01:00
|
|
|
def get_pointer_backend(request, user_profile):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, UserProfile) -> HttpResponse
|
2013-03-21 20:16:57 +01:00
|
|
|
return json_success({'pointer': user_profile.pointer})
|
|
|
|
|
2012-11-02 19:57:17 +01:00
|
|
|
@has_request_variables
|
2013-03-13 21:13:32 +01:00
|
|
|
def update_pointer_backend(request, user_profile,
|
2013-05-08 20:16:16 +02:00
|
|
|
pointer=REQ(converter=to_non_negative_int)):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, UserProfile, int) -> HttpResponse
|
2012-10-29 22:02:10 +01:00
|
|
|
if pointer <= user_profile.pointer:
|
|
|
|
return json_success()
|
|
|
|
|
2013-08-19 21:05:23 +02:00
|
|
|
try:
|
|
|
|
UserMessage.objects.get(
|
|
|
|
user_profile=user_profile,
|
|
|
|
message__id=pointer
|
|
|
|
)
|
|
|
|
except UserMessage.DoesNotExist:
|
2016-05-25 15:02:02 +02:00
|
|
|
raise JsonableError(_("Invalid message ID"))
|
2013-08-19 21:05:23 +02:00
|
|
|
|
2013-12-03 18:00:12 +01:00
|
|
|
request._log_data["extra"] = "[%s]" % (pointer,)
|
2014-01-24 23:50:24 +01:00
|
|
|
update_flags = (request.client.name.lower() in ['android', "zulipandroid"])
|
|
|
|
do_update_pointer(user_profile, pointer, update_flags=update_flags)
|
2012-10-17 23:10:34 +02:00
|
|
|
|
2012-09-05 22:21:25 +02:00
|
|
|
return json_success()
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2012-10-26 22:02:51 +02:00
|
|
|
def generate_client_id():
|
2016-06-12 12:24:27 +02:00
|
|
|
# type: () -> text_type
|
2013-08-08 16:50:58 +02:00
|
|
|
return generate_random_token(32)
|
2012-10-26 22:02:51 +02:00
|
|
|
|
2013-02-20 03:21:27 +01:00
|
|
|
def get_profile_backend(request, user_profile):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, UserProfile) -> HttpResponse
|
2012-11-07 19:05:23 +01:00
|
|
|
result = dict(pointer = user_profile.pointer,
|
|
|
|
client_id = generate_client_id(),
|
|
|
|
max_message_id = -1)
|
|
|
|
|
|
|
|
messages = Message.objects.filter(usermessage__user_profile=user_profile).order_by('-id')[:1]
|
|
|
|
if messages:
|
|
|
|
result['max_message_id'] = messages[0].id
|
|
|
|
|
|
|
|
return json_success(result)
|
2012-10-21 03:53:03 +02:00
|
|
|
|
2012-10-17 22:36:49 +02:00
|
|
|
@csrf_exempt
|
|
|
|
@require_post
|
2012-11-02 19:57:17 +01:00
|
|
|
@has_request_variables
|
2016-05-31 16:29:39 +02:00
|
|
|
def api_fetch_api_key(request, username=REQ(), password=REQ()):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, str, str) -> HttpResponse
|
2016-01-26 01:54:56 +01:00
|
|
|
return_data = {} # type: Dict[str, bool]
|
2014-01-10 23:48:05 +01:00
|
|
|
if username == "google-oauth2-token":
|
|
|
|
user_profile = authenticate(google_oauth2_token=password, return_data=return_data)
|
|
|
|
else:
|
2016-04-21 21:07:43 +02:00
|
|
|
user_profile = authenticate(username=username, password=password, return_data=return_data)
|
|
|
|
if return_data.get("inactive_user") == True:
|
2016-07-17 19:40:12 +02:00
|
|
|
return json_error(_("Your account has been disabled."),
|
|
|
|
data={"reason": "user disable"}, status=403)
|
2016-04-21 21:07:43 +02:00
|
|
|
if return_data.get("inactive_realm") == True:
|
2016-07-17 19:40:12 +02:00
|
|
|
return json_error(_("Your realm has been deactivated."),
|
|
|
|
data={"reason": "realm deactivated"}, status=403)
|
2016-04-21 21:07:43 +02:00
|
|
|
if return_data.get("password_auth_disabled") == True:
|
2016-07-17 19:40:12 +02:00
|
|
|
return json_error(_("Password auth is disabled in your team."),
|
|
|
|
data={"reason": "password auth disabled"}, status=403)
|
2013-03-29 17:39:53 +01:00
|
|
|
if user_profile is None:
|
2014-01-10 23:48:05 +01:00
|
|
|
if return_data.get("valid_attestation") == True:
|
|
|
|
# We can leak that the user is unregistered iff they present a valid authentication string for the user.
|
2016-07-17 19:40:12 +02:00
|
|
|
return json_error(_("This user is not registered; do so from a browser."),
|
|
|
|
data={"reason": "unregistered"}, status=403)
|
|
|
|
return json_error(_("Your username or password is incorrect."),
|
|
|
|
data={"reason": "incorrect_creds"}, status=403)
|
2014-01-31 19:50:19 +01:00
|
|
|
return json_success({"api_key": user_profile.api_key, "email": user_profile.email})
|
2012-10-17 22:26:59 +02:00
|
|
|
|
2016-06-21 03:32:23 +02:00
|
|
|
@csrf_exempt
|
|
|
|
def api_get_auth_backends(request):
|
|
|
|
# type: (HttpRequest) -> HttpResponse
|
|
|
|
# May return a false positive for password auth if it's been disabled
|
|
|
|
# for a specific realm. Currently only happens for zulip.com on prod
|
|
|
|
return json_success({"password": password_auth_enabled(None),
|
|
|
|
"dev": dev_auth_enabled(),
|
|
|
|
"google": google_auth_enabled(),
|
|
|
|
})
|
|
|
|
|
2012-12-02 20:51:51 +01:00
|
|
|
@authenticated_json_post_view
|
2012-11-02 19:57:17 +01:00
|
|
|
@has_request_variables
|
2013-11-05 16:46:39 +01:00
|
|
|
def json_fetch_api_key(request, user_profile, password=REQ(default='')):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, UserProfile, str) -> HttpResponse
|
2016-05-18 23:57:55 +02:00
|
|
|
if password_auth_enabled(user_profile.realm):
|
|
|
|
if not authenticate(username=user_profile.email, password=password):
|
|
|
|
return json_error(_("Your username or password is incorrect."))
|
2012-11-08 22:43:00 +01:00
|
|
|
return json_success({"api_key": user_profile.api_key})
|
2012-11-09 00:09:58 +01:00
|
|
|
|
2015-10-02 13:23:26 +02:00
|
|
|
@csrf_exempt
|
|
|
|
def api_fetch_google_client_id(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2015-10-02 13:23:26 +02:00
|
|
|
if not settings.GOOGLE_CLIENT_ID:
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("GOOGLE_CLIENT_ID is not configured"), status=400)
|
2015-10-02 13:23:26 +02:00
|
|
|
return json_success({"google_client_id": settings.GOOGLE_CLIENT_ID})
|
|
|
|
|
2013-04-02 22:42:51 +02:00
|
|
|
# Does not need to be authenticated because it's called from rest_dispatch
|
2013-03-14 23:21:53 +01:00
|
|
|
@has_request_variables
|
|
|
|
def api_events_register(request, user_profile,
|
2014-02-14 15:48:42 +01:00
|
|
|
apply_markdown=REQ(default=False, validator=check_bool),
|
2014-02-07 01:22:19 +01:00
|
|
|
all_public_streams=REQ(default=None, validator=check_bool)):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, UserProfile, bool, Optional[bool]) -> HttpResponse
|
2013-03-14 23:21:53 +01:00
|
|
|
return events_register_backend(request, user_profile,
|
2013-10-17 23:51:25 +02:00
|
|
|
apply_markdown=apply_markdown,
|
|
|
|
all_public_streams=all_public_streams)
|
2013-03-14 23:21:53 +01:00
|
|
|
|
2014-02-07 01:22:19 +01:00
|
|
|
def _default_all_public_streams(user_profile, all_public_streams):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (UserProfile, Optional[bool]) -> bool
|
2014-02-07 01:22:19 +01:00
|
|
|
if all_public_streams is not None:
|
|
|
|
return all_public_streams
|
|
|
|
else:
|
|
|
|
return user_profile.default_all_public_streams
|
|
|
|
|
|
|
|
def _default_narrow(user_profile, narrow):
|
2016-07-03 19:13:25 +02:00
|
|
|
# type: (UserProfile, Iterable[Sequence[text_type]]) -> Iterable[Sequence[text_type]]
|
2014-02-07 01:22:19 +01:00
|
|
|
default_stream = user_profile.default_events_register_stream
|
|
|
|
if not narrow and user_profile.default_events_register_stream is not None:
|
2016-06-05 04:58:33 +02:00
|
|
|
narrow = [['stream', default_stream.name]]
|
2014-02-07 01:22:19 +01:00
|
|
|
return narrow
|
|
|
|
|
2013-03-14 23:21:53 +01:00
|
|
|
@has_request_variables
|
|
|
|
def events_register_backend(request, user_profile, apply_markdown=True,
|
2014-02-07 01:22:19 +01:00
|
|
|
all_public_streams=None,
|
2013-12-13 20:51:53 +01:00
|
|
|
event_types=REQ(validator=check_list(check_string), default=None),
|
2013-12-17 23:25:36 +01:00
|
|
|
narrow=REQ(validator=check_list(check_list(check_string, length=2)), default=[]),
|
2013-08-05 22:09:12 +02:00
|
|
|
queue_lifespan_secs=REQ(converter=int, default=0)):
|
2016-07-03 19:13:25 +02:00
|
|
|
# type: (HttpRequest, UserProfile, bool, Optional[bool], Optional[Iterable[str]], Iterable[Sequence[text_type]], int) -> HttpResponse
|
2014-02-07 01:22:19 +01:00
|
|
|
all_public_streams = _default_all_public_streams(user_profile, all_public_streams)
|
|
|
|
narrow = _default_narrow(user_profile, narrow)
|
|
|
|
|
2013-05-07 17:25:25 +02:00
|
|
|
ret = do_events_register(user_profile, request.client, apply_markdown,
|
2013-12-10 16:28:16 +01:00
|
|
|
event_types, queue_lifespan_secs, all_public_streams,
|
|
|
|
narrow=narrow)
|
2013-03-21 22:43:53 +01:00
|
|
|
return json_success(ret)
|
2013-04-24 17:18:49 +02:00
|
|
|
|
2014-02-11 17:14:33 +01:00
|
|
|
|
2013-07-26 16:51:02 +02:00
|
|
|
@authenticated_json_post_view
|
|
|
|
@has_request_variables
|
2016-05-31 16:29:39 +02:00
|
|
|
def json_refer_friend(request, user_profile, email=REQ()):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, UserProfile, str) -> HttpResponse
|
2013-08-12 18:41:54 +02:00
|
|
|
if not email:
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("No email address specified"))
|
2013-07-26 16:51:02 +02:00
|
|
|
if user_profile.invites_granted - user_profile.invites_used <= 0:
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("Insufficient invites"))
|
2013-07-26 16:51:02 +02:00
|
|
|
|
|
|
|
do_refer_friend(user_profile, email);
|
|
|
|
|
|
|
|
return json_success()
|
2013-09-03 22:41:17 +02:00
|
|
|
|
2013-09-10 00:06:24 +02:00
|
|
|
@authenticated_json_post_view
|
|
|
|
@has_request_variables
|
|
|
|
def json_set_muted_topics(request, user_profile,
|
2013-12-18 18:02:10 +01:00
|
|
|
muted_topics=REQ(validator=check_list(check_list(check_string, length=2)), default=[])):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, UserProfile, List[List[text_type]]) -> HttpResponse
|
2013-09-10 00:06:24 +02:00
|
|
|
do_set_muted_topics(user_profile, muted_topics)
|
|
|
|
return json_success()
|
2013-10-15 20:19:41 +02:00
|
|
|
|
2013-11-08 17:53:41 +01:00
|
|
|
def generate_204(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2013-11-08 17:53:41 +01:00
|
|
|
return HttpResponse(content=None, status=204)
|
2013-11-25 16:47:19 +01:00
|
|
|
|
2016-06-05 04:58:33 +02:00
|
|
|
def process_unsubscribe(token, subscription_type, unsubscribe_function):
|
|
|
|
# type: (HttpRequest, str, Callable[[UserProfile], None]) -> HttpResponse
|
2013-11-25 16:47:19 +01:00
|
|
|
try:
|
|
|
|
confirmation = Confirmation.objects.get(confirmation_key=token)
|
|
|
|
except Confirmation.DoesNotExist:
|
|
|
|
return render_to_response('zerver/unsubscribe_link_error.html')
|
|
|
|
|
|
|
|
user_profile = confirmation.content_object
|
2013-11-26 00:08:17 +01:00
|
|
|
unsubscribe_function(user_profile)
|
2013-11-25 16:47:19 +01:00
|
|
|
return render_to_response('zerver/unsubscribe_success.html',
|
2016-06-05 04:58:33 +02:00
|
|
|
{"subscription_type": subscription_type,
|
2013-11-25 16:47:19 +01:00
|
|
|
"external_host": settings.EXTERNAL_HOST})
|
|
|
|
|
2013-11-26 00:08:17 +01:00
|
|
|
# Email unsubscribe functions. All have the function signature
|
|
|
|
# processor(user_profile).
|
|
|
|
|
|
|
|
def do_missedmessage_unsubscribe(user_profile):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (UserProfile) -> None
|
2013-11-26 00:08:17 +01:00
|
|
|
do_change_enable_offline_email_notifications(user_profile, False)
|
|
|
|
|
|
|
|
def do_welcome_unsubscribe(user_profile):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (UserProfile) -> None
|
2013-11-26 00:08:17 +01:00
|
|
|
clear_followup_emails_queue(user_profile.email)
|
|
|
|
|
2013-12-02 01:39:10 +01:00
|
|
|
def do_digest_unsubscribe(user_profile):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (UserProfile) -> None
|
2013-12-02 01:39:10 +01:00
|
|
|
do_change_enable_digest_emails(user_profile, False)
|
|
|
|
|
2013-11-26 23:21:03 +01:00
|
|
|
# The keys are part of the URL for the unsubscribe link and must be valid
|
|
|
|
# without encoding.
|
|
|
|
# The values are a tuple of (display name, unsubscribe function), where the
|
|
|
|
# display name is what we call this class of email in user-visible text.
|
|
|
|
email_unsubscribers = {
|
|
|
|
"missed_messages": ("missed messages", do_missedmessage_unsubscribe),
|
2013-12-02 01:39:10 +01:00
|
|
|
"welcome": ("welcome", do_welcome_unsubscribe),
|
|
|
|
"digest": ("digest", do_digest_unsubscribe)
|
2013-11-26 23:21:03 +01:00
|
|
|
}
|
2013-11-26 00:08:17 +01:00
|
|
|
|
2013-11-25 16:47:19 +01:00
|
|
|
# Login NOT required. These are for one-click unsubscribes.
|
|
|
|
def email_unsubscribe(request, type, token):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, str, str) -> HttpResponse
|
2013-11-26 00:08:17 +01:00
|
|
|
if type in email_unsubscribers:
|
2013-11-26 23:21:03 +01:00
|
|
|
display_name, unsubscribe_function = email_unsubscribers[type]
|
|
|
|
return process_unsubscribe(token, display_name, unsubscribe_function)
|
2013-11-25 16:47:19 +01:00
|
|
|
|
|
|
|
return render_to_response('zerver/unsubscribe_link_error.html', {},
|
2016-04-21 08:48:33 +02:00
|
|
|
request=request)
|