From 675a8cd6f276b6432f0d1841a65e35591c6688d9 Mon Sep 17 00:00:00 2001 From: Marco Morath Date: Thu, 26 Nov 2026 09:18:47 +0100 Subject: [PATCH] fix:Anzeige Sperre --- README.md | 6 + docs/sicherheit.md | 7 +- surveybox/accounts/audit.py | 1 + surveybox/accounts/service.py | 15 +- surveybox/locale/de/LC_MESSAGES/messages.po | 76 +++++-- surveybox/locale/en/LC_MESSAGES/messages.po | 72 +++++-- surveybox/locale/fr/LC_MESSAGES/messages.po | 76 +++++-- surveybox/locale/messages.pot | 196 ++++++++++++------- surveybox/models/accounts.py | 7 +- surveybox/web/routes/admin.py | 16 ++ surveybox/web/templates/admin_user_edit.html | 13 ++ surveybox/web/templates/admin_users.html | 1 + surveybox/web/templating.py | 1 + tests/test_accounts.py | 24 ++- 14 files changed, 388 insertions(+), 123 deletions(-) diff --git a/README.md b/README.md index 81de1d5..574113a 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,12 @@ Das entfernt alle zweiten Faktoren (Authenticator-App und Passkeys) und gibt einen Link zum Setzen eines neuen Passworts aus (24 Stunden gültig). Für andere Benutzer geht das auch in der Verwaltung unter *Benutzer → Anmeldung zurücksetzen*. +Nach fünf falschen Passwörtern in Folge ist ein Konto 15 Minuten gesperrt; die +Anmeldemeldung verrät das nicht. Die Benutzerliste zeigt das Konto dann als +„vorübergehend gesperrt“, die Seite des Benutzers Grund, Beginn und Ende der +Sperre. Mit *Sperre aufheben* kann sich die Person sofort wieder anmelden; +das steht im Protokoll. + ### Formulare Administratoren und Ersteller legen Formulare unter *Formulare* an. Ein diff --git a/docs/sicherheit.md b/docs/sicherheit.md index 1731003..80eca48 100644 --- a/docs/sicherheit.md +++ b/docs/sicherheit.md @@ -85,7 +85,10 @@ produktive Instanz. gesperrt“. Die Meldung ist bei falschem Passwort, gesperrtem und unbekanntem Konto dieselbe – sie verrät nicht, ob das Konto existiert. Nach 30 Fehlversuchen von derselben Adresse in 15 Minuten erscheint - „Zu viele Versuche“. + „Zu viele Versuche“. In der Benutzerverwaltung steht das Konto als + „vorübergehend gesperrt“; die Seite des Benutzers nennt Grund, Beginn + und Ende der Sperre, „Sperre aufheben“ gibt es sofort frei (Protokoll: + „Sperre eines Kontos aufgehoben“). - Formular-Link eines Formulars mit Zugriff „Ausgewählte Benutzer“ als nicht eingeladene Person öffnen: kein Zugriff. - Antwort-ID einer fremden Antwort in die Adresse einsetzen: 404. @@ -103,7 +106,7 @@ produktive Instanz. | 26.11.2026 | Testinstanz, 2 Web-Prozesse | Lasttest (95 %) | bestanden | 20 gleichzeitige Abrufe, je 100: `/` 142 ms, `/login` 169 ms, `/f/` 179 ms; einzeln ~30 ms; ~270 Abrufe/s | | 26.11.2026 | Testinstanz | Update mit Daten | bestanden mit Bemerkung | Neues Image (Debian-Updates, ohne pip) auf bestehende Daten: „Datenbank ist aktuell (0017_response_note)“, Formulare, Antworten, Gestaltung und Anmeldung unverändert. Kein Schemawechsel seit 0017; Migrationen mit Daten bei den Phasen-Updates erfolgt. Voller Weg mit `OLD_IMAGE` und Sicherung vor dem Update beim nächsten Release mit Migration | | 26.11.2026 | Testinstanz | Wiederherstellung | bestanden | Manuelle Sicherung zurückgespielt: Stand der Sicherung vollständig (Formulare, Antworten, Medien, Gestaltung, Anmeldung); nach der Sicherung angelegtes Formular entfernt; nach der Sicherung gelöschtes Konto blieb durch das Löschjournal gelöscht | -| | | Stichproben | | | +| 26.11.2026 | Testinstanz | Stichproben | bestanden mit Bemerkung | Sperre nach Fehlversuchen greift, Meldung verrät nichts, „Zu viele Versuche“ je Adresse; fremde Formulare und Antworten nicht erreichbar; umbenannte Dateien abgelehnt. Bemerkung: Die Sperre war in der Benutzerverwaltung nicht zu sehen; ergänzt (Anzeige „vorübergehend gesperrt“, Grund und Ende, Aufheben durch Admins, `test_accounts`) | ### Bewertung ZAP Baseline (26.11.2026) diff --git a/surveybox/accounts/audit.py b/surveybox/accounts/audit.py index ec5927c..4339911 100644 --- a/surveybox/accounts/audit.py +++ b/surveybox/accounts/audit.py @@ -73,6 +73,7 @@ ACTIONS = { "form.history_restored", "form.trash_restored", "admin.history_changed", + "admin.user_unlocked", } diff --git a/surveybox/accounts/service.py b/surveybox/accounts/service.py index a068711..2dd51d8 100644 --- a/surveybox/accounts/service.py +++ b/surveybox/accounts/service.py @@ -130,7 +130,9 @@ def password_login( if user.failed_logins >= LOCK_AFTER_FAILURES: user.locked_until = now + LOCK_DURATION user.failed_logins = 0 - audit.record(db, "auth.locked", target_type="user", target_id=user.id) + audit.record(db, "auth.locked", target_type="user", target_id=user.id, + details={"attempts": LOCK_AFTER_FAILURES, + "minutes": int(LOCK_DURATION.total_seconds() // 60)}) return LoginResult(LoginOutcome.INVALID) user.failed_logins = 0 @@ -455,6 +457,17 @@ def create_user( return user +def unlock(db: Session, admin: UserAccount, user: UserAccount) -> bool: + """Sperre nach Fehlversuchen aufheben. False, wenn keine bestand.""" + was_locked = user.is_locked + user.locked_until = None + user.failed_logins = 0 + if was_locked: + audit.record(db, "admin.user_unlocked", actor_id=admin.id, target_type="user", + target_id=user.id) + return was_locked + + def update_user( db: Session, admin: UserAccount, diff --git a/surveybox/locale/de/LC_MESSAGES/messages.po b/surveybox/locale/de/LC_MESSAGES/messages.po index 66f50a6..2ca34c1 100644 --- a/surveybox/locale/de/LC_MESSAGES/messages.po +++ b/surveybox/locale/de/LC_MESSAGES/messages.po @@ -4,7 +4,7 @@ msgid "" msgstr "" "Project-Id-Version: Surveybox VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2026-11-26 08:43+0100\n" +"POT-Creation-Date: 2026-11-26 09:14+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language: de\n" @@ -828,6 +828,10 @@ msgstr "Die Antwort wurde geändert und der Vermerk gespeichert." msgid "All sessions of this user have been signed out." msgstr "Alle Sitzungen dieses Benutzers wurden abgemeldet." +#: surveybox/web/templating.py +msgid "The lock has been lifted. The user can sign in again." +msgstr "Die Sperre wurde aufgehoben. Der Benutzer kann sich wieder anmelden." + #: surveybox/web/templating.py msgid "The passkey has been added." msgstr "Der Passkey wurde hinzugefügt." @@ -1132,6 +1136,10 @@ msgstr "Gelöschtes Formular wiederhergestellt" msgid "Form history settings changed" msgstr "Einstellungen zu Verlauf und Papierkorb geändert" +#: surveybox/web/routes/admin.py +msgid "Lock of an account lifted" +msgstr "Sperre eines Kontos aufgehoben" + #: surveybox/web/routes/admin.py msgid "Account deleted" msgstr "Konto gelöscht" @@ -3133,8 +3141,9 @@ msgstr "Art" msgid "Recipient" msgstr "Empfänger" -#: surveybox/web/templates/admin_mail.html surveybox/web/templates/admin_users.html -#: surveybox/web/templates/followup_recipients.html surveybox/web/templates/forms_list.html +#: surveybox/web/templates/admin_mail.html surveybox/web/templates/admin_user_edit.html +#: surveybox/web/templates/admin_users.html surveybox/web/templates/followup_recipients.html +#: surveybox/web/templates/forms_list.html msgid "Status" msgstr "Status" @@ -3427,6 +3436,32 @@ msgstr "Benutzer jetzt löschen" msgid "All users" msgstr "Alle Benutzer" +#: surveybox/web/templates/admin_user_edit.html +msgid "Temporarily locked" +msgstr "Vorübergehend gesperrt" + +#: surveybox/web/templates/admin_user_edit.html +#, python-format +msgid "Reason: %(num)d failed sign-in attempts in a row." +msgstr "Grund: %(num)d fehlgeschlagene Anmeldeversuche in Folge." + +#: surveybox/web/templates/admin_user_edit.html +#, python-format +msgid "Locked since %(since)s, until %(until)s. Until then, sign-in fails even with the right password." +msgstr "" +"Gesperrt seit %(since)s, bis %(until)s. Bis dahin scheitert die Anmeldung auch mit dem richtigen " +"Passwort." + +#: surveybox/web/templates/admin_user_edit.html +msgid "Lift lock" +msgstr "Sperre aufheben" + +#: surveybox/web/templates/admin_user_edit.html +msgid "If the attempts did not come from this person, reset the password instead and check the log." +msgstr "" +"Kamen die Versuche nicht von dieser Person, setzen Sie besser die Anmeldung zurück und prüfen Sie" +" das Protokoll." + #: surveybox/web/templates/admin_user_edit.html msgid "Account is active" msgstr "Konto ist aktiv" @@ -3435,6 +3470,29 @@ msgstr "Konto ist aktiv" msgid "Last sign-in" msgstr "Letzte Anmeldung" +#: surveybox/web/templates/admin_user_edit.html surveybox/web/templates/admin_users.html +msgid "deactivated" +msgstr "deaktiviert" + +#: surveybox/web/templates/admin_user_edit.html surveybox/web/templates/admin_users.html +msgid "temporarily locked" +msgstr "vorübergehend gesperrt" + +#: surveybox/web/templates/admin_user_edit.html surveybox/web/templates/admin_users.html +msgid "invited" +msgstr "eingeladen" + +#: surveybox/web/templates/admin_user_edit.html surveybox/web/templates/admin_users.html +msgid "active" +msgstr "aktiv" + +#: surveybox/web/templates/admin_user_edit.html +#, python-format +msgid "%(num)d failed sign-in attempt since the last success" +msgid_plural "%(num)d failed sign-in attempts since the last success" +msgstr[0] "%(num)d fehlgeschlagener Anmeldeversuch seit der letzten erfolgreichen Anmeldung" +msgstr[1] "%(num)d fehlgeschlagene Anmeldeversuche seit der letzten erfolgreichen Anmeldung" + #: surveybox/web/templates/admin_user_edit.html #, python-format msgid "%(num)d passkey" @@ -3588,18 +3646,6 @@ msgstr "Suchen" msgid "Search by name or email" msgstr "Nach Name oder E-Mail suchen" -#: surveybox/web/templates/admin_users.html -msgid "deactivated" -msgstr "deaktiviert" - -#: surveybox/web/templates/admin_users.html -msgid "invited" -msgstr "eingeladen" - -#: surveybox/web/templates/admin_users.html -msgid "active" -msgstr "aktiv" - #: surveybox/web/templates/admin_users.html msgid "No users found." msgstr "Keine Benutzer gefunden." diff --git a/surveybox/locale/en/LC_MESSAGES/messages.po b/surveybox/locale/en/LC_MESSAGES/messages.po index 4735927..98efa57 100644 --- a/surveybox/locale/en/LC_MESSAGES/messages.po +++ b/surveybox/locale/en/LC_MESSAGES/messages.po @@ -4,7 +4,7 @@ msgid "" msgstr "" "Project-Id-Version: Surveybox VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2026-11-26 08:43+0100\n" +"POT-Creation-Date: 2026-11-26 09:14+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language: en\n" @@ -828,6 +828,10 @@ msgstr "The answer has been changed and the note saved." msgid "All sessions of this user have been signed out." msgstr "All sessions of this user have been signed out." +#: surveybox/web/templating.py +msgid "The lock has been lifted. The user can sign in again." +msgstr "The lock has been lifted. The user can sign in again." + #: surveybox/web/templating.py msgid "The passkey has been added." msgstr "The passkey has been added." @@ -1130,6 +1134,10 @@ msgstr "Deleted form restored" msgid "Form history settings changed" msgstr "Form history settings changed" +#: surveybox/web/routes/admin.py +msgid "Lock of an account lifted" +msgstr "Lock of an account lifted" + #: surveybox/web/routes/admin.py msgid "Account deleted" msgstr "Account deleted" @@ -3114,8 +3122,9 @@ msgstr "Type" msgid "Recipient" msgstr "Recipient" -#: surveybox/web/templates/admin_mail.html surveybox/web/templates/admin_users.html -#: surveybox/web/templates/followup_recipients.html surveybox/web/templates/forms_list.html +#: surveybox/web/templates/admin_mail.html surveybox/web/templates/admin_user_edit.html +#: surveybox/web/templates/admin_users.html surveybox/web/templates/followup_recipients.html +#: surveybox/web/templates/forms_list.html msgid "Status" msgstr "Status" @@ -3405,6 +3414,28 @@ msgstr "Delete user now" msgid "All users" msgstr "All users" +#: surveybox/web/templates/admin_user_edit.html +msgid "Temporarily locked" +msgstr "Temporarily locked" + +#: surveybox/web/templates/admin_user_edit.html +#, python-format +msgid "Reason: %(num)d failed sign-in attempts in a row." +msgstr "Reason: %(num)d failed sign-in attempts in a row." + +#: surveybox/web/templates/admin_user_edit.html +#, python-format +msgid "Locked since %(since)s, until %(until)s. Until then, sign-in fails even with the right password." +msgstr "Locked since %(since)s, until %(until)s. Until then, sign-in fails even with the right password." + +#: surveybox/web/templates/admin_user_edit.html +msgid "Lift lock" +msgstr "Lift lock" + +#: surveybox/web/templates/admin_user_edit.html +msgid "If the attempts did not come from this person, reset the password instead and check the log." +msgstr "If the attempts did not come from this person, reset the password instead and check the log." + #: surveybox/web/templates/admin_user_edit.html msgid "Account is active" msgstr "Account is active" @@ -3413,6 +3444,29 @@ msgstr "Account is active" msgid "Last sign-in" msgstr "Last sign-in" +#: surveybox/web/templates/admin_user_edit.html surveybox/web/templates/admin_users.html +msgid "deactivated" +msgstr "deactivated" + +#: surveybox/web/templates/admin_user_edit.html surveybox/web/templates/admin_users.html +msgid "temporarily locked" +msgstr "temporarily locked" + +#: surveybox/web/templates/admin_user_edit.html surveybox/web/templates/admin_users.html +msgid "invited" +msgstr "invited" + +#: surveybox/web/templates/admin_user_edit.html surveybox/web/templates/admin_users.html +msgid "active" +msgstr "active" + +#: surveybox/web/templates/admin_user_edit.html +#, python-format +msgid "%(num)d failed sign-in attempt since the last success" +msgid_plural "%(num)d failed sign-in attempts since the last success" +msgstr[0] "%(num)d failed sign-in attempt since the last success" +msgstr[1] "%(num)d failed sign-in attempts since the last success" + #: surveybox/web/templates/admin_user_edit.html #, python-format msgid "%(num)d passkey" @@ -3564,18 +3618,6 @@ msgstr "Search" msgid "Search by name or email" msgstr "Search by name or email" -#: surveybox/web/templates/admin_users.html -msgid "deactivated" -msgstr "deactivated" - -#: surveybox/web/templates/admin_users.html -msgid "invited" -msgstr "invited" - -#: surveybox/web/templates/admin_users.html -msgid "active" -msgstr "active" - #: surveybox/web/templates/admin_users.html msgid "No users found." msgstr "No users found." diff --git a/surveybox/locale/fr/LC_MESSAGES/messages.po b/surveybox/locale/fr/LC_MESSAGES/messages.po index babd919..6589c0f 100644 --- a/surveybox/locale/fr/LC_MESSAGES/messages.po +++ b/surveybox/locale/fr/LC_MESSAGES/messages.po @@ -4,7 +4,7 @@ msgid "" msgstr "" "Project-Id-Version: Surveybox VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2026-11-26 08:43+0100\n" +"POT-Creation-Date: 2026-11-26 09:14+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language: fr\n" @@ -828,6 +828,10 @@ msgstr "La réponse a été modifiée et la note enregistrée." msgid "All sessions of this user have been signed out." msgstr "Toutes les sessions de cet utilisateur ont été déconnectées." +#: surveybox/web/templating.py +msgid "The lock has been lifted. The user can sign in again." +msgstr "Le blocage a été levé. L’utilisateur peut de nouveau se connecter." + #: surveybox/web/templating.py msgid "The passkey has been added." msgstr "La clé d’accès a été ajoutée." @@ -1130,6 +1134,10 @@ msgstr "Formulaire supprimé restauré" msgid "Form history settings changed" msgstr "Paramètres de l’historique et de la corbeille modifiés" +#: surveybox/web/routes/admin.py +msgid "Lock of an account lifted" +msgstr "Blocage d’un compte levé" + #: surveybox/web/routes/admin.py msgid "Account deleted" msgstr "Compte supprimé" @@ -3136,8 +3144,9 @@ msgstr "Type" msgid "Recipient" msgstr "Destinataire" -#: surveybox/web/templates/admin_mail.html surveybox/web/templates/admin_users.html -#: surveybox/web/templates/followup_recipients.html surveybox/web/templates/forms_list.html +#: surveybox/web/templates/admin_mail.html surveybox/web/templates/admin_user_edit.html +#: surveybox/web/templates/admin_users.html surveybox/web/templates/followup_recipients.html +#: surveybox/web/templates/forms_list.html msgid "Status" msgstr "Statut" @@ -3435,6 +3444,32 @@ msgstr "Supprimer l’utilisateur maintenant" msgid "All users" msgstr "Tous les utilisateurs" +#: surveybox/web/templates/admin_user_edit.html +msgid "Temporarily locked" +msgstr "Temporairement bloqué" + +#: surveybox/web/templates/admin_user_edit.html +#, python-format +msgid "Reason: %(num)d failed sign-in attempts in a row." +msgstr "Motif : %(num)d tentatives de connexion échouées d’affilée." + +#: surveybox/web/templates/admin_user_edit.html +#, python-format +msgid "Locked since %(since)s, until %(until)s. Until then, sign-in fails even with the right password." +msgstr "" +"Bloqué depuis le %(since)s, jusqu’au %(until)s. D’ici là, la connexion échoue même avec le bon " +"mot de passe." + +#: surveybox/web/templates/admin_user_edit.html +msgid "Lift lock" +msgstr "Lever le blocage" + +#: surveybox/web/templates/admin_user_edit.html +msgid "If the attempts did not come from this person, reset the password instead and check the log." +msgstr "" +"Si les tentatives ne venaient pas de cette personne, réinitialisez plutôt la connexion et " +"consultez le journal." + #: surveybox/web/templates/admin_user_edit.html msgid "Account is active" msgstr "Le compte est actif" @@ -3443,6 +3478,29 @@ msgstr "Le compte est actif" msgid "Last sign-in" msgstr "Dernière connexion" +#: surveybox/web/templates/admin_user_edit.html surveybox/web/templates/admin_users.html +msgid "deactivated" +msgstr "désactivé" + +#: surveybox/web/templates/admin_user_edit.html surveybox/web/templates/admin_users.html +msgid "temporarily locked" +msgstr "temporairement bloqué" + +#: surveybox/web/templates/admin_user_edit.html surveybox/web/templates/admin_users.html +msgid "invited" +msgstr "invité" + +#: surveybox/web/templates/admin_user_edit.html surveybox/web/templates/admin_users.html +msgid "active" +msgstr "actif" + +#: surveybox/web/templates/admin_user_edit.html +#, python-format +msgid "%(num)d failed sign-in attempt since the last success" +msgid_plural "%(num)d failed sign-in attempts since the last success" +msgstr[0] "%(num)d tentative de connexion échouée depuis la dernière réussie" +msgstr[1] "%(num)d tentatives de connexion échouées depuis la dernière réussie" + #: surveybox/web/templates/admin_user_edit.html #, python-format msgid "%(num)d passkey" @@ -3598,18 +3656,6 @@ msgstr "Rechercher" msgid "Search by name or email" msgstr "Rechercher par nom ou e-mail" -#: surveybox/web/templates/admin_users.html -msgid "deactivated" -msgstr "désactivé" - -#: surveybox/web/templates/admin_users.html -msgid "invited" -msgstr "invité" - -#: surveybox/web/templates/admin_users.html -msgid "active" -msgstr "actif" - #: surveybox/web/templates/admin_users.html msgid "No users found." msgstr "Aucun utilisateur trouvé." diff --git a/surveybox/locale/messages.pot b/surveybox/locale/messages.pot index 1626283..efdd046 100644 --- a/surveybox/locale/messages.pot +++ b/surveybox/locale/messages.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2026-11-26 08:43+0100\n" +"POT-Creation-Date: 2026-11-26 09:14+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -206,7 +206,7 @@ msgstr "" #: surveybox/web/templates/admin_group_edit.html:119 #: surveybox/web/templates/admin_user_new.html:14 #: surveybox/web/templates/admin_users.html:18 -#: surveybox/web/templates/admin_users.html:47 +#: surveybox/web/templates/admin_users.html:48 #: surveybox/web/templates/forgot.html:14 #: surveybox/web/templates/guest_links.html:13 #: surveybox/web/templates/guest_links.html:28 @@ -347,7 +347,7 @@ msgstr "" #: surveybox/web/templates/admin_group_edit.html:117 #: surveybox/web/templates/admin_groups.html:19 #: surveybox/web/templates/admin_groups.html:39 -#: surveybox/web/templates/admin_user_edit.html:12 +#: surveybox/web/templates/admin_user_edit.html:23 #: surveybox/web/templates/admin_user_new.html:12 #: surveybox/web/templates/admin_users.html:18 #: surveybox/web/templates/register.html:16 @@ -897,78 +897,82 @@ msgid "All sessions of this user have been signed out." msgstr "" #: surveybox/web/templating.py:41 -msgid "The passkey has been added." +msgid "The lock has been lifted. The user can sign in again." msgstr "" #: surveybox/web/templating.py:42 -msgid "The passkey has been removed." +msgid "The passkey has been added." msgstr "" #: surveybox/web/templating.py:43 -msgid "The group has been saved." +msgid "The passkey has been removed." msgstr "" #: surveybox/web/templating.py:44 -msgid "The group has been deleted." +msgid "The group has been saved." msgstr "" #: surveybox/web/templating.py:45 -msgid "The form has been saved." +msgid "The group has been deleted." msgstr "" #: surveybox/web/templating.py:46 +msgid "The form has been saved." +msgstr "" + +#: surveybox/web/templating.py:47 msgid "The form has been deleted." msgstr "" -#: surveybox/web/templating.py:48 +#: surveybox/web/templating.py:49 msgid "" "The form has been moved to the trash. It can be restored there for a " "limited time." msgstr "" -#: surveybox/web/templating.py:50 +#: surveybox/web/templating.py:51 msgid "The form has been restored from the trash." msgstr "" -#: surveybox/web/templating.py:51 +#: surveybox/web/templating.py:52 msgid "The selected changes have been restored." msgstr "" -#: surveybox/web/templating.py:52 +#: surveybox/web/templating.py:53 msgid "The copy has been created. It is a draft." msgstr "" -#: surveybox/web/templating.py:54 +#: surveybox/web/templating.py:55 msgid "" "The template has been saved. New forms can be created from it in the form" " list." msgstr "" -#: surveybox/web/templating.py:56 +#: surveybox/web/templating.py:57 msgid "The settings have been saved." msgstr "" -#: surveybox/web/templating.py:57 +#: surveybox/web/templating.py:58 msgid "Waiting messages are sent within the next minute." msgstr "" -#: surveybox/web/templating.py:58 +#: surveybox/web/templating.py:59 msgid "Your notification settings have been saved." msgstr "" -#: surveybox/web/templating.py:59 +#: surveybox/web/templating.py:60 msgid "The invitation has been sent by email." msgstr "" -#: surveybox/web/templating.py:60 +#: surveybox/web/templating.py:61 msgid "A link to set a new password has been sent by email." msgstr "" -#: surveybox/web/templating.py:61 +#: surveybox/web/templating.py:62 msgid "Your account has been created. You can now sign in." msgstr "" -#: surveybox/web/templating.py:63 +#: surveybox/web/templating.py:64 msgid "" "The question has been deleted. Rules of other questions that referred to " "it were removed." @@ -1211,54 +1215,58 @@ msgid "Form history settings changed" msgstr "" #: surveybox/web/routes/admin.py:97 -msgid "Account deleted" +msgid "Lock of an account lifted" msgstr "" #: surveybox/web/routes/admin.py:98 -msgid "Own data downloaded" +msgid "Account deleted" msgstr "" #: surveybox/web/routes/admin.py:99 -msgid "Data of a user exported" +msgid "Own data downloaded" msgstr "" #: surveybox/web/routes/admin.py:100 -msgid "Data of a guest address exported" +msgid "Data of a user exported" msgstr "" #: surveybox/web/routes/admin.py:101 -msgid "Guest downloaded their data" +msgid "Data of a guest address exported" msgstr "" #: surveybox/web/routes/admin.py:102 -msgid "Logo changed" +msgid "Guest downloaded their data" msgstr "" #: surveybox/web/routes/admin.py:103 -msgid "Design changed" +msgid "Logo changed" msgstr "" #: surveybox/web/routes/admin.py:104 -msgid "Email template changed" +msgid "Design changed" msgstr "" #: surveybox/web/routes/admin.py:105 -msgid "Answers exported" +msgid "Email template changed" msgstr "" #: surveybox/web/routes/admin.py:106 -msgid "Answer deleted by a manager" +msgid "Answers exported" msgstr "" #: surveybox/web/routes/admin.py:107 -msgid "Answer changed by a manager" +msgid "Answer deleted by a manager" msgstr "" #: surveybox/web/routes/admin.py:108 +msgid "Answer changed by a manager" +msgstr "" + +#: surveybox/web/routes/admin.py:109 msgid "Guest address and its answers deleted" msgstr "" -#: surveybox/web/routes/admin.py:182 +#: surveybox/web/routes/admin.py:183 msgid "A chosen group does not fit this user type." msgstr "" @@ -2059,7 +2067,7 @@ msgstr "" #: surveybox/web/templates/admin_settings.html:60 #: surveybox/web/templates/admin_settings.html:71 #: surveybox/web/templates/admin_settings.html:85 -#: surveybox/web/templates/admin_user_edit.html:21 +#: surveybox/web/templates/admin_user_edit.html:32 #: surveybox/web/templates/followup_edit.html:57 #: surveybox/web/templates/form_access.html:26 #: surveybox/web/templates/form_designer.html:50 @@ -2436,7 +2444,7 @@ msgid "Change password" msgstr "" #: surveybox/web/templates/account.html:51 -#: surveybox/web/templates/admin_user_edit.html:26 +#: surveybox/web/templates/admin_user_edit.html:39 msgid "Two-factor authentication" msgstr "" @@ -2499,7 +2507,7 @@ msgid "Add passkey" msgstr "" #: surveybox/web/templates/account.html:103 -#: surveybox/web/templates/admin_user_edit.html:28 +#: surveybox/web/templates/admin_user_edit.html:41 msgid "Authenticator app" msgstr "" @@ -3066,7 +3074,7 @@ msgid "Person" msgstr "" #: surveybox/web/templates/admin_group_edit.html:67 -#: surveybox/web/templates/admin_user_edit.html:14 +#: surveybox/web/templates/admin_user_edit.html:25 #: surveybox/web/templates/admin_user_new.html:16 #: surveybox/web/templates/admin_users.html:18 #: surveybox/web/templates/form_team.html:11 @@ -3152,7 +3160,7 @@ msgstr "" #: surveybox/web/templates/admin_groups.html:2 #: surveybox/web/templates/admin_groups.html:15 -#: surveybox/web/templates/admin_user_edit.html:32 +#: surveybox/web/templates/admin_user_edit.html:45 #: surveybox/web/templates/admin_user_new.html:24 #: surveybox/web/templates/base.html:48 surveybox/web/templates/base.html:52 #: surveybox/web/templates/form_access.html:11 @@ -3329,7 +3337,7 @@ msgid "" msgstr "" #: surveybox/web/templates/admin_mail.html:79 -#: surveybox/web/templates/admin_user_edit.html:24 +#: surveybox/web/templates/admin_user_edit.html:35 msgid "Created" msgstr "" @@ -3343,6 +3351,7 @@ msgid "Recipient" msgstr "" #: surveybox/web/templates/admin_mail.html:79 +#: surveybox/web/templates/admin_user_edit.html:37 #: surveybox/web/templates/admin_users.html:19 #: surveybox/web/templates/followup_recipients.html:18 #: surveybox/web/templates/forms_list.html:9 @@ -3596,7 +3605,7 @@ msgid "" msgstr "" #: surveybox/web/templates/admin_user_delete.html:3 -#: surveybox/web/templates/admin_user_edit.html:72 +#: surveybox/web/templates/admin_user_edit.html:85 msgid "Delete user" msgstr "" @@ -3629,90 +3638,143 @@ msgstr "" msgid "All users" msgstr "" -#: surveybox/web/templates/admin_user_edit.html:20 +#: surveybox/web/templates/admin_user_edit.html:12 +msgid "Temporarily locked" +msgstr "" + +#: surveybox/web/templates/admin_user_edit.html:13 +#, python-format +msgid "Reason: %(num)d failed sign-in attempts in a row." +msgstr "" + +#: surveybox/web/templates/admin_user_edit.html:14 +#, python-format +msgid "" +"Locked since %(since)s, until %(until)s. Until then, sign-in fails even " +"with the right password." +msgstr "" + +#: surveybox/web/templates/admin_user_edit.html:16 +msgid "Lift lock" +msgstr "" + +#: surveybox/web/templates/admin_user_edit.html:18 +msgid "" +"If the attempts did not come from this person, reset the password instead" +" and check the log." +msgstr "" + +#: surveybox/web/templates/admin_user_edit.html:31 msgid "Account is active" msgstr "" -#: surveybox/web/templates/admin_user_edit.html:25 +#: surveybox/web/templates/admin_user_edit.html:36 #: surveybox/web/templates/admin_users.html:19 msgid "Last sign-in" msgstr "" -#: surveybox/web/templates/admin_user_edit.html:29 +#: surveybox/web/templates/admin_user_edit.html:38 +#: surveybox/web/templates/admin_users.html:29 +msgid "deactivated" +msgstr "" + +#: surveybox/web/templates/admin_user_edit.html:38 +#: surveybox/web/templates/admin_users.html:30 +msgid "temporarily locked" +msgstr "" + +#: surveybox/web/templates/admin_user_edit.html:38 +#: surveybox/web/templates/admin_users.html:31 +msgid "invited" +msgstr "" + +#: surveybox/web/templates/admin_user_edit.html:38 +#: surveybox/web/templates/admin_users.html:32 +msgid "active" +msgstr "" + +#: surveybox/web/templates/admin_user_edit.html:38 +#, python-format +msgid "%(num)d failed sign-in attempt since the last success" +msgid_plural "%(num)d failed sign-in attempts since the last success" +msgstr[0] "" +msgstr[1] "" + +#: surveybox/web/templates/admin_user_edit.html:42 #, python-format msgid "%(num)d passkey" msgid_plural "%(num)d passkeys" msgstr[0] "" msgstr[1] "" -#: surveybox/web/templates/admin_user_edit.html:30 +#: surveybox/web/templates/admin_user_edit.html:43 msgid "off" msgstr "" -#: surveybox/web/templates/admin_user_edit.html:38 +#: surveybox/web/templates/admin_user_edit.html:51 msgid "Reset sign-in" msgstr "" -#: surveybox/web/templates/admin_user_edit.html:38 +#: surveybox/web/templates/admin_user_edit.html:51 msgid "Invitation link" msgstr "" -#: surveybox/web/templates/admin_user_edit.html:41 +#: surveybox/web/templates/admin_user_edit.html:54 msgid "" "Creates a one-time link with which the person sets a new password. Valid " "for 24 hours." msgstr "" -#: surveybox/web/templates/admin_user_edit.html:43 +#: surveybox/web/templates/admin_user_edit.html:56 msgid "" "Creates a new invitation link. Valid for 7 days; earlier links become " "invalid." msgstr "" -#: surveybox/web/templates/admin_user_edit.html:49 +#: surveybox/web/templates/admin_user_edit.html:62 msgid "" "Also remove all second factors: authenticator app and passkeys (for " "example if the device was lost)" msgstr "" -#: surveybox/web/templates/admin_user_edit.html:52 +#: surveybox/web/templates/admin_user_edit.html:65 msgid "Send link by email" msgstr "" -#: surveybox/web/templates/admin_user_edit.html:53 +#: surveybox/web/templates/admin_user_edit.html:66 msgid "Create link and show it" msgstr "" -#: surveybox/web/templates/admin_user_edit.html:55 +#: surveybox/web/templates/admin_user_edit.html:68 #, python-format msgid "By email, the link goes to %(address)s and is not shown here." msgstr "" -#: surveybox/web/templates/admin_user_edit.html:59 +#: surveybox/web/templates/admin_user_edit.html:72 msgid "Sign out all sessions of this user" msgstr "" -#: surveybox/web/templates/admin_user_edit.html:63 +#: surveybox/web/templates/admin_user_edit.html:76 msgid "Data of this user" msgstr "" -#: surveybox/web/templates/admin_user_edit.html:64 +#: surveybox/web/templates/admin_user_edit.html:77 msgid "" "For a request for access: everything stored about this person, as a JSON " "file. The export is recorded in the log." msgstr "" -#: surveybox/web/templates/admin_user_edit.html:67 +#: surveybox/web/templates/admin_user_edit.html:80 msgid "Export data" msgstr "" -#: surveybox/web/templates/admin_user_edit.html:73 +#: surveybox/web/templates/admin_user_edit.html:86 msgid "" "Deletes the account with all its data. You see beforehand exactly what " "will be deleted and decide what happens to the forms of this user." msgstr "" -#: surveybox/web/templates/admin_user_edit.html:74 +#: surveybox/web/templates/admin_user_edit.html:87 msgid "Delete user …" msgstr "" @@ -3796,34 +3858,22 @@ msgstr "" msgid "Search by name or email" msgstr "" -#: surveybox/web/templates/admin_users.html:29 -msgid "deactivated" -msgstr "" - -#: surveybox/web/templates/admin_users.html:30 -msgid "invited" -msgstr "" - -#: surveybox/web/templates/admin_users.html:31 -msgid "active" -msgstr "" - -#: surveybox/web/templates/admin_users.html:37 +#: surveybox/web/templates/admin_users.html:38 msgid "No users found." msgstr "" -#: surveybox/web/templates/admin_users.html:43 +#: surveybox/web/templates/admin_users.html:44 msgid "Export the data of a guest" msgstr "" -#: surveybox/web/templates/admin_users.html:44 +#: surveybox/web/templates/admin_users.html:45 msgid "" "For a request for access by someone who took part as a guest: everything " "stored for this email address, as a JSON file. The export is recorded in " "the log." msgstr "" -#: surveybox/web/templates/admin_users.html:49 +#: surveybox/web/templates/admin_users.html:50 #: surveybox/web/templates/form_responses.html:152 msgid "Export" msgstr "" diff --git a/surveybox/models/accounts.py b/surveybox/models/accounts.py index a2290e5..a6a6c98 100644 --- a/surveybox/models/accounts.py +++ b/surveybox/models/accounts.py @@ -4,7 +4,7 @@ from __future__ import annotations import uuid -from datetime import datetime +from datetime import UTC, datetime from enum import StrEnum from typing import Any @@ -113,6 +113,11 @@ class UserAccount(Base): def has_second_factor(self) -> bool: return self.has_totp or self.has_passkey + @property + def is_locked(self) -> bool: + """Nach zu vielen Fehlversuchen vorübergehend gesperrt (siehe accounts.service).""" + return self.locked_until is not None and self.locked_until > datetime.now(UTC) + @property def is_admin(self) -> bool: return self.role == Role.ADMIN diff --git a/surveybox/web/routes/admin.py b/surveybox/web/routes/admin.py index 149a659..5661985 100644 --- a/surveybox/web/routes/admin.py +++ b/surveybox/web/routes/admin.py @@ -94,6 +94,7 @@ ACTION_NAMES: dict[str, str] = { "form.history_restored": N_("Changes restored from the form history"), "form.trash_restored": N_("Deleted form restored"), "admin.history_changed": N_("Form history settings changed"), + "admin.user_unlocked": N_("Lock of an account lifted"), "account.deleted": N_("Account deleted"), "account.exported": N_("Own data downloaded"), "admin.user_exported": N_("Data of a user exported"), @@ -221,6 +222,9 @@ def _edit_page(request: Request, db: Db, target: UserAccount, **extra: Any) -> R "target": target, "role_names": ROLE_NAMES, "groups": groups.groups_of(db, target), + "lock_attempts": service.LOCK_AFTER_FAILURES, + "lock_since": (target.locked_until - service.LOCK_DURATION + if target.is_locked and target.locked_until else None), **extra, }, status_code=status_code, @@ -401,6 +405,18 @@ def create_user_link( ) +@router.post("/users/{public_id}/unlock") +def unlock_user(db: Db, auth: AdminUser, public_id: str) -> Response: + """Vorübergehende Sperre nach Fehlversuchen aufheben.""" + target = _load_user(db, public_id) + if target is None: + return RedirectResponse("/admin/users", status_code=303) + service.unlock(db, auth.user, target) + db.commit() + return RedirectResponse(f"/admin/users/{target.public_id}?notice=user_unlocked", + status_code=303) + + @router.post("/users/{public_id}/sessions") def revoke_user_sessions(db: Db, auth: AdminUser, public_id: str) -> Response: target = _load_user(db, public_id) diff --git a/surveybox/web/templates/admin_user_edit.html b/surveybox/web/templates/admin_user_edit.html index 4da27e0..10a1fee 100644 --- a/surveybox/web/templates/admin_user_edit.html +++ b/surveybox/web/templates/admin_user_edit.html @@ -7,6 +7,17 @@

