diff --git a/password_security/__init__.py b/password_security/__init__.py index df8f34c6c9..72e0d80358 100644 --- a/password_security/__init__.py +++ b/password_security/__init__.py @@ -2,3 +2,4 @@ # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). from . import controllers, models +from .hooks import post_init_hook diff --git a/password_security/__manifest__.py b/password_security/__manifest__.py index c83ca630f8..7880266b6c 100644 --- a/password_security/__manifest__.py +++ b/password_security/__manifest__.py @@ -5,7 +5,7 @@ { "name": "Password Security", "summary": "Allow admin to set password security requirements.", - "version": "16.0.1.0.1", + "version": "16.0.1.0.2", "author": "LasLabs, " "Onestein, " "Kaushal Prajapati, " @@ -29,4 +29,5 @@ "demo/res_users.xml", ], "installable": True, + "post_init_hook": "post_init_hook", } diff --git a/password_security/hooks.py b/password_security/hooks.py new file mode 100644 index 0000000000..b24083bb31 --- /dev/null +++ b/password_security/hooks.py @@ -0,0 +1,16 @@ +# Copyright 2024 Vauxoo +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + + +def post_init_hook(cr, registry): + # Set password date for already existing users + cr.execute( + """ + UPDATE + res_users + SET + password_write_date = NOW() at time zone 'UTC' + WHERE + password_write_date IS NULL; + """ + ) diff --git a/password_security/migrations/16.0.1.0.2/post-migration.py b/password_security/migrations/16.0.1.0.2/post-migration.py new file mode 100644 index 0000000000..6f5776854a --- /dev/null +++ b/password_security/migrations/16.0.1.0.2/post-migration.py @@ -0,0 +1,12 @@ +def migrate(cr, version): + # Set password date for already existing users + cr.execute( + """ + UPDATE + res_users + SET + password_write_date = NOW() at time zone 'UTC' + WHERE + password_write_date IS NULL; + """ + ) diff --git a/password_security/models/res_users.py b/password_security/models/res_users.py index cc78c1a9d9..79c74418f2 100644 --- a/password_security/models/res_users.py +++ b/password_security/models/res_users.py @@ -144,10 +144,7 @@ def _validate_pass_reset(self): if pass_min <= 0: continue write_date = user.password_write_date - if not write_date: - continue - delta = timedelta(hours=pass_min) - if write_date + delta > datetime.now(): + if write_date and write_date + timedelta(hours=pass_min) > datetime.now(): raise UserError( _( "Passwords can only be reset every %d hour(s). "