Ticket #3166: change_password.patch

File change_password.patch, 8.0 KB (added by SmileyChris, 8 years ago)
  • django/contrib/admin/templates/admin/auth/user/change_password.html

     
     1{% extends "admin/base_site.html" %}
     2{% load i18n admin_modify adminmedia %}
     3{% block extrahead %}{{ block.super }}
     4<script type="text/javascript" src="../../../../jsi18n/"></script>
     5{% for js in javascript_imports %}{% include_admin_script js %}{% endfor %}
     6{% endblock %}
     7{% block stylesheet %}{% admin_media_prefix %}css/forms.css{% endblock %}
     8{% block bodyclass %}{{ opts.app_label }}-{{ opts.object_name.lower }} change-form{% endblock %}
     9{% block userlinks %}<a href="../../../../doc/">{% trans 'Documentation' %}</a> / <a href="../../../password_change/">{% trans 'Change password' %}</a> / <a href="../../../logout/">{% trans 'Log out' %}</a>{% endblock %}
     10{% block breadcrumbs %}{% if not is_popup %}
     11<div class="breadcrumbs">
     12     <a href="../../../../">{% trans "Home" %}</a> &rsaquo;
     13     <a href="../../">{{ opts.verbose_name_plural|capfirst|escape }}</a> &rsaquo;
     14     <a href="../">{{ original|truncatewords:"18"|escape }}</a> &rsaquo;
     15     {% trans 'change password' %}
     16</div>
     17{% endif %}{% endblock %}
     18{% block content %}<div id="content-main">
     19<form action="{{ form_url }}" method="post" id="{{ opts.module_name }}_form">{% block form_top %}{% endblock %}
     20<div>
     21{% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}
     22{% if form.error_dict %}
     23    <p class="errornote">
     24    {% blocktrans count form.error_dict.items|length as counter %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}
     25    </p>
     26{% endif %}
     27
     28<p>{% trans "Enter a new username and password." %}</p>
     29
     30<fieldset class="module aligned">
     31
     32<div class="form-row">
     33  {{ form.password1.html_error_list }}
     34  <label for="id_password1" class="required">{% trans 'Password' %}:</label> {{ form.password1 }}
     35</div>
     36
     37<div class="form-row">
     38  {{ form.password2.html_error_list }}
     39  <label for="id_password2" class="required">{% trans 'Password (again)' %}:</label> {{ form.password2 }}
     40  <p class="help">{% trans 'Enter the same password as above, for verification.' %}</p>
     41</div>
     42
     43</fieldset>
     44
     45<div class="submit-row">
     46<input type="submit" value="{% trans 'Change password' %}" class="default" />
     47</div>
     48
     49<script type="text/javascript">document.getElementById("{{ first_form_field_id }}").focus();</script>
     50</div>
     51</form></div>
     52{% endblock %}
  • django/contrib/admin/urls.py

     
    2929
    3030    # "Add user" -- a special-case view
    3131    ('^auth/user/add/$', 'django.contrib.admin.views.auth.user_add_stage'),
     32    # "Change user password" -- another special-case view
     33    ('^auth/user/(\d+)/password/$', 'django.contrib.admin.views.auth.user_change_password'),
    3234
    3335    # Add/change/delete/history
    3436    ('^([^/]+)/([^/]+)/$', 'django.contrib.admin.views.main.change_list'),
  • django/contrib/admin/views/auth.py

     
    11from django.contrib.admin.views.decorators import staff_member_required
    2 from django.contrib.auth.forms import UserCreationForm
     2from django.contrib.auth.forms import UserCreationForm, AdminPasswordChangeForm
    33from django.contrib.auth.models import User
    44from django.core.exceptions import PermissionDenied
    55from django import oldforms, template
    6 from django.shortcuts import render_to_response
     6from django.shortcuts import render_to_response, get_object_or_404
    77from django.http import HttpResponseRedirect
    88
    99def user_add_stage(request):
     
    4242        'username_help_text': User._meta.get_field('username').help_text,
    4343    }, context_instance=template.RequestContext(request))
    4444user_add_stage = staff_member_required(user_add_stage)
     45
     46def user_change_password(request, id):
     47    if not request.user.has_perm('auth.change_user'):
     48        raise PermissionDenied
     49    user = get_object_or_404(User, pk=id)
     50    manipulator = AdminPasswordChangeForm(user)
     51    if request.method == 'POST':
     52        new_data = request.POST.copy()
     53        errors = manipulator.get_validation_errors(new_data)
     54        if not errors:
     55            new_user = manipulator.save(new_data)
     56            msg = _('Password changed successfully.') % user
     57            request.user.message_set.create(message=msg)
     58            return HttpResponseRedirect('..')
     59    else:
     60        errors = new_data = {}
     61    form = oldforms.FormWrapper(manipulator, new_data, errors)
     62    return render_to_response('admin/auth/user/change_password.html', {
     63        'title': _("Change password"),
     64        'form': form,
     65        'is_popup': request.REQUEST.has_key('_popup'),
     66        'add': True,
     67        'change': False,
     68        'has_delete_permission': False,
     69        'has_change_permission': True,
     70        'has_absolute_url': False,
     71        'first_form_field_id': 'id_password1',
     72        'opts': User._meta,
     73        'original': user,
     74        'show_save': True,
     75    }, context_instance=template.RequestContext(request))
     76user_change_password = staff_member_required(user_change_password)
  • django/contrib/auth/forms.py

     
    126126        "Saves the new password."
    127127        self.user.set_password(new_data['new_password1'])
    128128        self.user.save()
     129
     130class AdminPasswordChangeForm(oldforms.Manipulator):
     131    "A form used to change the password of a user in the admin interface."
     132    def __init__(self, user):
     133        self.user = user
     134        self.fields = (
     135            oldforms.PasswordField(field_name='password1', length=30, maxlength=60, is_required=True),
     136            oldforms.PasswordField(field_name='password2', length=30, maxlength=60, is_required=True,
     137                validator_list=[validators.AlwaysMatchesOtherField('password1', _("The two password fields didn't match."))]),
     138        )
     139
     140    def isValidUsername(self, field_data, all_data):
     141        try:
     142            User.objects.get(username=field_data)
     143        except User.DoesNotExist:
     144            return
     145        raise validators.ValidationError, _('A user with that username already exists.')
     146
     147    def save(self, new_data):
     148        "Saves the new password."
     149        self.user.set_password(new_data['password1'])
     150        self.user.save()
  • django/contrib/auth/models.py

     
    9191    first_name = models.CharField(_('first name'), maxlength=30, blank=True)
    9292    last_name = models.CharField(_('last name'), maxlength=30, blank=True)
    9393    email = models.EmailField(_('e-mail address'), blank=True)
    94     password = models.CharField(_('password'), maxlength=128, help_text=_("Use '[algo]$[salt]$[hexdigest]'"))
     94    password = models.CharField(_('password'), maxlength=128, help_text=_("Use '[algo]$[salt]$[hexdigest]' or use the <a href=\"password/\">change password form</a>."))
    9595    is_staff = models.BooleanField(_('staff status'), default=False, help_text=_("Designates whether the user can log into this admin site."))
    9696    is_active = models.BooleanField(_('active'), default=True, help_text=_("Designates whether this user can log into the Django admin. Unselect this instead of deleting accounts."))
    9797    is_superuser = models.BooleanField(_('superuser status'), default=False, help_text=_("Designates that this user has all permissions without explicitly assigning them."))
Back to Top