Move password set views to the authentication app
Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -1,22 +1,27 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.forms import SetPasswordForm
|
||||
from django.contrib.auth.views import (
|
||||
LoginView, LogoutView, PasswordChangeDoneView, PasswordChangeView,
|
||||
PasswordResetCompleteView, PasswordResetConfirmView, PasswordResetDoneView,
|
||||
PasswordResetView
|
||||
)
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import redirect
|
||||
from django.urls import reverse, reverse_lazy
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.translation import ungettext, ugettext_lazy as _
|
||||
|
||||
from stronghold.views import StrongholdPublicMixin
|
||||
|
||||
import mayan
|
||||
from mayan.apps.common.generics import MultipleObjectFormActionView
|
||||
from mayan.apps.common.settings import (
|
||||
setting_home_view, setting_project_title, setting_project_url
|
||||
)
|
||||
from mayan.apps.user_management.permissions import permission_user_edit
|
||||
|
||||
from .forms import EmailAuthenticationForm, UsernameAuthenticationForm
|
||||
from .settings import setting_login_method, setting_maximum_session_length
|
||||
@@ -131,3 +136,71 @@ class MayanPasswordResetView(StrongholdPublicMixin, PasswordResetView):
|
||||
viewname='authentication:password_reset_done_view'
|
||||
)
|
||||
template_name = 'authentication/password_reset_form.html'
|
||||
|
||||
|
||||
class UserSetPasswordView(MultipleObjectFormActionView):
|
||||
form_class = SetPasswordForm
|
||||
model = get_user_model()
|
||||
object_permission = permission_user_edit
|
||||
success_message = _('Password change request performed on %(count)d user')
|
||||
success_message_plural = _(
|
||||
'Password change request performed on %(count)d users'
|
||||
)
|
||||
|
||||
def get_extra_context(self):
|
||||
queryset = self.get_object_list()
|
||||
|
||||
result = {
|
||||
'submit_label': _('Submit'),
|
||||
'title': ungettext(
|
||||
singular='Change user password',
|
||||
plural='Change users passwords',
|
||||
number=queryset.count()
|
||||
)
|
||||
}
|
||||
|
||||
if queryset.count() == 1:
|
||||
result.update(
|
||||
{
|
||||
'object': queryset.first(),
|
||||
'title': _('Change password for user: %s') % queryset.first()
|
||||
}
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
def get_form_extra_kwargs(self):
|
||||
queryset = self.get_object_list()
|
||||
result = {}
|
||||
if queryset:
|
||||
result['user'] = queryset.first()
|
||||
return result
|
||||
else:
|
||||
raise PermissionDenied
|
||||
|
||||
def object_action(self, form, instance):
|
||||
try:
|
||||
if instance.is_superuser or instance.is_staff:
|
||||
messages.error(
|
||||
message=_(
|
||||
'Super user and staff user password '
|
||||
'reseting is not allowed, use the admin '
|
||||
'interface for these cases.'
|
||||
), request=self.request
|
||||
)
|
||||
else:
|
||||
instance.set_password(form.cleaned_data['new_password1'])
|
||||
instance.save()
|
||||
messages.success(
|
||||
message=_(
|
||||
'Successful password reset for user: %s.'
|
||||
) % instance, request=self.request
|
||||
)
|
||||
except Exception as exception:
|
||||
messages.error(
|
||||
message=_(
|
||||
'Error reseting password for user "%(user)s": %(error)s'
|
||||
) % {
|
||||
'user': instance, 'error': exception
|
||||
}, request=self.request
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user