2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
2019-02-02 23:53:29 +01:00
|
|
|
from typing import Any, List
|
2016-08-14 07:27:40 +02:00
|
|
|
|
2018-08-23 21:04:46 +02:00
|
|
|
from django.contrib.auth.tokens import default_token_generator
|
2016-08-14 07:27:40 +02:00
|
|
|
|
2018-08-23 21:04:46 +02:00
|
|
|
from zerver.forms import generate_password_reset_url
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.lib.management import CommandError, ZulipBaseCommand
|
|
|
|
from zerver.lib.send_email import FromAddress, send_email
|
|
|
|
from zerver.models import UserProfile
|
2017-06-06 05:53:04 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-07-10 21:42:29 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2016-08-14 07:27:40 +02:00
|
|
|
help = """Send email to specified email address."""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2020-09-02 20:45:48 +02:00
|
|
|
parser.add_argument('--entire-server', action="store_true",
|
2017-08-24 23:41:45 +02:00
|
|
|
help="Send to every user on the server. ")
|
2017-08-19 21:48:09 +02:00
|
|
|
self.add_user_list_args(parser,
|
2017-08-25 01:26:19 +02:00
|
|
|
help="Email addresses of user(s) to send password reset emails to.",
|
|
|
|
all_users_help="Send to every user on the realm.")
|
2017-07-10 21:42:29 +02:00
|
|
|
self.add_realm_args(parser)
|
2016-08-14 07:27:40 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: str) -> None:
|
2017-08-24 23:41:45 +02:00
|
|
|
if options["entire_server"]:
|
2016-08-14 07:27:40 +02:00
|
|
|
users = UserProfile.objects.filter(is_active=True, is_bot=False,
|
|
|
|
is_mirror_dummy=False)
|
2017-08-24 23:41:45 +02:00
|
|
|
else:
|
|
|
|
realm = self.get_realm(options)
|
2017-08-25 01:26:19 +02:00
|
|
|
try:
|
2019-08-14 19:48:54 +02:00
|
|
|
users = self.get_users(options, realm, is_bot=False)
|
2017-08-25 01:26:19 +02:00
|
|
|
except CommandError as error:
|
|
|
|
if str(error) == "You have to pass either -u/--users or -a/--all-users.":
|
|
|
|
raise CommandError("You have to pass -u/--users or -a/--all-users or --entire-server.")
|
|
|
|
raise error
|
2017-08-24 23:41:45 +02:00
|
|
|
|
2016-08-14 07:27:40 +02:00
|
|
|
self.send(users)
|
|
|
|
|
2018-08-23 11:02:47 +02:00
|
|
|
def send(self, users: List[UserProfile]) -> None:
|
2016-08-14 07:27:40 +02:00
|
|
|
"""Sends one-use only links for resetting password to target users
|
|
|
|
|
|
|
|
"""
|
|
|
|
for user_profile in users:
|
|
|
|
context = {
|
2019-11-16 01:42:36 +01:00
|
|
|
'email': user_profile.delivery_email,
|
2018-08-23 21:04:46 +02:00
|
|
|
'reset_url': generate_password_reset_url(user_profile, default_token_generator),
|
2018-08-23 11:02:47 +02:00
|
|
|
'realm_uri': user_profile.realm.uri,
|
2018-12-20 09:44:18 +01:00
|
|
|
'realm_name': user_profile.realm.name,
|
2018-08-23 11:02:47 +02:00
|
|
|
'active_account_in_realm': True,
|
2016-08-14 07:27:40 +02:00
|
|
|
}
|
2018-12-03 23:26:51 +01:00
|
|
|
send_email('zerver/emails/password_reset', to_user_ids=[user_profile.id],
|
2018-08-23 11:02:47 +02:00
|
|
|
from_address=FromAddress.tokenized_no_reply_address(),
|
2020-02-14 13:58:58 +01:00
|
|
|
from_name=FromAddress.security_email_from_name(user_profile=user_profile),
|
|
|
|
context=context)
|