2015-11-23 17:09:21 +01:00
|
|
|
from __future__ import absolute_import
|
2016-12-21 05:15:55 +01:00
|
|
|
from typing import Optional, Any
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Dict, Text
|
2015-11-23 17:09:21 +01:00
|
|
|
|
2016-05-25 15:02:02 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
2015-11-23 17:09:21 +01:00
|
|
|
from django.conf import settings
|
2016-11-17 08:56:01 +01:00
|
|
|
from django.contrib.auth import authenticate, update_session_auth_hash
|
2017-01-20 12:27:38 +01:00
|
|
|
from django.core.mail import send_mail
|
2016-06-05 02:15:26 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2017-01-20 12:27:38 +01:00
|
|
|
from django.shortcuts import redirect, render
|
|
|
|
from django.template.loader import render_to_string
|
|
|
|
from django.urls import reverse
|
2015-11-23 17:09:21 +01:00
|
|
|
|
2017-03-04 09:16:48 +01:00
|
|
|
from zerver.decorator import authenticated_json_post_view, has_request_variables, \
|
2017-04-16 22:10:56 +02:00
|
|
|
zulip_login_required, REQ, human_users_only
|
2015-11-23 17:09:21 +01:00
|
|
|
from zerver.lib.actions import do_change_password, \
|
2017-02-08 04:39:55 +01:00
|
|
|
do_change_enable_desktop_notifications, \
|
2015-11-23 17:09:21 +01:00
|
|
|
do_change_enter_sends, do_change_enable_sounds, \
|
|
|
|
do_change_enable_offline_email_notifications, do_change_enable_digest_emails, \
|
2016-09-19 22:55:18 +02:00
|
|
|
do_change_enable_offline_push_notifications, do_change_enable_online_push_notifications, \
|
|
|
|
do_change_default_desktop_notifications, do_change_autoscroll_forever, \
|
2015-11-23 17:09:21 +01:00
|
|
|
do_change_enable_stream_desktop_notifications, do_change_enable_stream_sounds, \
|
2017-04-07 00:05:55 +02:00
|
|
|
do_regenerate_api_key, do_change_avatar_fields, do_set_user_display_setting, \
|
2017-01-20 12:27:38 +01:00
|
|
|
do_change_pm_content_in_desktop_notifications, validate_email, \
|
2017-04-07 00:05:55 +02:00
|
|
|
do_change_user_email, do_start_email_change_process
|
2015-11-23 17:09:21 +01:00
|
|
|
from zerver.lib.avatar import avatar_url
|
2016-08-05 22:28:25 +02:00
|
|
|
from zerver.lib.i18n import get_available_language_codes
|
2015-11-23 17:09:21 +01:00
|
|
|
from zerver.lib.response import json_success, json_error
|
|
|
|
from zerver.lib.upload import upload_avatar_image
|
2016-06-23 11:32:45 +02:00
|
|
|
from zerver.lib.validator import check_bool, check_string
|
2016-07-31 10:02:42 +02:00
|
|
|
from zerver.lib.request import JsonableError
|
2017-02-08 04:39:55 +01:00
|
|
|
from zerver.lib.users import check_change_full_name
|
2017-03-14 10:53:09 +01:00
|
|
|
from zerver.lib.timezone import get_all_timezones
|
2017-01-20 12:27:38 +01:00
|
|
|
from zerver.models import UserProfile, Realm, name_changes_disabled, \
|
|
|
|
EmailChangeStatus
|
|
|
|
from confirmation.models import EmailChangeConfirmation
|
|
|
|
|
2017-03-04 09:16:48 +01:00
|
|
|
@zulip_login_required
|
2017-01-20 12:27:38 +01:00
|
|
|
def confirm_email_change(request, confirmation_key):
|
|
|
|
# type: (HttpRequest, str) -> HttpResponse
|
2017-03-04 06:39:45 +01:00
|
|
|
user_profile = request.user
|
|
|
|
if user_profile.realm.email_changes_disabled:
|
2017-03-05 02:18:42 +01:00
|
|
|
raise JsonableError(_("Email address changes are disabled in this organization."))
|
2017-03-04 06:39:45 +01:00
|
|
|
|
2017-01-20 12:27:38 +01:00
|
|
|
confirmation_key = confirmation_key.lower()
|
|
|
|
obj = EmailChangeConfirmation.objects.confirm(confirmation_key)
|
|
|
|
confirmed = False
|
|
|
|
new_email = old_email = None # type: Text
|
|
|
|
if obj:
|
|
|
|
confirmed = True
|
|
|
|
assert isinstance(obj, EmailChangeStatus)
|
|
|
|
new_email = obj.new_email
|
|
|
|
old_email = obj.old_email
|
|
|
|
|
|
|
|
do_change_user_email(obj.user_profile, obj.new_email)
|
|
|
|
|
|
|
|
context = {'support_email': settings.ZULIP_ADMINISTRATOR,
|
|
|
|
'verbose_support_offers': settings.VERBOSE_SUPPORT_OFFERS,
|
|
|
|
'realm': obj.realm,
|
|
|
|
'new_email': new_email,
|
|
|
|
}
|
|
|
|
subject = render_to_string(
|
|
|
|
'confirmation/notify_change_in_email_subject.txt', context)
|
|
|
|
body = render_to_string(
|
|
|
|
'confirmation/notify_change_in_email_body.txt', context)
|
|
|
|
send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, [old_email])
|
|
|
|
|
|
|
|
ctx = {
|
|
|
|
'confirmed': confirmed,
|
|
|
|
'support_email': settings.ZULIP_ADMINISTRATOR,
|
|
|
|
'verbose_support_offers': settings.VERBOSE_SUPPORT_OFFERS,
|
|
|
|
'new_email': new_email,
|
|
|
|
'old_email': old_email,
|
|
|
|
}
|
|
|
|
return render(request, 'confirmation/confirm_email_change.html', context=ctx)
|
2015-11-23 17:09:21 +01:00
|
|
|
|
2017-04-16 22:10:56 +02:00
|
|
|
@human_users_only
|
2015-11-23 17:09:21 +01:00
|
|
|
@has_request_variables
|
|
|
|
def json_change_ui_settings(request, user_profile,
|
|
|
|
autoscroll_forever=REQ(validator=check_bool,
|
|
|
|
default=None),
|
|
|
|
default_desktop_notifications=REQ(validator=check_bool,
|
|
|
|
default=None)):
|
2016-06-05 02:15:26 +02:00
|
|
|
# type: (HttpRequest, UserProfile, Optional[bool], Optional[bool]) -> HttpResponse
|
2015-11-23 17:09:21 +01:00
|
|
|
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
if autoscroll_forever is not None and \
|
|
|
|
user_profile.autoscroll_forever != autoscroll_forever:
|
|
|
|
do_change_autoscroll_forever(user_profile, autoscroll_forever)
|
|
|
|
result['autoscroll_forever'] = autoscroll_forever
|
|
|
|
|
|
|
|
if default_desktop_notifications is not None and \
|
|
|
|
user_profile.default_desktop_notifications != default_desktop_notifications:
|
|
|
|
do_change_default_desktop_notifications(user_profile, default_desktop_notifications)
|
|
|
|
result['default_desktop_notifications'] = default_desktop_notifications
|
|
|
|
|
|
|
|
return json_success(result)
|
|
|
|
|
|
|
|
@authenticated_json_post_view
|
|
|
|
@has_request_variables
|
|
|
|
def json_change_settings(request, user_profile,
|
2016-07-23 22:13:53 +02:00
|
|
|
full_name=REQ(default=""),
|
2017-01-20 12:27:38 +01:00
|
|
|
email=REQ(default=""),
|
2015-11-23 17:09:21 +01:00
|
|
|
old_password=REQ(default=""),
|
|
|
|
new_password=REQ(default=""),
|
|
|
|
confirm_password=REQ(default="")):
|
2017-01-20 12:27:38 +01:00
|
|
|
# type: (HttpRequest, UserProfile, Text, Text, Text, Text, Text) -> HttpResponse
|
|
|
|
if not (full_name or new_password or email):
|
2016-07-23 22:22:25 +02:00
|
|
|
return json_error(_("No new data supplied"))
|
|
|
|
|
2015-11-23 17:09:21 +01:00
|
|
|
if new_password != "" or confirm_password != "":
|
|
|
|
if new_password != confirm_password:
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("New password must match confirmation password!"))
|
2015-11-23 17:09:21 +01:00
|
|
|
if not authenticate(username=user_profile.email, password=old_password):
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("Wrong password!"))
|
2015-11-23 17:09:21 +01:00
|
|
|
do_change_password(user_profile, new_password)
|
2016-11-17 08:56:01 +01:00
|
|
|
# In Django 1.10, password changes invalidates sessions, see
|
|
|
|
# https://docs.djangoproject.com/en/1.10/topics/auth/default/#session-invalidation-on-password-change
|
|
|
|
# for details. To avoid this logging the user out of his own
|
|
|
|
# session (which would provide a confusing UX at best), we
|
|
|
|
# update the session hash here.
|
|
|
|
update_session_auth_hash(request, user_profile)
|
2016-12-16 11:38:21 +01:00
|
|
|
# We also save the session to the DB immediately to mitigate
|
|
|
|
# race conditions. In theory, there is still a race condition
|
|
|
|
# and to completely avoid it we will have to use some kind of
|
|
|
|
# mutex lock in `django.contrib.auth.get_user` where session
|
|
|
|
# is verified. To make that lock work we will have to control
|
|
|
|
# the AuthenticationMiddleware which is currently controlled
|
|
|
|
# by Django,
|
|
|
|
request.session.save()
|
2015-11-23 17:09:21 +01:00
|
|
|
|
|
|
|
result = {}
|
2017-01-20 12:27:38 +01:00
|
|
|
new_email = email.strip()
|
|
|
|
if user_profile.email != email and new_email != '':
|
2017-03-04 06:39:45 +01:00
|
|
|
if user_profile.realm.email_changes_disabled:
|
2017-03-05 02:18:42 +01:00
|
|
|
return json_error(_("Email address changes are disabled in this organization."))
|
2017-01-20 12:27:38 +01:00
|
|
|
error, skipped = validate_email(user_profile, new_email)
|
|
|
|
if error or skipped:
|
|
|
|
return json_error(error or skipped)
|
|
|
|
|
|
|
|
do_start_email_change_process(user_profile, new_email)
|
|
|
|
result['account_email'] = _('We have sent you an email on your '
|
|
|
|
'new email address for confirmation.')
|
|
|
|
|
2015-11-23 17:09:21 +01:00
|
|
|
if user_profile.full_name != full_name and full_name.strip() != "":
|
|
|
|
if name_changes_disabled(user_profile.realm):
|
|
|
|
# Failingly silently is fine -- they can't do it through the UI, so
|
|
|
|
# they'd have to be trying to break the rules.
|
|
|
|
pass
|
|
|
|
else:
|
2017-02-08 04:39:55 +01:00
|
|
|
# Note that check_change_full_name strips the passed name automatically
|
|
|
|
result['full_name'] = check_change_full_name(user_profile, full_name)
|
2015-11-23 17:09:21 +01:00
|
|
|
|
|
|
|
return json_success(result)
|
|
|
|
|
2017-04-16 22:10:56 +02:00
|
|
|
@human_users_only
|
2016-06-23 11:32:45 +02:00
|
|
|
@has_request_variables
|
2016-12-22 15:57:06 +01:00
|
|
|
def update_display_settings_backend(request, user_profile,
|
2016-12-21 05:15:55 +01:00
|
|
|
twenty_four_hour_time=REQ(validator=check_bool, default=None),
|
2016-12-24 11:44:41 +01:00
|
|
|
default_language=REQ(validator=check_string, default=None),
|
2017-03-02 08:30:53 +01:00
|
|
|
left_side_userlist=REQ(validator=check_bool, default=None),
|
2017-03-14 10:53:09 +01:00
|
|
|
emoji_alt_code=REQ(validator=check_bool, default=None),
|
|
|
|
timezone=REQ(validator=check_string, default=None)):
|
|
|
|
# type: (HttpRequest, UserProfile, Optional[bool], Optional[str], Optional[bool], Optional[bool], Optional[Text]) -> HttpResponse
|
2016-12-22 15:58:18 +01:00
|
|
|
if (default_language is not None and
|
|
|
|
default_language not in get_available_language_codes()):
|
|
|
|
raise JsonableError(_("Invalid language '%s'" % (default_language,)))
|
|
|
|
|
2017-03-14 10:53:09 +01:00
|
|
|
if (timezone is not None and
|
|
|
|
timezone not in get_all_timezones()):
|
|
|
|
raise JsonableError(_("Invalid timezone '%s'" % (timezone,)))
|
|
|
|
|
2016-12-21 05:15:55 +01:00
|
|
|
result = {} # type: Dict[str, Any]
|
2016-06-23 11:32:45 +02:00
|
|
|
if (default_language is not None and
|
|
|
|
user_profile.default_language != default_language):
|
2017-04-07 00:05:55 +02:00
|
|
|
do_set_user_display_setting(user_profile, "default_language", default_language)
|
2016-12-22 15:58:18 +01:00
|
|
|
result['default_language'] = default_language
|
2016-06-23 11:32:45 +02:00
|
|
|
|
2016-12-21 05:15:55 +01:00
|
|
|
elif (twenty_four_hour_time is not None and
|
|
|
|
user_profile.twenty_four_hour_time != twenty_four_hour_time):
|
2017-04-07 00:05:55 +02:00
|
|
|
do_set_user_display_setting(user_profile, "twenty_four_hour_time", twenty_four_hour_time)
|
2016-12-21 05:15:55 +01:00
|
|
|
result['twenty_four_hour_time'] = twenty_four_hour_time
|
|
|
|
|
2016-12-24 11:44:41 +01:00
|
|
|
elif (left_side_userlist is not None and
|
|
|
|
user_profile.left_side_userlist != left_side_userlist):
|
2017-04-07 00:05:55 +02:00
|
|
|
do_set_user_display_setting(user_profile, "left_side_userlist", left_side_userlist)
|
2016-12-24 11:44:41 +01:00
|
|
|
result['left_side_userlist'] = left_side_userlist
|
|
|
|
|
2017-03-02 08:30:53 +01:00
|
|
|
elif (emoji_alt_code is not None and
|
|
|
|
user_profile.emoji_alt_code != emoji_alt_code):
|
2017-04-07 00:05:55 +02:00
|
|
|
do_set_user_display_setting(user_profile, "emoji_alt_code", emoji_alt_code)
|
2017-03-02 08:30:53 +01:00
|
|
|
result['emoji_alt_code'] = emoji_alt_code
|
|
|
|
|
2017-03-14 10:53:09 +01:00
|
|
|
elif (timezone is not None and
|
|
|
|
user_profile.timezone != timezone):
|
2017-04-07 00:05:55 +02:00
|
|
|
do_set_user_display_setting(user_profile, "timezone", timezone)
|
2017-03-14 10:53:09 +01:00
|
|
|
result['timezone'] = timezone
|
|
|
|
|
2016-06-23 11:32:45 +02:00
|
|
|
return json_success(result)
|
|
|
|
|
2017-04-16 22:10:56 +02:00
|
|
|
@human_users_only
|
2015-11-23 17:09:21 +01:00
|
|
|
@has_request_variables
|
|
|
|
def json_change_notify_settings(request, user_profile,
|
|
|
|
enable_stream_desktop_notifications=REQ(validator=check_bool,
|
|
|
|
default=None),
|
|
|
|
enable_stream_sounds=REQ(validator=check_bool,
|
|
|
|
default=None),
|
|
|
|
enable_desktop_notifications=REQ(validator=check_bool,
|
|
|
|
default=None),
|
|
|
|
enable_sounds=REQ(validator=check_bool,
|
|
|
|
default=None),
|
|
|
|
enable_offline_email_notifications=REQ(validator=check_bool,
|
|
|
|
default=None),
|
|
|
|
enable_offline_push_notifications=REQ(validator=check_bool,
|
|
|
|
default=None),
|
2016-09-19 22:55:18 +02:00
|
|
|
enable_online_push_notifications=REQ(validator=check_bool,
|
|
|
|
default=None),
|
2015-11-23 17:09:21 +01:00
|
|
|
enable_digest_emails=REQ(validator=check_bool,
|
2016-12-07 17:29:12 +01:00
|
|
|
default=None),
|
|
|
|
pm_content_in_desktop_notifications=REQ(validator=check_bool,
|
2016-12-30 23:31:04 +01:00
|
|
|
default=None)):
|
2016-12-07 17:29:12 +01:00
|
|
|
# type: (HttpRequest, UserProfile, Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool]) -> HttpResponse
|
2015-11-23 17:09:21 +01:00
|
|
|
result = {}
|
|
|
|
|
|
|
|
# Stream notification settings.
|
|
|
|
|
|
|
|
if enable_stream_desktop_notifications is not None and \
|
|
|
|
user_profile.enable_stream_desktop_notifications != enable_stream_desktop_notifications:
|
|
|
|
do_change_enable_stream_desktop_notifications(
|
|
|
|
user_profile, enable_stream_desktop_notifications)
|
|
|
|
result['enable_stream_desktop_notifications'] = enable_stream_desktop_notifications
|
|
|
|
|
|
|
|
if enable_stream_sounds is not None and \
|
|
|
|
user_profile.enable_stream_sounds != enable_stream_sounds:
|
|
|
|
do_change_enable_stream_sounds(user_profile, enable_stream_sounds)
|
|
|
|
result['enable_stream_sounds'] = enable_stream_sounds
|
|
|
|
|
|
|
|
# PM and @-mention settings.
|
|
|
|
|
|
|
|
if enable_desktop_notifications is not None and \
|
|
|
|
user_profile.enable_desktop_notifications != enable_desktop_notifications:
|
|
|
|
do_change_enable_desktop_notifications(user_profile, enable_desktop_notifications)
|
|
|
|
result['enable_desktop_notifications'] = enable_desktop_notifications
|
|
|
|
|
|
|
|
if enable_sounds is not None and \
|
|
|
|
user_profile.enable_sounds != enable_sounds:
|
|
|
|
do_change_enable_sounds(user_profile, enable_sounds)
|
|
|
|
result['enable_sounds'] = enable_sounds
|
|
|
|
|
|
|
|
if enable_offline_email_notifications is not None and \
|
|
|
|
user_profile.enable_offline_email_notifications != enable_offline_email_notifications:
|
|
|
|
do_change_enable_offline_email_notifications(user_profile, enable_offline_email_notifications)
|
|
|
|
result['enable_offline_email_notifications'] = enable_offline_email_notifications
|
|
|
|
|
|
|
|
if enable_offline_push_notifications is not None and \
|
|
|
|
user_profile.enable_offline_push_notifications != enable_offline_push_notifications:
|
|
|
|
do_change_enable_offline_push_notifications(user_profile, enable_offline_push_notifications)
|
|
|
|
result['enable_offline_push_notifications'] = enable_offline_push_notifications
|
|
|
|
|
2016-09-19 22:55:18 +02:00
|
|
|
if enable_online_push_notifications is not None and \
|
|
|
|
user_profile.enable_online_push_notifications != enable_online_push_notifications:
|
|
|
|
do_change_enable_online_push_notifications(user_profile, enable_online_push_notifications)
|
|
|
|
result['enable_online_push_notifications'] = enable_online_push_notifications
|
|
|
|
|
2015-11-23 17:09:21 +01:00
|
|
|
if enable_digest_emails is not None and \
|
|
|
|
user_profile.enable_digest_emails != enable_digest_emails:
|
|
|
|
do_change_enable_digest_emails(user_profile, enable_digest_emails)
|
|
|
|
result['enable_digest_emails'] = enable_digest_emails
|
|
|
|
|
2016-12-07 17:29:12 +01:00
|
|
|
if pm_content_in_desktop_notifications is not None and \
|
|
|
|
user_profile.pm_content_in_desktop_notifications != pm_content_in_desktop_notifications:
|
|
|
|
do_change_pm_content_in_desktop_notifications(user_profile, pm_content_in_desktop_notifications)
|
|
|
|
result['pm_content_in_desktop_notifications'] = pm_content_in_desktop_notifications
|
|
|
|
|
2015-11-23 17:09:21 +01:00
|
|
|
return json_success(result)
|
|
|
|
|
2016-12-21 21:29:46 +01:00
|
|
|
def set_avatar_backend(request, user_profile):
|
2016-06-05 02:15:26 +02:00
|
|
|
# type: (HttpRequest, UserProfile) -> HttpResponse
|
2015-11-23 17:09:21 +01:00
|
|
|
if len(request.FILES) != 1:
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("You must upload exactly one avatar."))
|
2015-11-23 17:09:21 +01:00
|
|
|
|
2016-01-25 01:27:18 +01:00
|
|
|
user_file = list(request.FILES.values())[0]
|
2017-03-06 06:22:28 +01:00
|
|
|
if ((settings.MAX_AVATAR_FILE_SIZE * 1024 * 1024) < user_file.size):
|
|
|
|
return json_error(_("Uploaded file is larger than the allowed limit of %s MB") % (
|
|
|
|
settings.MAX_AVATAR_FILE_SIZE))
|
2017-03-02 16:21:46 +01:00
|
|
|
upload_avatar_image(user_file, user_profile, user_profile)
|
2017-01-28 19:05:20 +01:00
|
|
|
do_change_avatar_fields(user_profile, UserProfile.AVATAR_FROM_USER)
|
2015-11-23 17:09:21 +01:00
|
|
|
user_avatar_url = avatar_url(user_profile)
|
|
|
|
|
|
|
|
json_result = dict(
|
|
|
|
avatar_url = user_avatar_url
|
|
|
|
)
|
|
|
|
return json_success(json_result)
|
|
|
|
|
2016-12-21 18:34:03 +01:00
|
|
|
def delete_avatar_backend(request, user_profile):
|
|
|
|
# type: (HttpRequest, UserProfile) -> HttpResponse
|
2017-01-28 19:05:20 +01:00
|
|
|
do_change_avatar_fields(user_profile, UserProfile.AVATAR_FROM_GRAVATAR)
|
2016-12-21 18:34:03 +01:00
|
|
|
gravatar_url = avatar_url(user_profile)
|
|
|
|
|
|
|
|
json_result = dict(
|
|
|
|
avatar_url = gravatar_url
|
|
|
|
)
|
|
|
|
return json_success(json_result)
|
|
|
|
|
2015-11-23 17:09:21 +01:00
|
|
|
@has_request_variables
|
|
|
|
def regenerate_api_key(request, user_profile):
|
2016-06-05 02:15:26 +02:00
|
|
|
# type: (HttpRequest, UserProfile) -> HttpResponse
|
2015-11-23 17:09:21 +01:00
|
|
|
do_regenerate_api_key(user_profile)
|
|
|
|
json_result = dict(
|
|
|
|
api_key = user_profile.api_key
|
|
|
|
)
|
|
|
|
return json_success(json_result)
|
|
|
|
|
|
|
|
@has_request_variables
|
2016-04-08 21:39:00 +02:00
|
|
|
def change_enter_sends(request, user_profile,
|
2016-06-16 09:40:25 +02:00
|
|
|
enter_sends=REQ(validator=check_bool)):
|
2016-06-05 02:15:26 +02:00
|
|
|
# type: (HttpRequest, UserProfile, bool) -> HttpResponse
|
2015-11-23 17:09:21 +01:00
|
|
|
do_change_enter_sends(user_profile, enter_sends)
|
|
|
|
return json_success()
|