Skip to content

Commit 3ccf330

Browse files
ajaniszewska-devsebalix
authored andcommitted
[IMP] password_security: add flag to enable/disable password security policy
1 parent 840110e commit 3ccf330

File tree

6 files changed

+43
-8
lines changed

6 files changed

+43
-8
lines changed

password_security/models/res_company.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
class ResCompany(models.Model):
1010
_inherit = "res.company"
1111

12+
password_policy_enabled = fields.Boolean(default=False)
13+
1214
password_expiration = fields.Integer(
1315
"Days",
1416
default=60,

password_security/models/res_config_settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
class ResConfigSettings(models.TransientModel):
77
_inherit = "res.config.settings"
88

9+
password_policy_enabled = fields.Boolean(
10+
related="company_id.password_policy_enabled", readonly=False
11+
)
912
password_expiration = fields.Integer(
1013
related="company_id.password_expiration", readonly=False
1114
)

password_security/models/res_users.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def write(self, vals):
5555
def get_password_policy(self):
5656
data = super(ResUsers, self).get_password_policy()
5757
company_id = self.env.user.company_id
58+
if not company_id.password_policy_enabled:
59+
return data
5860
data.update(
5961
{
6062
"password_lower": company_id.password_lower,
@@ -69,7 +71,8 @@ def get_password_policy(self):
6971

7072
def _check_password_policy(self, passwords):
7173
result = super(ResUsers, self)._check_password_policy(passwords)
72-
74+
if not self.env.user.company_id.password_policy_enabled:
75+
return result
7376
for password in passwords:
7477
if not password:
7578
continue
@@ -115,6 +118,8 @@ def password_match_message(self):
115118
return "\r".join(message)
116119

117120
def _check_password(self, password):
121+
if not self.env.user.company_id.password_policy_enabled:
122+
return True
118123
self._check_password_rules(password)
119124
self._check_password_history(password)
120125
return True
@@ -143,6 +148,8 @@ def _check_password_rules(self, password):
143148

144149
def _password_has_expired(self):
145150
self.ensure_one()
151+
if not self.company_id.password_policy_enabled:
152+
return False
146153
if not self.password_write_date:
147154
return True
148155

@@ -165,6 +172,8 @@ def _validate_pass_reset(self):
165172
:return: True on allowed reset
166173
"""
167174
for user in self:
175+
if not user.company_id.password_policy_enabled:
176+
continue
168177
pass_min = user.company_id.password_minimum
169178
if pass_min <= 0:
170179
continue
@@ -204,7 +213,8 @@ def _check_password_history(self, password):
204213
def _set_encrypted_password(self, uid, pw):
205214
"""It saves password crypt history for history rules"""
206215
res = super(ResUsers, self)._set_encrypted_password(uid, pw)
207-
216+
if not self.env.user.company_id.password_policy_enabled:
217+
return res
208218
self.write({"password_history_ids": [(0, 0, {"password_crypt": pw})]})
209219
return res
210220

password_security/tests/test_password_security_home.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def __init__(self):
3333
class TestPasswordSecurityHome(TransactionCase):
3434
def setUp(self):
3535
super(TestPasswordSecurityHome, self).setUp()
36+
self.main_comp = self.env.ref("base.main_company")
37+
self.main_comp.password_policy_enabled = True
3638
self.PasswordSecurityHome = main.PasswordSecurityHome
3739
self.password_security_home = self.PasswordSecurityHome()
3840
self.passwd = "I am a password!"
@@ -175,6 +177,11 @@ def test_web_auth_signup_invalid_render(self):
175177

176178
@mock.patch("odoo.http.WebRequest.validate_csrf", return_value=True)
177179
class LoginCase(HttpCase):
180+
def setUp(self):
181+
super(LoginCase, self).setUp()
182+
self.main_comp = self.env.ref("base.main_company")
183+
self.main_comp.password_policy_enabled = True
184+
178185
def test_web_login_authenticate(self, *args):
179186
"""It should allow authenticating by login"""
180187
response = self.url_open(

password_security/tests/test_res_users.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
33

44
from odoo.exceptions import UserError
5-
from odoo.tests.common import SavepointCase
5+
from odoo.tests.common import TransactionCase
66

77

8-
class TestResUsers(SavepointCase):
8+
class TestResUsers(TransactionCase):
99
@classmethod
1010
def setUpClass(cls):
1111
super(TestResUsers, cls).setUpClass()
@@ -17,6 +17,7 @@ def setUpClass(cls):
1717
}
1818
cls.password = "asdQWE123$%^"
1919
cls.main_comp = cls.env.ref("base.main_company")
20+
cls.main_comp.password_policy_enabled = True
2021
cls.vals = {
2122
"name": "User",
2223
"login": cls.login,

password_security/views/res_config_settings_views.xml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,23 @@
1818
</xpath>
1919
<!-- Add our password policy fields -->
2020
<xpath expr="//div[@id='access_rights']" position="before">
21-
<div class="col-12 col-lg-6 o_setting_box" title="Password Policy">
22-
<div class="o_setting_left_pane" />
21+
<div
22+
class="col-12 col-lg-6 o_setting_box"
23+
id="password_policy"
24+
title="Password Policy"
25+
>
26+
<div class="o_setting_left_pane">
27+
<field name="password_policy_enabled" />
28+
</div>
2329
<div class="o_setting_right_pane">
24-
<label string="Password Policy" for="password_expiration" />
25-
<div class="content-group">
30+
<label string="Password Policy" for="password_policy_enabled" />
31+
<div class="text-muted">
32+
Set password security requirements
33+
</div>
34+
<div
35+
class="content-group"
36+
attrs="{'invisible': [('password_policy_enabled','=', False)]}"
37+
>
2638
<div class="mt16">
2739
<span>
2840
Password expires in

0 commit comments

Comments
 (0)