From 391180090dc9d198b76efb88c257358bdde7de76 Mon Sep 17 00:00:00 2001 From: Spitap Date: Thu, 29 Dec 2022 19:03:28 +0100 Subject: [PATCH] Improved UI --- .../views/user/PasswordRecoveryChangeForm.vue | 14 +++++--------- .../views/user/PasswordRecoverySmsTotpForm.vue | 17 +++++++---------- modoboa/core/api/v2/serializers.py | 6 ++---- modoboa/core/api/v2/tests.py | 2 +- modoboa/core/api/v2/views.py | 13 ++++++------- 5 files changed, 21 insertions(+), 31 deletions(-) diff --git a/frontend/src/views/user/PasswordRecoveryChangeForm.vue b/frontend/src/views/user/PasswordRecoveryChangeForm.vue index 47b557c70..c8e02f40f 100644 --- a/frontend/src/views/user/PasswordRecoveryChangeForm.vue +++ b/frontend/src/views/user/PasswordRecoveryChangeForm.vue @@ -102,29 +102,25 @@ export default { id: this.id } this.loading = true + document.getElementById('password_validation').innerHTML = '' auth.changePassword(payload).then(resp => { - document.getElementById('password_validation').innerHTML = '' if (resp.status === 200) { this.loading = false this.returnLogin() } }).catch(err => { + let message = '' this.loading = false if (err.response.status === 403) { - this.$refs.observer.setErrors({ - password_confirmed: this.$gettext('Invalid reset token.') - }) + message = this.$gettext('Invalid reset token.') } else if (err.response.status === 404) { - this.$refs.observer.setErrors({ - password_confirmed: this.$gettext('User unknown.') - }) + message = this.$gettext('User unknown.') } else if (err.response.status === 400 && err.response.data.type === 'password_requirement') { - let message = '' err.response.data.errors.forEach(element => { message += this.$gettext(element) + '
' }) - document.getElementById('password_validation').innerHTML = message } + document.getElementById('password_validation').innerHTML = message }) } }, diff --git a/frontend/src/views/user/PasswordRecoverySmsTotpForm.vue b/frontend/src/views/user/PasswordRecoverySmsTotpForm.vue index 23ad9f0c5..f65ceaf0f 100644 --- a/frontend/src/views/user/PasswordRecoverySmsTotpForm.vue +++ b/frontend/src/views/user/PasswordRecoverySmsTotpForm.vue @@ -57,9 +57,6 @@ export default { return { loading: false, sms_totp: '', - password: '', - password_confirmed: '', - user_pk: -1, errors: {} } }, @@ -67,12 +64,10 @@ export default { returnLogin () { this.$router.push({ name: 'Login' }) }, - async userPkError () { + async showErrorDialog (body) { const confirm = await this.$refs.confirm.open( this.$gettext('Error'), - this.$gettext( - 'User seems wrong, return to login or restart reset the process?' - ), + this.$gettext(body), { color: 'error', cancelLabel: this.$gettext('Return to login'), @@ -96,7 +91,7 @@ export default { }).catch(err => { if (err.response.status === 400) { this.loading = false - this.userPkError() + this.showErrorDialog('User seems wrong, return to login or restart reset the process?') } }) }, @@ -118,9 +113,11 @@ export default { } }).catch(err => { this.loading = false - if (err.response.status === 500) { + if (err.response.status === 403 && 'reason' in err.response.data) { + this.showErrorDialog(err.response.data.reason) + } else if (err.response.status === 400 && 'reason' in err.response.data) { this.$refs.observer.setErrors({ - password_confirmed: this.$gettext('Invalid totp.') + sms_totp: this.$gettext(err.response.data.reason) }) } }) diff --git a/modoboa/core/api/v2/serializers.py b/modoboa/core/api/v2/serializers.py index 46e6d792d..1e847f0c9 100644 --- a/modoboa/core/api/v2/serializers.py +++ b/modoboa/core/api/v2/serializers.py @@ -375,9 +375,8 @@ class PasswordRecoverySmsConfirmSerializer(serializers.Serializer): except KeyError: raise CustomValidationError( {"reason": "totp secret not set in session"}, 403) - if not oath.accept_totp(self.context["totp_secret"], data["sms_totp"])[0]: - raise CustomValidationError({"reason": "Wrong totp"}, 403) + raise CustomValidationError({"reason": "Wrong totp"}, 400) # Attempt to get user, will raise an error if pk is not valid self.context["user"] = User.objects.get( @@ -453,8 +452,7 @@ class PasswordRecoveryConfirmSerializer(serializers.Serializer): try: password_validation.validate_password(data["new_password1"], user) except djangoValidationError as e: - raise PasswordRequirementsFailure(e.error_list) - + raise PasswordRequirementsFailure(e.messages) self.context["user"] = user return super().validate(data) diff --git a/modoboa/core/api/v2/tests.py b/modoboa/core/api/v2/tests.py index 258273176..6c72b102f 100644 --- a/modoboa/core/api/v2/tests.py +++ b/modoboa/core/api/v2/tests.py @@ -317,7 +317,7 @@ class PasswordResetTestCase(AccountViewSetTestCase): # False totp code data = {"sms_totp": "123456", "type": "confirm"} response = self.client.post(url_sms_totp, data, format="json") - self.assertEqual(response.status_code, 403) + self.assertEqual(response.status_code, 400) self.assertEqual(response.json()["reason"], "Wrong totp") # Good totp code diff --git a/modoboa/core/api/v2/views.py b/modoboa/core/api/v2/views.py index 506820552..efe3c9814 100644 --- a/modoboa/core/api/v2/views.py +++ b/modoboa/core/api/v2/views.py @@ -147,13 +147,12 @@ class PasswordResetConfirmView(APIView): try: serializer.is_valid(raise_exception=True) except PasswordRequirementsFailure as e: - if type(e) == PasswordRequirementsFailure: - data = {"type": "password_requirement"} - errors = [] - for element in e.message_list: - errors.append(element) - data.update({"errors": errors}) - return response.Response(data, 400) + data = {"type": "password_requirement"} + errors = [] + for element in e.message_list: + errors.append(element) + data.update({"errors": errors}) + return response.Response(data, 400) serializer.save() return response.Response(status=200)