2015-08-21 02:10:41 +02:00
|
|
|
import argparse
|
2022-03-21 05:09:56 +01:00
|
|
|
import logging
|
|
|
|
from typing import Any, Optional
|
2012-12-06 23:26:47 +01:00
|
|
|
|
2022-03-21 05:09:56 +01:00
|
|
|
from django.conf import settings
|
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:
|
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
|
|
|
|
|
2022-03-21 05:16:05 +01:00
|
|
|
if "email" not in options:
|
2022-03-21 05:17:58 +01:00
|
|
|
email = input("Email: ")
|
|
|
|
else:
|
|
|
|
email = options["email"]
|
2022-03-21 05:16:05 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
validators.validate_email(email)
|
|
|
|
except ValidationError:
|
|
|
|
raise CommandError("Invalid email address.")
|
2012-12-06 23:26:47 +01:00
|
|
|
|
2022-03-21 05:17:58 +01:00
|
|
|
if "full_name" not in options:
|
|
|
|
full_name = input("Full name: ")
|
|
|
|
else:
|
|
|
|
full_name = options["full_name"]
|
|
|
|
|
2022-03-21 05:19:23 +01:00
|
|
|
if options["password_file"] is not None:
|
|
|
|
with open(options["password_file"]) as f:
|
2022-03-21 05:20:28 +01:00
|
|
|
password: Optional[str] = f.read().strip()
|
2022-03-21 05:19:23 +01:00
|
|
|
elif options["password"] is not None:
|
|
|
|
logging.warning(
|
|
|
|
"Passing password on the command line is insecure; prefer --password-file."
|
|
|
|
)
|
2022-03-21 05:20:28 +01:00
|
|
|
password = options["password"]
|
2022-03-21 05:19:23 +01:00
|
|
|
else:
|
|
|
|
# initial_password will return a random password that
|
|
|
|
# is a salted hash of the email address in a
|
|
|
|
# development environment, and None in a production
|
|
|
|
# environment.
|
|
|
|
user_initial_password = initial_password(email)
|
|
|
|
if user_initial_password is None:
|
|
|
|
logging.info("User will be created with a disabled password.")
|
2017-07-18 05:50:54 +02:00
|
|
|
else:
|
2022-03-21 05:19:23 +01:00
|
|
|
assert settings.DEVELOPMENT
|
|
|
|
logging.info("Password will be available via `./manage.py print_initial_password`.")
|
2022-03-21 05:20:28 +01:00
|
|
|
password = user_initial_password
|
2022-03-21 05:09:56 +01:00
|
|
|
|
2022-03-21 05:19:23 +01:00
|
|
|
try:
|
2020-07-16 14:10:43 +02:00
|
|
|
do_create_user(
|
|
|
|
email,
|
2022-03-21 05:20:28 +01:00
|
|
|
password,
|
2020-07-16 14:10:43 +02:00
|
|
|
realm,
|
|
|
|
full_name,
|
2022-03-21 04:43:43 +01:00
|
|
|
# Explicitly set tos_version=None. For servers that
|
|
|
|
# have configured Terms of Service, this means that
|
|
|
|
# users created via this mechanism will be prompted to
|
|
|
|
# accept the Terms of Service on first login.
|
|
|
|
tos_version=None,
|
2020-07-16 14:10:43 +02:00
|
|
|
acting_user=None,
|
|
|
|
)
|
2012-12-06 23:26:47 +01:00
|
|
|
except IntegrityError:
|
|
|
|
raise CommandError("User already exists.")
|