Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset user password from the admin interface

In my website, I want to let the admins reset the password of any user.

With reset I mean exactly what the password_reset view does (under contrib.auth): Send a confirmation link to that user email.

How would be the best way of doing that? Is there an already app/snippet that does that?

Edit:

Let's suppose user john is an admin. What I want is to let john reset any user's password through the admin interface. For example, to reset max password, he will just go to the max user, and click on any link to reset his password.

like image 231
Oscar Mederos Avatar asked Apr 24 '12 05:04

Oscar Mederos


People also ask

Can admin reset user password?

As an administrator, you can reset users' passwords to maintain account security. To do so, you must be signed in with an administrator account that has reset password privileges. Sign in to your Google Admin console.

How do I reset a user account password?

On the Users tab, under Users for this computer, select the user account name, and then select Reset Password. Type the new password, confirm the new password, and then select OK.

How do I reset my admin console password?

Reset a user's password in the panel Navigate to the Users page in your Google admin console. Click your user from the list. In the left menu, select 'RESET PASSWORD'. Enter your new password in the prompt.


1 Answers

What I finally did was to add a custom ModelAdmin:

from django.contrib.auth.forms import PasswordResetForm
from django.contrib.auth.admin import UserAdmin


class CustomUserAdmin(UserAdmin):
    ...
    def reset_password(self, request, user_id):
        if not self.has_change_permission(request):
            raise PermissionDenied
        user = get_object_or_404(self.model, pk=user_id)

        form = PasswordResetForm(data={'email': user.email})
        form.is_valid()

        form.save(email_template_name='my_template.html')
        return HttpResponseRedirect('..')

    def get_urls(self):
        urls = super(UserAdmin, self).get_urls()

        my_urls = patterns('',
            (r'^(\d+)/reset-password/$',
                     self.admin_site.admin_view(self.reset_password)
            ),
        )
        return my_urls + urls

and I also had to override the change_form.html template, like this:

{% extends "admin/change_form.html" %}
{% load i18n %}
{% block object-tools %}
    {% if change %}{% if not is_popup %}
        <ul class="object-tools">
            {# You can also give a name to that pattern and refer to it below using 'url' #}
            <li><a href="reset-password/" class="historylink">Reset password</a></li>

            <li><a href="history/" class="historylink">{% trans "History" %}</a></li>
            {% if has_absolute_url %}
                <li><a href="../../../r/{{ content_type_id }}/{{ object_id }}/" class="viewsitelink">
                    {% trans "View on site" %}</a>
                </li>
            {% endif%}
        </ul>
    {% endif %}{% endif %}
{% endblock %}

The result looks like this:

Reset Password from admin in Django

If you want a more detailed explanation, I blogged about it.

like image 192
Oscar Mederos Avatar answered Sep 19 '22 05:09

Oscar Mederos