|
32 | 32 |
|
33 | 33 | # Copyright The IETF Trust 2007, All Rights Reserved |
34 | 34 |
|
| 35 | +import importlib |
| 36 | + |
35 | 37 | from datetime import datetime as DateTime, timedelta as TimeDelta, date as Date |
36 | 38 | from collections import defaultdict |
37 | 39 |
|
| 40 | +import django.core.signing |
| 41 | +from django import forms |
| 42 | +from django.contrib import messages |
38 | 43 | from django.conf import settings |
39 | | -from django.http import Http404 #, HttpResponse, HttpResponseRedirect |
40 | | -from django.shortcuts import render, redirect, get_object_or_404 |
41 | | -#from django.contrib.auth import REDIRECT_FIELD_NAME, authenticate, login |
| 44 | +from django.contrib.auth import update_session_auth_hash |
42 | 45 | from django.contrib.auth.decorators import login_required |
43 | | -#from django.utils.http import urlquote |
44 | | -import django.core.signing |
45 | | -from django.contrib.sites.models import Site |
46 | 46 | from django.contrib.auth.models import User |
47 | | -from django import forms |
| 47 | +from django.contrib.sites.models import Site |
| 48 | +from django.core.urlresolvers import reverse as urlreverse |
| 49 | +from django.http import Http404, HttpResponseRedirect #, HttpResponse, |
| 50 | +from django.shortcuts import render, redirect, get_object_or_404 |
48 | 51 |
|
49 | 52 | import debug # pyflakes:ignore |
50 | 53 |
|
51 | 54 | from ietf.group.models import Role, Group |
52 | | -from ietf.ietfauth.forms import RegistrationForm, PasswordForm, ResetPasswordForm, TestEmailForm, WhitelistForm |
| 55 | +from ietf.ietfauth.forms import RegistrationForm, PasswordForm, ResetPasswordForm, TestEmailForm, WhitelistForm, ChangePasswordForm |
53 | 56 | from ietf.ietfauth.forms import get_person_form, RoleEmailForm, NewEmailForm |
54 | 57 | from ietf.ietfauth.htpasswd import update_htpasswd_file |
55 | 58 | from ietf.ietfauth.utils import role_required |
@@ -340,10 +343,14 @@ def confirm_password_reset(request, auth): |
340 | 343 | else: |
341 | 344 | form = PasswordForm() |
342 | 345 |
|
| 346 | + hlibname, hashername = settings.PASSWORD_HASHERS[0].rsplit('.',1) |
| 347 | + hlib = importlib.import_module(hlibname) |
| 348 | + hasher = getattr(hlib, hashername) |
343 | 349 | return render(request, 'registration/change_password.html', { |
344 | 350 | 'form': form, |
345 | | - 'username': username, |
| 351 | + 'user': user, |
346 | 352 | 'success': success, |
| 353 | + 'hasher': hasher, |
347 | 354 | }) |
348 | 355 |
|
349 | 356 | def test_email(request): |
@@ -465,3 +472,48 @@ def review_overview(request): |
465 | 472 | 'review_wishes': review_wishes, |
466 | 473 | 'review_wish_form': review_wish_form, |
467 | 474 | }) |
| 475 | + |
| 476 | +@login_required |
| 477 | +def change_password(request): |
| 478 | + success = False |
| 479 | + person = None |
| 480 | + |
| 481 | + try: |
| 482 | + person = request.user.person |
| 483 | + except Person.DoesNotExist: |
| 484 | + return render(request, 'registration/missing_person.html') |
| 485 | + |
| 486 | + emails = [ e.address for e in Email.objects.filter(person=person, active=True).order_by('-primary','-time') ] |
| 487 | + user = request.user |
| 488 | + |
| 489 | + if request.method == 'POST': |
| 490 | + form = ChangePasswordForm(user, request.POST) |
| 491 | + if form.is_valid(): |
| 492 | + new_password = form.cleaned_data["new_password"] |
| 493 | + |
| 494 | + user.set_password(new_password) |
| 495 | + user.save() |
| 496 | + # password is also stored in htpasswd file |
| 497 | + update_htpasswd_file(user.username, new_password) |
| 498 | + # keep the session |
| 499 | + update_session_auth_hash(request, user) |
| 500 | + |
| 501 | + send_mail(request, emails, None, "Datatracker password change notification", "registration/password_change_email.txt", {}) |
| 502 | + |
| 503 | + messages.success(request, "Your password was successfully changed") |
| 504 | + return HttpResponseRedirect(urlreverse('ietf.ietfauth.views.profile')) |
| 505 | + |
| 506 | + else: |
| 507 | + form = ChangePasswordForm(request.user) |
| 508 | + |
| 509 | + hlibname, hashername = settings.PASSWORD_HASHERS[0].rsplit('.',1) |
| 510 | + hlib = importlib.import_module(hlibname) |
| 511 | + hasher = getattr(hlib, hashername) |
| 512 | + return render(request, 'registration/change_password.html', { |
| 513 | + 'form': form, |
| 514 | + 'user': user, |
| 515 | + 'success': success, |
| 516 | + 'hasher': hasher, |
| 517 | + }) |
| 518 | + |
| 519 | + |
0 commit comments