Skip to content

Commit d6cf660

Browse files
committed
Merge PR #762 into 16.0
Signed-off-by pedrobaeza
2 parents d7ebb65 + 1152250 commit d6cf660

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

password_security/models/res_users.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from odoo import _, api, fields, models
1010
from odoo.exceptions import UserError, ValidationError
11+
from odoo.tools import groupby
1112

1213

1314
def delta_now(**kwargs):
@@ -97,22 +98,21 @@ def _check_password(self, password):
9798
return True
9899

99100
def _check_password_rules(self, password):
100-
self.ensure_one()
101101
if not password:
102102
return True
103-
company_id = self.company_id
104103
params = self.env["ir.config_parameter"].sudo()
105104
minlength = params.get_param("auth_password_policy.minlength", default=0)
106-
password_regex = [
107-
"^",
108-
"(?=.*?[a-z]){" + str(company_id.password_lower) + ",}",
109-
"(?=.*?[A-Z]){" + str(company_id.password_upper) + ",}",
110-
"(?=.*?\\d){" + str(company_id.password_numeric) + ",}",
111-
r"(?=.*?[\W_]){" + str(company_id.password_special) + ",}",
112-
".{%d,}$" % int(minlength),
113-
]
114-
if not re.search("".join(password_regex), password):
115-
raise ValidationError(self.password_match_message())
105+
for company_id, users in groupby(self, lambda u: u.company_id):
106+
password_regex = [
107+
"^",
108+
"(?=.*?[a-z]){" + str(company_id.password_lower) + ",}",
109+
"(?=.*?[A-Z]){" + str(company_id.password_upper) + ",}",
110+
"(?=.*?\\d){" + str(company_id.password_numeric) + ",}",
111+
r"(?=.*?[\W_]){" + str(company_id.password_special) + ",}",
112+
".{%d,}$" % int(minlength),
113+
]
114+
if not re.search("".join(password_regex), password):
115+
raise ValidationError(users[0].password_match_message())
116116

117117
return True
118118

password_security/tests/test_change_password.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright 2023 Onestein (<https://www.onestein.eu>)
22
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
3-
3+
import re
44
from unittest import mock
55

66
from odoo import http
@@ -134,3 +134,14 @@ def test_04_change_password_check_password_history(self):
134134
# Log in with new password: ensure we end up on the right page
135135
res_login2 = self.login("admin", "!asdQWE12345_4")
136136
self.assertEqual(res_login2.request.path_url, "/web")
137+
138+
def test_20_write_password(self):
139+
"""Detects expected singleton errors writing passwords for more than one user"""
140+
users = self.env["res.users"].search([], limit=2)
141+
self.assertEqual(len(users), 2)
142+
res = users.write({"password": "!asdQWE12345"})
143+
self.assertTrue(res)
144+
145+
msg = re.escape(users[0].password_match_message())
146+
with self.assertRaisesRegex(ValidationError, msg):
147+
users.write({"password": "12345678"})

0 commit comments

Comments
 (0)