browsertrix/backend/btrixcloud/migrations/migration_0035_fix_failed_logins.py
Ilya Kreymer 5f53db75ee
fix resetting of invalid logins: (#2002)
* Fixes issue in FailedLogin model:
- fix data-model to remove nested 'attempted.attempted'
- migrate existing data to remove nested field

* Also, avoid setting dt_now() in model as that results in fixed date for
all objects:
- update FailedLogin to update 'attempted' date on every attempt
- also update PageNote object to set date in constructor

* Update text for too many logins to make it clear it is set only if its a
valid email

* fixes #2001
2024-08-07 12:36:06 -07:00

38 lines
1010 B
Python

"""
Migration 0035 -- fix model for failed logins
"""
from btrixcloud.migrations import BaseMigration
MIGRATION_VERSION = "0035"
class Migration(BaseMigration):
"""Migration class."""
# pylint: disable=unused-argument
def __init__(self, mdb, **kwargs):
super().__init__(mdb, migration_version=MIGRATION_VERSION)
async def migrate_up(self):
"""Perform migration up.
Set created from attempted.attempted
"""
failed_logins = self.mdb["logins"]
try:
res = await failed_logins.update_many(
{"attempted.attempted": {"$exists": 1}},
[{"$set": {"attempted": "$attempted.attempted"}}],
)
updated = res.modified_count
print(f"{updated} failed logins fixed", flush=True)
# pylint: disable=broad-exception-caught
except Exception as err:
print(
f"Error fixing failed logins: {err}",
flush=True,
)