Files
zulip/zerver/management/commands/knight.py
Tim Abbott e111a2f9a5 [manual] Rename Django app from zephyr to zerver.
This needs to be deployed to both staging and prod at the same
off-peak time (and the schema migration run).

At the time it is deployed, we need to make a few changes directly in
the database:

(1) UPDATE django_content_type set app_label='zerver' where app_label='zephyr';
(2) UPDATE south_migrationhistory set app_name='zerver' where app_name='zephyr';

(imported from commit eb3fd719571740189514ef0b884738cb30df1320)
2013-08-06 07:39:36 -04:00

63 lines
2.3 KiB
Python

from __future__ import absolute_import
import sys
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError
from django.core import validators
from guardian.shortcuts import assign_perm, remove_perm
from zerver.models import Realm, UserProfile
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 + (
make_option('-f', '--for-real',
dest='ack',
action="store_true",
default=False,
help='Acknowledgement that this is done according to policy.'),
make_option('--revoke',
dest='grant',
action="store_false",
default=True,
help='Remove an administrator\'s rights.'),
)
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.")
if options['grant']:
if profile.has_perm('administer', profile.realm):
raise CommandError("User already has permission for this realm.")
else:
if options['ack']:
assign_perm('administer', profile, profile.realm)
print "Done!"
else:
print "Would have made %s an administrator for %s" % (email, profile.realm.domain)
else:
if profile.has_perm('administer', profile.realm):
if options['ack']:
remove_perm('administer', profile, profile.realm)
print "Done!"
else:
print "Would have removed %s's administrator rights on %s" % (email,
profile.realm.domain)
else:
raise CommandError("User did not have permission for this realm!")