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 2d8775e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
56 changes: 30 additions & 26 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,25 +55,28 @@ 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):
result = super(ResUsers, self)._check_password_policy(passwords)
company_id = self.env.user.company_id

for password in passwords:
if not password:
continue
self._check_password(password)
if company_id.password_policy_enabled:
self._check_password(password)

return result

Expand Down Expand Up @@ -124,20 +127,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

0 comments on commit 2d8775e

Please sign in to comment.