2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2012-12-06 23:26:47 +01:00
|
|
|
import sys
|
2015-08-21 02:10:41 +02:00
|
|
|
import argparse
|
2012-12-06 23:26:47 +01:00
|
|
|
|
2017-08-07 17:15:11 +02:00
|
|
|
from django.core.management.base import CommandError
|
2012-12-06 23:26:47 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.db.utils import IntegrityError
|
|
|
|
from django.core import validators
|
|
|
|
|
2017-08-07 17:15:11 +02:00
|
|
|
from zerver.models import email_to_username
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.actions import do_create_user
|
2015-11-09 01:58:18 +01:00
|
|
|
from zerver.lib.actions import notify_new_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
|
2015-11-01 17:14:42 +01:00
|
|
|
from six.moves import input
|
2012-12-06 23:26:47 +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.
|
|
|
|
|
|
|
|
A user MUST have ALREADY accepted the Terms of Service before creating their
|
|
|
|
account this way.
|
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
|
|
|
"""
|
|
|
|
|
2015-08-21 02:10:41 +02:00
|
|
|
def add_arguments(self, parser):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (argparse.ArgumentParser) -> None
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('--this-user-has-accepted-the-tos',
|
|
|
|
dest='tos',
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help='Acknowledgement that the user has already accepted the ToS.')
|
2017-07-08 07:25:17 +02:00
|
|
|
parser.add_argument('--password',
|
|
|
|
dest='password',
|
|
|
|
type=str,
|
|
|
|
default='',
|
2017-07-18 05:50:54 +02:00
|
|
|
help='password of new user. For development only.'
|
|
|
|
'Note that we recommend against setting '
|
2017-07-08 23:12:34 +02:00
|
|
|
'passwords this way, since they can be snooped by any user account '
|
2017-07-18 05:50:54 +02:00
|
|
|
'on the server via `ps -ef` or by any superuser with'
|
|
|
|
'read access to the user\'s bash history.')
|
|
|
|
parser.add_argument('--password-file',
|
|
|
|
dest='password_file',
|
|
|
|
type=str,
|
|
|
|
default='',
|
|
|
|
help='The file containing the password of the new user.')
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('email', metavar='<email>', type=str, nargs='?', default=argparse.SUPPRESS,
|
|
|
|
help='email address of new user')
|
|
|
|
parser.add_argument('full_name', metavar='<full name>', type=str, nargs='?', default=argparse.SUPPRESS,
|
|
|
|
help='full name of new user')
|
2017-08-07 17:15:11 +02:00
|
|
|
self.add_realm_args(parser, True, "The name of the existing realm to which to add the user.")
|
2012-12-06 23:26:47 +01:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **Any) -> None
|
2013-01-08 23:05:42 +01:00
|
|
|
if not options["tos"]:
|
|
|
|
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)
|
2012-12-06 23:26:47 +01:00
|
|
|
try:
|
2015-08-21 02:10:41 +02: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:
|
|
|
|
if 'email' in options or 'full_name' in options:
|
2013-01-08 23:08:13 +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:
|
2017-07-18 05:50:54 +02:00
|
|
|
if 'password' in options:
|
|
|
|
pw = options['password']
|
|
|
|
if 'password_file' in options:
|
|
|
|
pw = open(options['password_file'], 'r').read()
|
|
|
|
else:
|
|
|
|
pw = initial_password(email).encode()
|
2017-07-08 07:25:17 +02:00
|
|
|
notify_new_user(do_create_user(email, pw,
|
2016-12-03 00:04:17 +01:00
|
|
|
realm, full_name, email_to_username(email)),
|
|
|
|
internal=True)
|
2012-12-06 23:26:47 +01:00
|
|
|
except IntegrityError:
|
|
|
|
raise CommandError("User already exists.")
|