2013-05-29 23:58:07 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-05-29 23:58:07 +02:00
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.models import UserProfile, get_user_profile_by_email
|
|
|
|
from zerver.lib.rate_limiter import block_user, unblock_user
|
2013-05-29 23:58:07 +02:00
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from optparse import make_option
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2015-08-21 02:10:41 +02:00
|
|
|
help = """Manually block or unblock a user from accessing the API"""
|
|
|
|
|
|
|
|
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('-e', '--email',
|
|
|
|
dest='email',
|
|
|
|
help="Email account of user.")
|
|
|
|
parser.add_argument('-a', '--api-key',
|
|
|
|
dest='api_key',
|
|
|
|
help="API key of user.")
|
|
|
|
parser.add_argument('-s', '--seconds',
|
|
|
|
dest='seconds',
|
|
|
|
default=60,
|
|
|
|
type=int,
|
|
|
|
help="Seconds to block for.")
|
|
|
|
parser.add_argument('-d', '--domain',
|
|
|
|
dest='domain',
|
|
|
|
default='all',
|
|
|
|
help="Rate-limiting domain. Defaults to 'all'.")
|
|
|
|
parser.add_argument('-b', '--all-bots',
|
|
|
|
dest='bots',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help="Whether or not to also block all bots for this user.")
|
|
|
|
parser.add_argument('operation', metavar='<operation>', type=str, choices=['block', 'unblock'],
|
|
|
|
help="operation to perform (block or unblock)")
|
2013-05-29 23:58:07 +02:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **Any) -> None
|
2013-05-29 23:58:07 +02:00
|
|
|
if (not options['api_key'] and not options['email']) or \
|
|
|
|
(options['api_key'] and options['email']):
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Please enter either an email or API key to manage")
|
2013-05-29 23:58:07 +02:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
if options['email']:
|
|
|
|
user_profile = get_user_profile_by_email(options['email'])
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
user_profile = UserProfile.objects.get(api_key=options['api_key'])
|
2017-03-05 10:25:27 +01:00
|
|
|
except Exception:
|
2016-05-04 23:16:27 +02:00
|
|
|
print("Unable to get user profile for api key %s" % (options['api_key'],))
|
2013-05-29 23:58:07 +02:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
users = [user_profile]
|
|
|
|
if options['bots']:
|
|
|
|
users.extend(bot for bot in UserProfile.objects.filter(is_bot=True,
|
|
|
|
bot_owner=user_profile))
|
|
|
|
|
2015-08-21 02:10:41 +02:00
|
|
|
operation = options['operation']
|
2013-05-29 23:58:07 +02:00
|
|
|
for user in users:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Applying operation to User ID: %s: %s" % (user.id, operation))
|
2013-05-29 23:58:07 +02:00
|
|
|
|
|
|
|
if operation == 'block':
|
|
|
|
block_user(user, options['seconds'], options['domain'])
|
|
|
|
elif operation == 'unblock':
|
|
|
|
unblock_user(user, options['domain'])
|