2015-08-21 02:10:41 +02:00
|
|
|
import argparse
|
2017-11-16 00:43:27 +01:00
|
|
|
import sys
|
|
|
|
from typing import Any
|
2012-12-06 23:26:47 +01:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
from django.core import validators
|
2012-12-06 23:26:47 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
2017-11-16 00:43:27 +01:00
|
|
|
from django.core.management.base import CommandError
|
2012-12-06 23:26:47 +01:00
|
|
|
from django.db.utils import IntegrityError
|
|
|
|
|
2018-12-17 07:22:52 +01:00
|
|
|
from zerver.lib.actions import do_create_user
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.initial_password import initial_password
|
2017-08-07 17:15:11 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2012-12-06 23:26:47 +01:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-08-07 17:15:11 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2013-01-08 23:05:42 +01:00
|
|
|
help = """Create the specified user with a default initial password.
|
|
|
|
|
2022-03-21 04:32:20 +01:00
|
|
|
Sets tos_version=None, so that the user needs to do a ToS flow on login.
|
2015-08-21 02:10:41 +02:00
|
|
|
|
|
|
|
Omit both <email> and <full name> for interactive user creation.
|
2013-01-08 23:05:42 +01:00
|
|
|
"""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: argparse.ArgumentParser) -> None:
|
2022-03-21 04:32:20 +01:00
|
|
|
self.add_create_user_args(parser)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.add_realm_args(
|
2021-05-10 21:29:25 +02:00
|
|
|
parser, required=True, help="The name of the existing realm to which to add the user."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2012-12-06 23:26:47 +01:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2013-01-08 23:05:42 +01:00
|
|
|
if not options["tos"]:
|
2021-02-12 08:19:30 +01:00
|
|
|
raise CommandError(
|
|
|
|
"""You must confirm that this user has accepted the
|
|
|
|
Terms of Service by passing --this-user-has-accepted-the-tos."""
|
|
|
|
)
|
2017-08-07 17:15:11 +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
|
|
|
|
|
2012-12-06 23:26:47 +01:00
|
|
|
try:
|
2021-02-12 08:20:45 +01:00
|
|
|
email = options["email"]
|
|
|
|
full_name = options["full_name"]
|
2012-12-06 23:26:47 +01:00
|
|
|
try:
|
|
|
|
validators.validate_email(email)
|
|
|
|
except ValidationError:
|
|
|
|
raise CommandError("Invalid email address.")
|
2015-08-21 02:10:41 +02:00
|
|
|
except KeyError:
|
2021-02-12 08:20:45 +01:00
|
|
|
if "email" in options or "full_name" in options:
|
2021-02-12 08:19:30 +01:00
|
|
|
raise CommandError(
|
|
|
|
"""Either specify an email and full name as two
|
|
|
|
parameters, or specify no parameters for interactive user creation."""
|
|
|
|
)
|
2012-12-06 23:26:47 +01:00
|
|
|
else:
|
|
|
|
while True:
|
2015-11-01 17:14:42 +01:00
|
|
|
email = input("Email: ")
|
2012-12-06 23:26:47 +01:00
|
|
|
try:
|
|
|
|
validators.validate_email(email)
|
|
|
|
break
|
|
|
|
except ValidationError:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Invalid email address.", file=sys.stderr)
|
2015-11-01 17:14:42 +01:00
|
|
|
full_name = input("Full name: ")
|
2012-12-06 23:26:47 +01:00
|
|
|
|
|
|
|
try:
|
2021-02-12 08:20:45 +01:00
|
|
|
if options["password_file"] is not None:
|
|
|
|
with open(options["password_file"]) as f:
|
2020-10-09 20:57:37 +02:00
|
|
|
pw = f.read().strip()
|
2021-02-12 08:20:45 +01:00
|
|
|
elif options["password"] is not None:
|
|
|
|
pw = options["password"]
|
2017-07-18 05:50:54 +02:00
|
|
|
else:
|
2018-03-24 23:40:54 +01:00
|
|
|
user_initial_password = initial_password(email)
|
2018-03-24 23:47:11 +01:00
|
|
|
if user_initial_password is None:
|
|
|
|
raise CommandError("Password is unusable.")
|
2019-06-20 14:15:10 +02:00
|
|
|
pw = user_initial_password
|
2020-07-16 14:10:43 +02:00
|
|
|
do_create_user(
|
|
|
|
email,
|
|
|
|
pw,
|
|
|
|
realm,
|
|
|
|
full_name,
|
|
|
|
acting_user=None,
|
|
|
|
)
|
2012-12-06 23:26:47 +01:00
|
|
|
except IntegrityError:
|
|
|
|
raise CommandError("User already exists.")
|