{{ target.display_name }}

{{ target.email }}

{% if error %}{% endif %} + {% if target.is_locked %} +
+

{{ _('Temporarily locked') }}

+

{{ _('Reason: %(num)d failed sign-in attempts in a row.', num=lock_attempts) }} + {{ _('Locked since %(since)s, until %(until)s. Until then, sign-in fails even with the right password.', since=lock_since | datetime, until=target.locked_until | datetime) }}

+
+ {{ csrf_field(request) }} +
+

{{ _('If the attempts did not come from this person, reset the password instead and check the log.') }}

+
+ {% endif %}
{{ csrf_field(request) }} @@ -23,6 +34,8 @@
{{ _('Created') }}
{{ target.created_at | datetime }}
{{ _('Last sign-in') }}
{{ target.last_login_at | datetime }}
+
{{ _('Status') }}
+
{% if not target.is_active %}{{ _('deactivated') }}{% elif target.is_locked %}{{ _('temporarily locked') }}{% elif not target.password_hash %}{{ _('invited') }}{% else %}{{ _('active') }}{% endif %}{% if target.failed_logins and not target.is_locked %} · {{ ngettext('%(num)d failed sign-in attempt since the last success', '%(num)d failed sign-in attempts since the last success', target.failed_logins) }}{% endif %}
{{ _('Two-factor authentication') }}
{% if target.has_totp %}{{ _('Authenticator app') }}{% endif %} diff --git a/surveybox/web/templates/admin_users.html b/surveybox/web/templates/admin_users.html index 4384cb4..92ff7b4 100644 --- a/surveybox/web/templates/admin_users.html +++ b/surveybox/web/templates/admin_users.html @@ -27,6 +27,7 @@ {{ _(role_names[u.role]) }} {% if not u.is_active %}{{ _('deactivated') }} + {% elif u.is_locked %}{{ _('temporarily locked') }} {% elif not u.password_hash %}{{ _('invited') }} {% else %}{{ _('active') }}{% endif %} {% if u.has_second_factor %}2FA{% endif %} diff --git a/surveybox/web/templating.py b/surveybox/web/templating.py index c295661..9306bf7 100644 --- a/surveybox/web/templating.py +++ b/surveybox/web/templating.py @@ -38,6 +38,7 @@ NOTICES: dict[str, str] = { "response_deleted": N_("The answer has been deleted."), "response_edited": N_("The answer has been changed and the note saved."), "user_sessions_revoked": N_("All sessions of this user have been signed out."), + "user_unlocked": N_("The lock has been lifted. The user can sign in again."), "passkey_added": N_("The passkey has been added."), "passkey_removed": N_("The passkey has been removed."), "group_saved": N_("The group has been saved."), diff --git a/tests/test_accounts.py b/tests/test_accounts.py index 16a7351..d6a0d5f 100644 --- a/tests/test_accounts.py +++ b/tests/test_accounts.py @@ -64,7 +64,29 @@ def test_wrong_password_and_lockout(client: TestClient) -> None: assert resp.status_code == 400 with Session(db.get_engine()) as s: user = s.scalar(select(UserAccount).where(UserAccount.email == "anna@example.de")) - assert user is not None and user.locked_until is not None + assert user is not None and user.locked_until is not None and user.is_locked + + +def test_admin_sees_and_lifts_lock(app) -> None: # type: ignore[no-untyped-def] + from test_mail import admin + + target, _ = make_user(Role.PARTICIPANT, "paula@example.de") + guest = new_client(app) + for _ in range(5): + assert login(guest, "paula@example.de", "falsches passwort!!").status_code == 400 + root = admin(app) + listing = root.get("/admin/users").text + assert "temporarily locked" in listing + page = root.get(f"/admin/users/{target.public_id}").text + assert "5 failed sign-in attempts in a row" in page and "Lift lock" in page + resp = root.post(f"/admin/users/{target.public_id}/unlock", + data={"csrf_token": csrf_from(page)}) + assert resp.status_code == 303 and "user_unlocked" in resp.headers["location"] + assert "Lift lock" not in root.get(f"/admin/users/{target.public_id}").text + assert login(guest, "paula@example.de").status_code == 303 + with Session(db.get_engine()) as s: + actions = list(s.scalars(select(AuditEvent.action))) + assert "auth.locked" in actions and "admin.user_unlocked" in actions def test_unknown_user_gets_same_answer(client: TestClient) -> None: