2013-06-24 21:42:46 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from optparse import make_option
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
2014-01-22 19:27:00 +01:00
|
|
|
from zerver.lib.actions import do_change_is_admin
|
2013-06-24 21:42:46 +02:00
|
|
|
|
2013-11-04 17:22:58 +01:00
|
|
|
from zerver.models import UserProfile
|
2013-06-24 21:42:46 +02:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Give an existing user administrative permissions over their (own) Realm.
|
|
|
|
|
|
|
|
ONLY perform this on customer request from an authorized person.
|
|
|
|
"""
|
|
|
|
|
|
|
|
option_list = BaseCommand.option_list + (
|
2013-06-27 23:42:41 +02:00
|
|
|
make_option('-f', '--for-real',
|
|
|
|
dest='ack',
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help='Acknowledgement that this is done according to policy.'),
|
2013-06-24 21:57:40 +02:00
|
|
|
make_option('--revoke',
|
|
|
|
dest='grant',
|
|
|
|
action="store_false",
|
|
|
|
default=True,
|
|
|
|
help='Remove an administrator\'s rights.'),
|
|
|
|
)
|
2013-06-24 21:42:46 +02:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
try:
|
|
|
|
email = args[0]
|
|
|
|
except ValueError:
|
|
|
|
raise CommandError("""Please specify a user.""")
|
|
|
|
try:
|
|
|
|
profile = UserProfile.objects.get(email=email)
|
|
|
|
except ValidationError:
|
|
|
|
raise CommandError("No such user.")
|
2013-06-24 21:57:40 +02:00
|
|
|
|
|
|
|
if options['grant']:
|
|
|
|
if profile.has_perm('administer', profile.realm):
|
|
|
|
raise CommandError("User already has permission for this realm.")
|
|
|
|
else:
|
2013-06-27 23:42:41 +02:00
|
|
|
if options['ack']:
|
2014-01-22 19:27:00 +01:00
|
|
|
do_change_is_admin(profile, True)
|
2013-06-27 23:42:41 +02:00
|
|
|
print "Done!"
|
|
|
|
else:
|
|
|
|
print "Would have made %s an administrator for %s" % (email, profile.realm.domain)
|
2013-06-24 21:42:46 +02:00
|
|
|
else:
|
2013-06-24 21:57:40 +02:00
|
|
|
if profile.has_perm('administer', profile.realm):
|
2013-06-27 23:42:41 +02:00
|
|
|
if options['ack']:
|
2014-01-22 19:27:00 +01:00
|
|
|
do_change_is_admin(profile, False)
|
2013-06-27 23:42:41 +02:00
|
|
|
print "Done!"
|
|
|
|
else:
|
|
|
|
print "Would have removed %s's administrator rights on %s" % (email,
|
|
|
|
profile.realm.domain)
|
2013-06-24 21:57:40 +02:00
|
|
|
else:
|
|
|
|
raise CommandError("User did not have permission for this realm!")
|