2017-01-17 08:34:47 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2019-02-27 00:45:00 +00:00
|
|
|
from django.views.generic import RedirectView
|
2017-01-17 08:34:47 +00:00
|
|
|
from django.shortcuts import reverse, redirect
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.views.generic.base import TemplateView
|
2017-03-31 15:46:00 +00:00
|
|
|
from django.conf import settings
|
2019-02-27 00:45:00 +00:00
|
|
|
from django.urls import reverse_lazy
|
2019-12-16 08:53:29 +00:00
|
|
|
from django.views.generic import FormView
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2021-08-24 06:20:54 +00:00
|
|
|
from users.notifications import ResetPasswordSuccessMsg, ResetPasswordMsg
|
2021-04-29 09:15:46 +00:00
|
|
|
from common.utils import get_object_or_none, FlashMessageUtil
|
2021-04-08 06:59:14 +00:00
|
|
|
from common.permissions import IsValidUser
|
|
|
|
from common.mixins.views import PermissionsMixin
|
2020-06-03 03:43:43 +00:00
|
|
|
from ...models import User
|
|
|
|
from ...utils import (
|
2021-08-24 06:20:54 +00:00
|
|
|
get_password_check_rules, check_password_rules,
|
2019-02-27 00:45:00 +00:00
|
|
|
)
|
2020-06-03 03:43:43 +00:00
|
|
|
from ... import forms
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
|
2018-01-29 08:57:50 +00:00
|
|
|
__all__ = [
|
2021-04-29 09:15:46 +00:00
|
|
|
'UserLoginView', 'UserResetPasswordView', 'UserForgotPasswordView', 'UserFirstLoginView',
|
2018-01-29 08:57:50 +00:00
|
|
|
]
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
|
2019-02-27 00:45:00 +00:00
|
|
|
class UserLoginView(RedirectView):
|
2019-03-29 07:53:31 +00:00
|
|
|
url = reverse_lazy('authentication:login')
|
|
|
|
query_string = True
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
class UserForgotPasswordView(FormView):
|
2017-01-17 08:34:47 +00:00
|
|
|
template_name = 'users/forgot_password.html'
|
2019-12-16 08:53:29 +00:00
|
|
|
form_class = forms.UserForgotPasswordForm
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2021-04-29 09:15:46 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_redirect_message_url():
|
|
|
|
message_data = {
|
|
|
|
'title': _('Send reset password message'),
|
|
|
|
'message': _('Send reset password mail success, '
|
|
|
|
'login your mail box and follow it '),
|
|
|
|
'redirect_url': reverse('authentication:login'),
|
|
|
|
}
|
|
|
|
url = FlashMessageUtil.gen_message_url(message_data)
|
|
|
|
return url
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
email = form.cleaned_data['email']
|
2017-01-17 08:34:47 +00:00
|
|
|
user = get_object_or_none(User, email=email)
|
|
|
|
if not user:
|
2018-11-23 08:24:30 +00:00
|
|
|
error = _('Email address invalid, please input again')
|
2020-04-16 10:29:02 +00:00
|
|
|
form.add_error('email', error)
|
|
|
|
return self.form_invalid(form)
|
2021-01-17 07:28:22 +00:00
|
|
|
|
|
|
|
if not user.is_local:
|
2021-01-17 08:22:06 +00:00
|
|
|
error = _(
|
2021-01-18 07:04:53 +00:00
|
|
|
'The user is from {}, please go to the corresponding system to change the password'
|
2021-01-20 03:38:09 +00:00
|
|
|
).format(user.get_source_display())
|
2021-01-17 07:28:22 +00:00
|
|
|
form.add_error('email', error)
|
2020-04-16 10:29:02 +00:00
|
|
|
return self.form_invalid(form)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
ResetPasswordMsg(user).publish_async()
|
2021-04-29 09:15:46 +00:00
|
|
|
url = self.get_redirect_message_url()
|
|
|
|
return redirect(url)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
class UserResetPasswordView(FormView):
|
2017-01-17 08:34:47 +00:00
|
|
|
template_name = 'users/reset_password.html'
|
2019-12-16 08:53:29 +00:00
|
|
|
form_class = forms.UserTokenResetPasswordForm
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
2019-12-16 08:53:29 +00:00
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
errors = kwargs.get('errors')
|
|
|
|
if errors:
|
|
|
|
context['errors'] = errors
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
token = self.request.GET.get('token', '')
|
2019-04-15 06:37:45 +00:00
|
|
|
user = User.validate_reset_password_token(token)
|
2017-01-17 08:34:47 +00:00
|
|
|
if not user:
|
2019-12-16 08:53:29 +00:00
|
|
|
context['errors'] = _('Token invalid or expired')
|
|
|
|
context['token_invalid'] = True
|
2021-07-30 07:19:00 +00:00
|
|
|
else:
|
|
|
|
check_rules = get_password_check_rules(user)
|
|
|
|
context['password_check_rules'] = check_rules
|
2019-12-16 08:53:29 +00:00
|
|
|
return context
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
token = self.request.GET.get('token')
|
2019-04-15 06:37:45 +00:00
|
|
|
user = User.validate_reset_password_token(token)
|
|
|
|
if not user:
|
2020-04-17 03:26:29 +00:00
|
|
|
error = _('Token invalid or expired')
|
|
|
|
form.add_error('new_password', error)
|
|
|
|
return self.form_invalid(form)
|
2019-12-16 08:53:29 +00:00
|
|
|
|
2018-12-27 08:47:40 +00:00
|
|
|
if not user.can_update_password():
|
2020-04-17 15:41:50 +00:00
|
|
|
error = _('User auth from {}, go there change password')
|
|
|
|
form.add_error('new_password', error.format(user.get_source_display()))
|
2020-04-17 03:26:29 +00:00
|
|
|
return self.form_invalid(form)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
password = form.cleaned_data['new_password']
|
2021-08-17 08:01:27 +00:00
|
|
|
is_ok = check_password_rules(password, is_org_admin=user.is_org_admin)
|
2018-06-05 09:26:31 +00:00
|
|
|
if not is_ok:
|
2020-04-17 03:26:29 +00:00
|
|
|
error = _('* Your password does not meet the requirements')
|
|
|
|
form.add_error('new_password', error)
|
|
|
|
return self.form_invalid(form)
|
2018-06-05 09:26:31 +00:00
|
|
|
|
2021-04-28 09:03:20 +00:00
|
|
|
if user.is_history_password(password):
|
|
|
|
limit_count = settings.OLD_PASSWORD_HISTORY_LIMIT_COUNT
|
|
|
|
error = _('* The new password cannot be the last {} passwords').format(limit_count)
|
|
|
|
form.add_error('new_password', error)
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
2017-01-17 08:34:47 +00:00
|
|
|
user.reset_password(password)
|
2019-04-15 06:37:45 +00:00
|
|
|
User.expired_reset_password_token(token)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
ResetPasswordSuccessMsg(user, self.request).publish_async()
|
2021-04-29 09:15:46 +00:00
|
|
|
url = self.get_redirect_url()
|
|
|
|
return redirect(url)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_redirect_url():
|
|
|
|
message_data = {
|
|
|
|
'title': _('Reset password success'),
|
|
|
|
'message': _('Reset password success, return to login page'),
|
|
|
|
'redirect_url': reverse('authentication:login'),
|
|
|
|
'auto_redirect': True,
|
|
|
|
}
|
|
|
|
return FlashMessageUtil.gen_message_url(message_data)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
|
2020-06-03 04:26:53 +00:00
|
|
|
class UserFirstLoginView(PermissionsMixin, TemplateView):
|
2017-01-17 08:34:47 +00:00
|
|
|
template_name = 'users/first_login.html'
|
2019-06-19 03:31:38 +00:00
|
|
|
permission_classes = [IsValidUser]
|