2020-03-12 08:24:38 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
|
|
|
from django.contrib.auth import authenticate
|
|
|
|
from django.shortcuts import redirect
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2021-07-30 07:19:00 +00:00
|
|
|
from django.views.generic.edit import FormView
|
2021-11-10 03:30:48 +00:00
|
|
|
|
2021-09-08 08:40:09 +00:00
|
|
|
from authentication import errors
|
2023-07-24 03:52:25 +00:00
|
|
|
from authentication.mixins import AuthMixin
|
2020-03-12 08:24:38 +00:00
|
|
|
from common.utils import get_logger
|
|
|
|
from ... import forms
|
|
|
|
from ...utils import (
|
|
|
|
get_user_or_pre_auth_user,
|
|
|
|
)
|
|
|
|
|
2021-07-30 07:19:00 +00:00
|
|
|
__all__ = ['UserVerifyPasswordView']
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
class UserVerifyPasswordView(AuthMixin, FormView):
|
2020-03-12 08:24:38 +00:00
|
|
|
template_name = 'users/user_password_verify.html'
|
|
|
|
form_class = forms.UserCheckPasswordForm
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
user = get_user_or_pre_auth_user(self.request)
|
2021-11-10 03:30:48 +00:00
|
|
|
if user is None:
|
|
|
|
return redirect('authentication:login')
|
|
|
|
|
2021-09-08 08:40:09 +00:00
|
|
|
try:
|
2022-05-07 08:20:12 +00:00
|
|
|
password = form.cleaned_data['password']
|
2021-09-08 08:40:09 +00:00
|
|
|
except errors.AuthFailedError as e:
|
|
|
|
form.add_error("password", _(f"Password invalid") + f'({e.msg})')
|
|
|
|
return self.form_invalid(form)
|
2021-11-10 03:30:48 +00:00
|
|
|
|
2020-05-18 10:02:37 +00:00
|
|
|
user = authenticate(request=self.request, username=user.username, password=password)
|
2020-03-12 08:24:38 +00:00
|
|
|
if not user:
|
|
|
|
form.add_error("password", _("Password invalid"))
|
|
|
|
return self.form_invalid(form)
|
2021-11-10 03:30:48 +00:00
|
|
|
|
|
|
|
self.mark_password_ok(user)
|
2020-03-12 08:24:38 +00:00
|
|
|
return redirect(self.get_success_url())
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
referer = self.request.META.get('HTTP_REFERER')
|
|
|
|
next_url = self.request.GET.get("next")
|
|
|
|
if next_url:
|
|
|
|
return next_url
|
|
|
|
else:
|
|
|
|
return referer
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'user': get_user_or_pre_auth_user(self.request)
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|