2013-04-23 18:51:17 +02:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2017-07-10 21:06:28 +02:00
|
|
|
from django.core.management.base import CommandError
|
2017-11-16 00:43:27 +01:00
|
|
|
|
2017-07-08 04:38:13 +02:00
|
|
|
from confirmation.models import Confirmation, create_confirmation_link
|
2017-07-10 21:06:28 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2018-03-14 12:54:05 +01:00
|
|
|
from zerver.models import PreregistrationUser, email_allowed_for_realm, \
|
2019-02-23 00:47:38 +01:00
|
|
|
DomainNotAllowedForRealmError
|
2013-02-28 20:07:54 +01:00
|
|
|
|
2017-07-10 21:06:28 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2013-02-28 20:07:54 +01:00
|
|
|
help = "Generate activation links for users and print them to stdout."
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('--force',
|
|
|
|
dest='force',
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help='Override that the domain is restricted to external users.')
|
|
|
|
parser.add_argument('emails', metavar='<email>', type=str, nargs='*',
|
2017-07-10 21:06:28 +02:00
|
|
|
help='email of users to generate an activation link for')
|
|
|
|
self.add_realm_args(parser, True)
|
2013-09-19 19:38:34 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2013-02-28 20:07:54 +01:00
|
|
|
duplicates = False
|
2017-07-10 21:06:28 +02:00
|
|
|
realm = self.get_realm(options)
|
2017-09-26 01:25:39 +02:00
|
|
|
assert realm is not None # Should be ensured by parser
|
2017-07-10 21:06:28 +02:00
|
|
|
|
|
|
|
if not options['emails']:
|
|
|
|
self.print_help("./manage.py", "generate_invite_links")
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError
|
2017-07-10 21:06:28 +02:00
|
|
|
|
2015-08-21 02:10:41 +02:00
|
|
|
for email in options['emails']:
|
2013-02-28 20:07:54 +01:00
|
|
|
try:
|
2017-07-10 21:06:28 +02:00
|
|
|
self.get_user(email, realm)
|
2015-11-01 17:11:06 +01:00
|
|
|
print(email + ": There is already a user registered with that address.")
|
2013-02-28 20:07:54 +01:00
|
|
|
duplicates = True
|
|
|
|
continue
|
2017-07-10 21:06:28 +02:00
|
|
|
except CommandError:
|
2013-02-28 20:07:54 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
if duplicates:
|
|
|
|
return
|
|
|
|
|
2015-08-21 02:10:41 +02:00
|
|
|
for email in options['emails']:
|
2018-03-14 12:54:05 +01:00
|
|
|
try:
|
|
|
|
email_allowed_for_realm(email, realm)
|
|
|
|
except DomainNotAllowedForRealmError:
|
|
|
|
if not options["force"]:
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError("You've asked to add an external user '{}' to a "
|
|
|
|
"closed realm '{}'.\nAre you sure? To do this, "
|
|
|
|
"pass --force.".format(email, realm.string_id))
|
2017-07-10 21:06:28 +02:00
|
|
|
|
|
|
|
prereg_user = PreregistrationUser(email=email, realm=realm)
|
2013-05-30 20:13:41 +02:00
|
|
|
prereg_user.save()
|
2017-07-08 04:38:13 +02:00
|
|
|
print(email + ": " + create_confirmation_link(prereg_user, realm.host,
|
|
|
|
Confirmation.INVITATION))
|