Django

Code

Changeset 4266

Show
Ignore:
Timestamp:
12/30/06 01:16:25 (2 years ago)
Author:
adrian
Message:

Fixed #3166 -- Added admin 'Change user password' view. Thanks for the patch, SmileyChris?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/admin/urls.py

    r3564 r4266  
    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 
  • django/trunk/django/contrib/admin/views/auth.py

    r4265 r4266  
    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 
     8from django.utils.html import escape 
    89 
    910def user_add_stage(request): 
     
    4344    }, context_instance=template.RequestContext(request)) 
    4445user_add_stage = staff_member_required(user_add_stage) 
     46 
     47def user_change_password(request, id): 
     48    if not request.user.has_perm('auth.change_user'): 
     49        raise PermissionDenied 
     50    user = get_object_or_404(User, pk=id) 
     51    manipulator = AdminPasswordChangeForm(user) 
     52    if request.method == 'POST': 
     53        new_data = request.POST.copy() 
     54        errors = manipulator.get_validation_errors(new_data) 
     55        if not errors: 
     56            new_user = manipulator.save(new_data) 
     57            msg = _('Password changed successfully.') 
     58            request.user.message_set.create(message=msg) 
     59            return HttpResponseRedirect('..') 
     60    else: 
     61        errors = new_data = {} 
     62    form = oldforms.FormWrapper(manipulator, new_data, errors) 
     63    return render_to_response('admin/auth/user/change_password.html', { 
     64        'title': _('Change password: %s') % escape(user.username), 
     65        'form': form, 
     66        'is_popup': request.REQUEST.has_key('_popup'), 
     67        'add': True, 
     68        'change': False, 
     69        'has_delete_permission': False, 
     70        'has_change_permission': True, 
     71        'has_absolute_url': False, 
     72        'first_form_field_id': 'id_password1', 
     73        'opts': User._meta, 
     74        'original': user, 
     75        'show_save': True, 
     76    }, context_instance=template.RequestContext(request)) 
     77user_change_password = staff_member_required(user_change_password) 
  • django/trunk/django/contrib/auth/forms.py

    r4208 r4266  
    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 save(self, new_data): 
     141        "Saves the new password." 
     142        self.user.set_password(new_data['password1']) 
     143        self.user.save() 
  • django/trunk/django/contrib/auth/models.py

    r4265 r4266  
    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."))