mirror of
https://github.com/django/django.git
synced 2025-08-21 19:14:36 +00:00
Fixed #6273 -- Added a 'changepassword' management command. Thanks to Ludvig Ericson and Justin Lilly for their work on this patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12351 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
72c39410c8
commit
47acb1d659
2 changed files with 62 additions and 3 deletions
48
django/contrib/auth/management/commands/changepassword.py
Normal file
48
django/contrib/auth/management/commands/changepassword.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.contrib.auth.models import User
|
||||
import getpass
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Change a user's password for django.contrib.auth."
|
||||
|
||||
requires_model_validation = False
|
||||
|
||||
def _get_pass(self, prompt="Password: "):
|
||||
p = getpass.getpass(prompt=prompt)
|
||||
if not p:
|
||||
raise CommandError("aborted")
|
||||
return p
|
||||
|
||||
def handle(self, *args, **options):
|
||||
if len(args) > 1:
|
||||
raise CommandError("need exactly one or zero arguments for username")
|
||||
|
||||
if args:
|
||||
username, = args
|
||||
else:
|
||||
username = getpass.getuser()
|
||||
|
||||
try:
|
||||
u = User.objects.get(username=username)
|
||||
except User.DoesNotExist:
|
||||
raise CommandError("user '%s' does not exist" % username)
|
||||
|
||||
print "Changing password for user '%s'" % u.username
|
||||
|
||||
MAX_TRIES = 3
|
||||
count = 0
|
||||
p1, p2 = 1, 2 # To make them initially mismatch.
|
||||
while p1 != p2 and count < MAX_TRIES:
|
||||
p1 = self._get_pass()
|
||||
p2 = self._get_pass("Password (again): ")
|
||||
if p1 != p2:
|
||||
print "Passwords do not match. Please try again."
|
||||
count = count + 1
|
||||
|
||||
if count == MAX_TRIES:
|
||||
raise CommandError("Aborting password change for user '%s' after %s attempts" % (username, count))
|
||||
|
||||
u.set_password(p1)
|
||||
u.save()
|
||||
|
||||
return "Password changed successfully for user '%s'" % u.username
|
Loading…
Add table
Add a link
Reference in a new issue