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
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
2013-02-28 20:07:54 +01:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from confirmation.models import Confirmation
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.models import UserProfile, PreregistrationUser, \
|
2016-01-12 16:24:34 +01:00
|
|
|
get_user_profile_by_email, get_realm, email_allowed_for_realm
|
2013-02-28 20:07:54 +01:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Generate activation links for users and print them to stdout."
|
|
|
|
|
2015-08-21 02:10:41 +02:00
|
|
|
def add_arguments(self, parser):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (ArgumentParser) -> None
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('--domain',
|
|
|
|
dest='domain',
|
|
|
|
type=str,
|
|
|
|
help='The realm in which to generate the invites (use for open realms).')
|
|
|
|
parser.add_argument('--force',
|
|
|
|
dest='force',
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help='Override that the domain is restricted to external users.')
|
|
|
|
parser.add_argument('emails', metavar='<email>', type=str, nargs='*',
|
|
|
|
help='email of user to generate an activation link for')
|
2013-09-19 19:38:34 +02:00
|
|
|
|
2013-02-28 20:07:54 +01:00
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **Any) -> None
|
2013-02-28 20:07:54 +01:00
|
|
|
duplicates = False
|
2015-08-21 02:10:41 +02:00
|
|
|
for email in options['emails']:
|
2013-02-28 20:07:54 +01:00
|
|
|
try:
|
2013-03-28 20:20:31 +01:00
|
|
|
get_user_profile_by_email(email)
|
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
|
2013-03-28 20:20:31 +01:00
|
|
|
except UserProfile.DoesNotExist:
|
2013-02-28 20:07:54 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
if duplicates:
|
|
|
|
return
|
|
|
|
|
2013-09-19 19:38:34 +02:00
|
|
|
realm = None
|
|
|
|
domain = options["domain"]
|
|
|
|
if domain:
|
|
|
|
realm = get_realm(domain)
|
|
|
|
if not realm:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("The realm %s doesn't exist yet, please create it first." % (domain,))
|
|
|
|
print("Don't forget default streams!")
|
2013-09-19 19:38:34 +02:00
|
|
|
exit(1)
|
|
|
|
|
2015-08-21 02:10:41 +02:00
|
|
|
for email in options['emails']:
|
2013-09-19 19:38:34 +02:00
|
|
|
if realm:
|
2016-01-12 16:24:34 +01:00
|
|
|
if not email_allowed_for_realm(email, realm) and not options["force"]:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("You've asked to add an external user (%s) to a closed realm (%s)." % (
|
|
|
|
email, domain))
|
|
|
|
print("Are you sure? To do this, pass --force.")
|
2013-09-19 19:38:34 +02:00
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
prereg_user = PreregistrationUser(email=email, realm=realm)
|
|
|
|
else:
|
|
|
|
prereg_user = PreregistrationUser(email=email)
|
2013-05-30 20:13:41 +02:00
|
|
|
prereg_user.save()
|
2015-11-01 17:11:06 +01:00
|
|
|
print(email + ": " + Confirmation.objects.get_link_for_object(prereg_user))
|