2016-10-12 05:13:32 +02:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import List, Optional, Set, Text
|
2016-10-12 05:13:32 +02:00
|
|
|
|
2017-11-04 00:59:22 +01:00
|
|
|
from zerver.decorator import require_realm_admin, to_non_negative_int
|
2017-10-21 03:15:12 +02:00
|
|
|
from zerver.lib.actions import do_invite_users, do_revoke_user_invite, do_resend_user_invite_email, \
|
|
|
|
get_default_subs, do_get_user_invites
|
2016-10-12 05:13:32 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables, JsonableError
|
2017-10-21 03:15:12 +02:00
|
|
|
from zerver.lib.response import json_success, json_error, json_response
|
2017-01-30 03:09:04 +01:00
|
|
|
from zerver.lib.streams import access_stream_by_name
|
2017-10-15 18:34:47 +02:00
|
|
|
from zerver.lib.validator import check_string, check_list, check_bool
|
2017-01-30 03:09:04 +01:00
|
|
|
from zerver.models import PreregistrationUser, Stream, UserProfile
|
2016-10-12 05:13:32 +02:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
@has_request_variables
|
2017-07-31 20:55:57 +02:00
|
|
|
def invite_users_backend(request, user_profile,
|
|
|
|
invitee_emails_raw=REQ("invitee_emails"),
|
2017-10-15 18:34:47 +02:00
|
|
|
invite_as_admin=REQ(validator=check_bool, default=False),
|
2017-07-31 20:55:57 +02:00
|
|
|
body=REQ("custom_body", default=None)):
|
2017-10-15 18:34:47 +02:00
|
|
|
# type: (HttpRequest, UserProfile, str, Optional[bool], Optional[str]) -> HttpResponse
|
|
|
|
|
2017-05-18 02:45:20 +02:00
|
|
|
if user_profile.realm.invite_by_admins_only and not user_profile.is_realm_admin:
|
|
|
|
return json_error(_("Must be a realm administrator"))
|
2017-10-15 18:34:47 +02:00
|
|
|
if invite_as_admin and not user_profile.is_realm_admin:
|
|
|
|
return json_error(_("Must be a realm administrator"))
|
2016-10-12 05:13:32 +02:00
|
|
|
if not invitee_emails_raw:
|
|
|
|
return json_error(_("You must specify at least one email address."))
|
2017-02-12 21:21:31 +01:00
|
|
|
if body == '':
|
|
|
|
body = None
|
2016-10-12 05:13:32 +02:00
|
|
|
|
|
|
|
invitee_emails = get_invitee_emails_set(invitee_emails_raw)
|
|
|
|
|
|
|
|
stream_names = request.POST.getlist('stream')
|
|
|
|
if not stream_names:
|
|
|
|
return json_error(_("You must specify at least one stream for invitees to join."))
|
|
|
|
|
|
|
|
# We unconditionally sub you to the notifications stream if it
|
|
|
|
# exists and is public.
|
2017-02-11 05:45:39 +01:00
|
|
|
notifications_stream = user_profile.realm.notifications_stream # type: Optional[Stream]
|
2016-10-12 05:13:32 +02:00
|
|
|
if notifications_stream and not notifications_stream.invite_only:
|
|
|
|
stream_names.append(notifications_stream.name)
|
|
|
|
|
2017-05-17 22:10:59 +02:00
|
|
|
streams = [] # type: List[Stream]
|
2016-10-12 05:13:32 +02:00
|
|
|
for stream_name in stream_names:
|
2017-01-30 03:09:04 +01:00
|
|
|
try:
|
|
|
|
(stream, recipient, sub) = access_stream_by_name(user_profile, stream_name)
|
|
|
|
except JsonableError:
|
2016-10-12 05:13:32 +02:00
|
|
|
return json_error(_("Stream does not exist: %s. No invites were sent.") % (stream_name,))
|
|
|
|
streams.append(stream)
|
|
|
|
|
2017-10-15 18:34:47 +02:00
|
|
|
do_invite_users(user_profile, invitee_emails, streams, invite_as_admin, body)
|
2017-07-25 02:02:30 +02:00
|
|
|
return json_success()
|
2016-10-12 05:13:32 +02:00
|
|
|
|
|
|
|
def get_invitee_emails_set(invitee_emails_raw):
|
|
|
|
# type: (str) -> Set[str]
|
|
|
|
invitee_emails_list = set(re.split(r'[,\n]', invitee_emails_raw))
|
|
|
|
invitee_emails = set()
|
|
|
|
for email in invitee_emails_list:
|
|
|
|
is_email_with_name = re.search(r'<(?P<email>.*)>', email)
|
|
|
|
if is_email_with_name:
|
|
|
|
email = is_email_with_name.group('email')
|
|
|
|
invitee_emails.add(email.strip())
|
|
|
|
return invitee_emails
|
2017-10-21 03:15:12 +02:00
|
|
|
|
|
|
|
@require_realm_admin
|
|
|
|
def get_user_invites(request, user_profile):
|
|
|
|
# type: (HttpRequest, UserProfile) -> HttpResponse
|
|
|
|
all_users = do_get_user_invites(user_profile)
|
|
|
|
return json_success({'invites': all_users})
|
|
|
|
|
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
|
|
|
def revoke_user_invite(request, user_profile, prereg_id):
|
|
|
|
# type: (HttpRequest, UserProfile, int) -> HttpResponse
|
|
|
|
do_revoke_user_invite(prereg_id, user_profile.realm_id)
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
|
|
|
def resend_user_invite_email(request, user_profile, prereg_id):
|
|
|
|
# type: (HttpRequest, UserProfile, int) -> HttpResponse
|
|
|
|
timestamp = do_resend_user_invite_email(prereg_id, user_profile.realm_id)
|
|
|
|
return json_success({'timestamp': timestamp})
|