Merge pull request #1554 from normanjaeckel/UserResetPassword

Added view to reset user's password.
This commit is contained in:
Oskar Hahn 2015-06-18 23:16:45 +02:00
commit a5d9f0bb42
2 changed files with 29 additions and 1 deletions

View File

@ -1,10 +1,11 @@
from django.contrib.auth import login as auth_login from django.contrib.auth import login as auth_login
from django.contrib.auth import logout as auth_logout from django.contrib.auth import logout as auth_logout
from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.forms import AuthenticationForm
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy from django.utils.translation import ugettext_lazy
from rest_framework import status from rest_framework import status
from openslides.utils.rest_api import ModelViewSet, Response from openslides.utils.rest_api import ModelViewSet, Response, detail_route
from openslides.utils.views import APIView, PDFView from openslides.utils.views import APIView, PDFView
from .models import Group, User from .models import Group, User
@ -83,6 +84,18 @@ class UserViewSet(ModelViewSet):
serializer_class = UserShortSerializer serializer_class = UserShortSerializer
return serializer_class return serializer_class
@detail_route(methods=['post'])
def reset_password(self, request, pk=None):
"""
View to reset the password (using the default password).
"""
if not request.user.has_perm('users.can_manage'):
self.permission_denied(request)
user = self.get_object()
user.set_password(user.default_password)
user.save()
return Response({'detail': _('Password successfully reset.')})
class GroupViewSet(ModelViewSet): class GroupViewSet(ModelViewSet):
""" """

View File

@ -127,6 +127,21 @@ class UserDelete(TestCase):
self.assertFalse(User.objects.filter(username='Test name bo3zieT3iefahng0ahqu').exists()) self.assertFalse(User.objects.filter(username='Test name bo3zieT3iefahng0ahqu').exists())
class UserResetPassword(TestCase):
"""
Tests resetting users password via REST API by a manager.
"""
def test_reset(self):
admin_client = APIClient()
admin_client.login(username='admin', password='admin')
user = User.objects.create(username='Test name ooMoa4ou4mohn2eo1ree')
user.default_password = 'new_password_Yuuh8OoQueePahngohy3'
user.save()
response = admin_client.post(reverse('user-reset-password', args=[user.pk]))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(User.objects.get(pk=user.pk).check_password('new_password_Yuuh8OoQueePahngohy3'))
class GroupCreate(TestCase): class GroupCreate(TestCase):
""" """
Tests creation of groups via REST API. Tests creation of groups via REST API.