From 81170b4b7b316216a88487abeda7ac8201fea100 Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 9 Mar 2021 12:18:04 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=EF=BC=8C=E9=9D=9E=E5=B8=B8=E7=BB=99=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit perf: 优化报错 perf: 优化忘记密码 perf: 添加注释 --- apps/authentication/forms.py | 26 +- apps/authentication/mixins.py | 11 +- .../templates/authentication/login.html | 256 ++--- apps/authentication/views/login.py | 9 +- apps/jumpserver/conf.py | 2 + apps/jumpserver/context_processor.py | 4 +- apps/jumpserver/settings/base.py | 5 +- apps/locale/zh/LC_MESSAGES/django.mo | Bin 69720 -> 70335 bytes apps/locale/zh/LC_MESSAGES/django.po | 912 ++++++++++-------- apps/static/css/login-style.css | 8 +- apps/users/api/profile.py | 6 - .../templates/users/forgot_password.html | 8 + 12 files changed, 732 insertions(+), 515 deletions(-) diff --git a/apps/authentication/forms.py b/apps/authentication/forms.py index fe28edb68..447d842bd 100644 --- a/apps/authentication/forms.py +++ b/apps/authentication/forms.py @@ -8,11 +8,26 @@ from captcha.fields import CaptchaField, CaptchaTextInput class UserLoginForm(forms.Form): - username = forms.CharField(label=_('Username'), max_length=100) + days_auto_login = int(settings.SESSION_COOKIE_AGE / 3600 / 24) + disable_days_auto_login = settings.SESSION_EXPIRE_AT_BROWSER_CLOSE_FORCE or days_auto_login < 1 + + username = forms.CharField( + label=_('Username'), max_length=100, + widget=forms.TextInput(attrs={ + 'placeholder': _("Username"), + 'autofocus': 'autofocus' + }) + ) password = forms.CharField( label=_('Password'), widget=forms.PasswordInput, max_length=1024, strip=False ) + auto_login = forms.BooleanField( + label=_("{} days auto login").format(days_auto_login or 1), + required=False, initial=False, widget=forms.CheckboxInput( + attrs={'disabled': disable_days_auto_login} + ) + ) def confirm_login_allowed(self, user): if not user.is_staff: @@ -35,8 +50,13 @@ class CaptchaMixin(forms.Form): class ChallengeMixin(forms.Form): - challenge = forms.CharField(label=_('MFA code'), max_length=6, - required=False) + challenge = forms.CharField( + label=_('MFA code'), max_length=6, required=False, + widget=forms.TextInput(attrs={ + 'placeholder': _("MFA code"), + 'style': 'width: 50%' + }) + ) def get_user_login_form_cls(*, captcha=False): diff --git a/apps/authentication/mixins.py b/apps/authentication/mixins.py index e4813ef3f..9702e6046 100644 --- a/apps/authentication/mixins.py +++ b/apps/authentication/mixins.py @@ -82,7 +82,7 @@ class AuthMixin: return raw_passwd def raise_credential_error(self, error): - raise self.partial_credential_error(error=errors.reason_password_decrypt_failed) + raise self.partial_credential_error(error=error) def get_auth_data(self, decrypt_passwd=False): request = self.request @@ -91,8 +91,8 @@ class AuthMixin: else: data = request.POST - items = ['username', 'password', 'challenge', 'public_key'] - username, password, challenge, public_key = bulk_get(data, *items, default='') + items = ['username', 'password', 'challenge', 'public_key', 'auto_login'] + username, password, challenge, public_key, auto_login = bulk_get(data, *items, default='') password = password + challenge.strip() ip = self.get_request_ip() self.partial_credential_error = partial(errors.CredentialError, username=username, ip=ip, request=request) @@ -101,7 +101,7 @@ class AuthMixin: password = self.decrypt_passwd(password) if not password: self.raise_credential_error(errors.reason_password_decrypt_failed) - return username, password, public_key, ip + return username, password, public_key, ip, auto_login def _check_only_allow_exists_user_auth(self, username): # 仅允许预先存在的用户认证 @@ -131,7 +131,7 @@ class AuthMixin: def check_user_auth(self, decrypt_passwd=False): self.check_is_block() request = self.request - username, password, public_key, ip = self.get_auth_data(decrypt_passwd=decrypt_passwd) + username, password, public_key, ip, auto_login = self.get_auth_data(decrypt_passwd=decrypt_passwd) self._check_only_allow_exists_user_auth(username) user = self._check_auth_user_is_valid(username, password, public_key) @@ -145,6 +145,7 @@ class AuthMixin: clean_failed_count(username, ip) request.session['auth_password'] = 1 request.session['user_id'] = str(user.id) + request.session['auto_login'] = auto_login request.session['auth_backend'] = auth_backend return user diff --git a/apps/authentication/templates/authentication/login.html b/apps/authentication/templates/authentication/login.html index e06567aa4..9866537ba 100644 --- a/apps/authentication/templates/authentication/login.html +++ b/apps/authentication/templates/authentication/login.html @@ -1,12 +1,11 @@ -{% load static %} {% load i18n %} +{% load bootstrap3 %} +{% load static %} - - - + {{ JMS_TITLE }} @@ -16,6 +15,8 @@ + + @@ -24,26 +25,54 @@ - -
-
-
- + +