diff --git a/max/private_static/i18n/de.json b/max/private_static/i18n/de.json index 92f693f..2f20b1f 100644 --- a/max/private_static/i18n/de.json +++ b/max/private_static/i18n/de.json @@ -51,6 +51,9 @@ "Note changed successfully!": "Notiz wurde erfolgreich bearbeitet!", "Both passwords must be given": "Beide Passwörter müssen gegeben sein", "Both passwords must be the same": "Beide Passwörter müssen übereinstimmen", + "Current password": "Aktuelles Passwort", + "The current password must be given": "Das aktuelle Passwort muss gegeben sein", + "The current password is wrong": "Das aktuelle Passwort ist falsch", "Password changed successfully!": "Passwort erfolgreich geändert!", "Admin is not set": "Admin ist nicht gesetzt", "Creation of {email} was successful!": "{email} wurde erfolgreich erstellt!", diff --git a/max/routes/user/change_password.py b/max/routes/user/change_password.py index e481a92..90b119a 100644 --- a/max/routes/user/change_password.py +++ b/max/routes/user/change_password.py @@ -1,6 +1,6 @@ from flask import flash -from max.auth import change_password +from max.auth import change_password, check_password from max.permissions import AllowAdminOrSelf from max.translations import t @@ -13,15 +13,34 @@ class UserChangePassword(AllowAdminOrSelf, BackToUsersMixin, FetchUserTemplateGe def get_webpagetitle(self): return t("Change password for {user}", user=self.user.email) + def is_own_password(self): + return self.user.id == self.auth_user.id + + def get_context(self): + context = super().get_context() + context["is_own_password"] = self.is_own_password() + return context + def post(self): pw1 = self.request.form.get("password1") pw2 = self.request.form.get("password2") if not pw1 or not pw2: flash(t("Both passwords must be given"), category="error") return + if not isinstance(pw1, str) or not isinstance(pw2, str): - flash("Invalid data") + flash("Invalid data", category="error") return + + if self.is_own_password(): + current_password = self.request.form.get("current_password") + if not current_password or not isinstance(current_password, str): + flash(t("The current password must be given"), category="error") + return + if not check_password(current_password, self.user.passwordhash): + flash(t("The current password is wrong"), category="error") + return + if pw1 != pw2: flash(t("Both passwords must be the same"), category="error") return diff --git a/max/templates/user/change-password.html b/max/templates/user/change-password.html index 40e70ee..1cdf013 100644 --- a/max/templates/user/change-password.html +++ b/max/templates/user/change-password.html @@ -5,14 +5,21 @@ {% include "alerts.html" %}