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
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.email_validation import email_allowed_for_realm
|
2017-07-10 21:06:28 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2020-03-05 13:54:37 +01:00
|
|
|
from zerver.models import DomainNotAllowedForRealmError, PreregistrationUser
|
2020-01-14 21:59:46 +01:00
|
|
|
|
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:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--force",
|
2021-02-12 08:19:30 +01:00
|
|
|
action="store_true",
|
2021-02-12 08:20:45 +01:00
|
|
|
help="Override that the domain is restricted to external users.",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"emails",
|
|
|
|
metavar="<email>",
|
|
|
|
nargs="*",
|
|
|
|
help="email of users to generate an activation link for",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-07-10 21:06:28 +02:00
|
|
|
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
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if not options["emails"]:
|
2017-07-10 21:06:28 +02:00
|
|
|
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
|
|
|
|
2021-02-12 08:20:45 +01: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
|
|
|
|
|
2021-02-12 08:20:45 +01: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"]:
|
2021-02-12 08:19:30 +01: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()
|
2021-02-12 08:19:30 +01:00
|
|
|
print(email + ": " + create_confirmation_link(prereg_user, Confirmation.INVITATION))
|