mirror of
https://github.com/zulip/zulip.git
synced 2026-07-15 21:03:26 +08:00
Add a management command to deactivate a user.
(imported from commit 14ef58bdd2cd20c05c68cd53cf911711d3bdb5fd)
This commit is contained in:
parent
0c62dcc9f6
commit
ac305ffc1d
@ -1,4 +1,5 @@
|
||||
from django.conf import settings
|
||||
from django.contrib.sessions.models import Session
|
||||
from zephyr.lib.context_managers import lockfile
|
||||
from zephyr.models import Realm, Stream, UserProfile, UserActivity, \
|
||||
Subscription, Recipient, Message, UserMessage, \
|
||||
@ -51,6 +52,22 @@ def do_create_user(email, password, realm, full_name, short_name,
|
||||
'domain': realm.domain})
|
||||
return create_user(email, password, realm, full_name, short_name, active)
|
||||
|
||||
def user_sessions(user):
|
||||
return [s for s in Session.objects.all() if s.get_decoded().get('_auth_user_id') == user.id]
|
||||
|
||||
def do_deactivate(user_profile):
|
||||
user_profile.user.set_unusable_password()
|
||||
user_profile.user.is_active = False
|
||||
user_profile.user.save()
|
||||
|
||||
for session in user_sessions(user_profile.user):
|
||||
session.delete()
|
||||
|
||||
log_event({'type': 'user_deactivated',
|
||||
'timestamp': time.time(),
|
||||
'user': user_profile.user.email,
|
||||
'domain': user_profile.realm.domain})
|
||||
|
||||
def do_change_user_email(user, new_email):
|
||||
old_email = user.email
|
||||
user.email = new_email
|
||||
|
||||
40
zephyr/management/commands/deactivate_user.py
Normal file
40
zephyr/management/commands/deactivate_user.py
Normal file
@ -0,0 +1,40 @@
|
||||
from optparse import make_option
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from zephyr.lib.actions import do_deactivate, user_sessions
|
||||
from zephyr.models import UserProfile
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Deactivate a user, including forcibly logging them out."
|
||||
|
||||
option_list = BaseCommand.option_list + (
|
||||
make_option('-f', '--for-real',
|
||||
dest='for_real',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help="Actually deactivate the user. Default is a dry run."),
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
if not args:
|
||||
print "Please specify an e-mail address."
|
||||
exit(1)
|
||||
user = User.objects.get(email__iexact=args[0])
|
||||
user_profile = UserProfile.objects.get(user=user)
|
||||
|
||||
sessions = user_sessions(user)
|
||||
print "Deactivating %s (%s) - %s" % (user_profile.full_name, user.email,
|
||||
user_profile.realm.domain)
|
||||
print "%s has the following active sessions:" % (user.email,)
|
||||
for session in sessions:
|
||||
print session.expire_date, session.get_decoded()
|
||||
print ""
|
||||
|
||||
if not options["for_real"]:
|
||||
print "This was a dry run. Pass -f to actually deactivate."
|
||||
exit(1)
|
||||
|
||||
do_deactivate(user_profile)
|
||||
print "Sessions deleted, user deactivated."
|
||||
Loading…
Reference in New Issue
Block a user