2016-08-14 07:27:40 +02:00
|
|
|
|
|
|
|
import logging
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Dict, List, Optional, Text
|
2016-08-14 07:27:40 +02:00
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
2017-07-10 21:42:29 +02:00
|
|
|
from zerver.models import UserProfile
|
2016-08-14 07:27:40 +02:00
|
|
|
from django.utils.http import urlsafe_base64_encode
|
|
|
|
from django.utils.encoding import force_bytes
|
|
|
|
|
2016-09-10 21:32:43 +02:00
|
|
|
from django.contrib.auth.tokens import default_token_generator, PasswordResetTokenGenerator
|
2016-08-14 07:27:40 +02:00
|
|
|
|
2017-07-10 00:11:06 +02:00
|
|
|
from zerver.lib.send_email import send_email, FromAddress
|
2017-08-25 01:26:19 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand, CommandError
|
2017-06-06 05:53:04 +02: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:
|
2017-08-24 23:41:45 +02:00
|
|
|
parser.add_argument('--entire-server', action="store_true", default=False,
|
|
|
|
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:
|
|
|
|
users = self.get_users(options, realm)
|
|
|
|
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)
|
|
|
|
|
2017-06-06 05:53:04 +02:00
|
|
|
def send(self, users, subject_template_name='', email_template_name='',
|
2016-08-14 07:27:40 +02:00
|
|
|
use_https=True, token_generator=default_token_generator,
|
2016-09-10 21:32:43 +02:00
|
|
|
from_email=None, html_email_template_name=None):
|
|
|
|
# type: (List[UserProfile], str, str, bool, PasswordResetTokenGenerator, Optional[Text], Optional[str]) -> 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 = {
|
|
|
|
'email': user_profile.email,
|
|
|
|
'domain': user_profile.realm.host,
|
|
|
|
'site_name': "zulipo",
|
2017-10-06 13:15:59 +02:00
|
|
|
'uid': urlsafe_base64_encode(force_bytes(user_profile.id)),
|
2016-08-14 07:27:40 +02:00
|
|
|
'user': user_profile,
|
|
|
|
'token': token_generator.make_token(user_profile),
|
|
|
|
'protocol': 'https' if use_https else 'http',
|
|
|
|
}
|
|
|
|
|
|
|
|
logging.warning("Sending %s email to %s" % (email_template_name, user_profile.email,))
|
2017-07-11 05:11:08 +02:00
|
|
|
send_email('zerver/emails/password_reset', to_user_id=user_profile.id,
|
2017-07-11 05:01:32 +02:00
|
|
|
from_name="Zulip Account Security", from_address=FromAddress.NOREPLY,
|
|
|
|
context=context)
|