2015-08-21 02:10:41 +02:00
|
|
|
import argparse
|
2022-03-21 05:32:38 +01:00
|
|
|
from typing import Any
|
2012-12-06 23:26:47 +01:00
|
|
|
|
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
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2012-12-06 23:26:47 +01:00
|
|
|
|
2022-04-14 23:53:15 +02:00
|
|
|
from zerver.actions.create_user import do_create_user
|
2017-08-07 17:15:11 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2023-05-08 09:17:57 +02:00
|
|
|
from zerver.models import UserProfile
|
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):
|
2022-03-21 06:03:23 +01:00
|
|
|
help = """Create a new Zulip user via the command line.
|
2013-01-08 23:05:42 +01:00
|
|
|
|
2022-03-21 06:03:23 +01:00
|
|
|
Prompts the user for <email> and <full name> if not specified.
|
2015-08-21 02:10:41 +02:00
|
|
|
|
2022-03-21 06:03:23 +01:00
|
|
|
We recommend the Zulip API (https://zulip.com/api/create-user) instead
|
|
|
|
of this tool for most use cases.
|
|
|
|
|
|
|
|
If the server has Terms of Service configured, the user will be
|
|
|
|
prompted to accept the Terms of Service the first time they login.
|
2013-01-08 23:05:42 +01:00
|
|
|
"""
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
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
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
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:32:38 +01:00
|
|
|
create_user_params = self.get_create_user_params(options)
|
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(
|
2022-03-21 05:32:38 +01:00
|
|
|
create_user_params.email,
|
|
|
|
create_user_params.password,
|
2020-07-16 14:10:43 +02:00
|
|
|
realm,
|
2022-03-21 05:32:38 +01:00
|
|
|
create_user_params.full_name,
|
2023-05-08 09:17:57 +02:00
|
|
|
# Explicitly set tos_version=-1. This means that users
|
|
|
|
# created via this mechanism would be prompted to set
|
|
|
|
# the email_address_visibility setting on first login.
|
|
|
|
# For servers that have configured Terms of Service,
|
|
|
|
# users will also be prompted to accept the Terms of
|
|
|
|
# Service on first login.
|
|
|
|
tos_version=UserProfile.TOS_VERSION_BEFORE_FIRST_LOGIN,
|
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.")
|