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
|
|
|
|
from zerver.models import PreregistrationUser, email_allowed_for_realm
|
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")
|
|
|
|
exit(1)
|
|
|
|
|
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']:
|
2017-07-10 21:06:28 +02:00
|
|
|
if not email_allowed_for_realm(email, realm) and not options["force"]:
|
|
|
|
print("You've asked to add an external user '%s' to a closed realm '%s'." % (
|
|
|
|
email, realm.string_id))
|
|
|
|
print("Are you sure? To do this, pass --force.")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
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))
|