summaryrefslogtreecommitdiff
path: root/addons/auth_password_policy/models
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/auth_password_policy/models
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/auth_password_policy/models')
-rw-r--r--addons/auth_password_policy/models/__init__.py2
-rw-r--r--addons/auth_password_policy/models/res_config_settings.py27
-rw-r--r--addons/auth_password_policy/models/res_users.py33
3 files changed, 62 insertions, 0 deletions
diff --git a/addons/auth_password_policy/models/__init__.py b/addons/auth_password_policy/models/__init__.py
new file mode 100644
index 00000000..ef6d5092
--- /dev/null
+++ b/addons/auth_password_policy/models/__init__.py
@@ -0,0 +1,2 @@
+from . import res_config_settings
+from . import res_users
diff --git a/addons/auth_password_policy/models/res_config_settings.py b/addons/auth_password_policy/models/res_config_settings.py
new file mode 100644
index 00000000..bb1f52a2
--- /dev/null
+++ b/addons/auth_password_policy/models/res_config_settings.py
@@ -0,0 +1,27 @@
+from odoo import api, fields, models, _
+
+
+class ResConfigSettings(models.TransientModel):
+ _inherit = 'res.config.settings'
+
+ minlength = fields.Integer("Minimum Password Length", help="Minimum number of characters passwords must contain, set to 0 to disable.")
+
+ @api.model
+ def get_values(self):
+ res = super(ResConfigSettings, self).get_values()
+
+ res['minlength'] = int(self.env['ir.config_parameter'].sudo().get_param('auth_password_policy.minlength', default=0))
+
+ return res
+
+ @api.model
+ def set_values(self):
+ self.env['ir.config_parameter'].sudo().set_param('auth_password_policy.minlength', self.minlength)
+
+ super(ResConfigSettings, self).set_values()
+
+ @api.onchange('minlength')
+ def _on_change_mins(self):
+ """ Password lower bounds must be naturals
+ """
+ self.minlength = max(0, self.minlength or 0)
diff --git a/addons/auth_password_policy/models/res_users.py b/addons/auth_password_policy/models/res_users.py
new file mode 100644
index 00000000..b938acd5
--- /dev/null
+++ b/addons/auth_password_policy/models/res_users.py
@@ -0,0 +1,33 @@
+# -*- coding: utf-8 -*-
+from odoo import api, models, _
+from odoo.exceptions import UserError
+
+
+class ResUsers(models.Model):
+ _inherit = 'res.users'
+
+ @api.model
+ def get_password_policy(self):
+ params = self.env['ir.config_parameter'].sudo()
+ return {
+ 'minlength': int(params.get_param('auth_password_policy.minlength', default=0)),
+ }
+
+ def _set_password(self):
+ self._check_password_policy(self.mapped('password'))
+
+ super(ResUsers, self)._set_password()
+
+ def _check_password_policy(self, passwords):
+ failures = []
+ params = self.env['ir.config_parameter'].sudo()
+
+ minlength = int(params.get_param('auth_password_policy.minlength', default=0))
+ for password in passwords:
+ if not password:
+ continue
+ if len(password) < minlength:
+ failures.append(_(u"Passwords must have at least %d characters, got %d.") % (minlength, len(password)))
+
+ if failures:
+ raise UserError(u'\n\n '.join(failures))