Skip to content

Commit

Permalink
fixup! [IMP] password_security: add flag to enable/disable password s…
Browse files Browse the repository at this point in the history
…ecurity policy
  • Loading branch information
ajaniszewska-dev committed Mar 20, 2023
1 parent ae570f2 commit a3c10d2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
2 changes: 1 addition & 1 deletion password_security/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ResCompany(models.Model):
_inherit = "res.company"

password_policy_enabled = fields.Boolean(default=False)

password_expiration = fields.Integer(
"Days",
default=60,
Expand Down
52 changes: 27 additions & 25 deletions password_security/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def create(self, vals):
return super(ResUsers, self).create(vals)

def write(self, vals):
if vals.get("password"):
if vals.get("password") and self.env.user.company_id.password_policy_enabled:
self._check_password(vals["password"])
vals["password_write_date"] = fields.Datetime.now()
return super(ResUsers, self).write(vals)
Expand All @@ -55,16 +55,17 @@ def write(self, vals):
def get_password_policy(self):
data = super(ResUsers, self).get_password_policy()
company_id = self.env.user.company_id
data.update(
{
"password_lower": company_id.password_lower,
"password_upper": company_id.password_upper,
"password_numeric": company_id.password_numeric,
"password_special": company_id.password_special,
"password_length": company_id.password_length,
"password_estimate": company_id.password_estimate,
}
)
if company_id.password_policy_enabled:
data.update(
{
"password_lower": company_id.password_lower,
"password_upper": company_id.password_upper,
"password_numeric": company_id.password_numeric,
"password_special": company_id.password_special,
"password_length": company_id.password_length,
"password_estimate": company_id.password_estimate,
}
)
return data

def _check_password_policy(self, passwords):
Expand Down Expand Up @@ -124,20 +125,21 @@ def _check_password_rules(self, password):
if not password:
return True
company_id = self.company_id
password_regex = [
"^",
"(?=.*?[a-z]){" + str(company_id.password_lower) + ",}",
"(?=.*?[A-Z]){" + str(company_id.password_upper) + ",}",
"(?=.*?\\d){" + str(company_id.password_numeric) + ",}",
r"(?=.*?[\W_]){" + str(company_id.password_special) + ",}",
".{%d,}$" % int(company_id.password_length),
]
if not re.search("".join(password_regex), password):
raise ValidationError(self.password_match_message())

estimation = self.get_estimation(password)
if estimation["score"] < company_id.password_estimate:
raise UserError(estimation["feedback"]["warning"])
if company_id.password_policy_enabled:
password_regex = [
"^",
"(?=.*?[a-z]){" + str(company_id.password_lower) + ",}",
"(?=.*?[A-Z]){" + str(company_id.password_upper) + ",}",
"(?=.*?\\d){" + str(company_id.password_numeric) + ",}",
r"(?=.*?[\W_]){" + str(company_id.password_special) + ",}",
".{%d,}$" % int(company_id.password_length),
]
if not re.search("".join(password_regex), password):
raise ValidationError(self.password_match_message())

estimation = self.get_estimation(password)
if estimation["score"] < company_id.password_estimate:
raise UserError(estimation["feedback"]["warning"])

return True

Expand Down
1 change: 1 addition & 0 deletions password_security/tests/test_res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def setUpClass(cls):
}
cls.password = "asdQWE123$%^"
cls.main_comp = cls.env.ref("base.main_company")
cls.main_comp.password_policy_enabled = True
cls.vals = {
"name": "User",
"login": cls.login,
Expand Down
10 changes: 2 additions & 8 deletions password_security/views/res_config_settings_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@
<div class="mt16">
<span>
Password policy enabled
<field
name="password_policy_enabled"
class="oe_inline"
/>
<field name="password_policy_enabled" class="oe_inline"/>
</span>
</div>
</div>
<div
class="content-group"
attrs="{'invisible': [('password_policy_enabled','=', False)]}"
>
<div class="content-group" attrs="{'invisible': [('password_policy_enabled','=', False)]}">
<div class="mt16">
<span>
Password expires in
Expand Down

0 comments on commit a3c10d2

Please sign in to comment.