[ADD] auth_password_policy

Hard-checks password length according to configured rule (default: at
least 8 characters).

Closes #22351
This commit is contained in:
Xavier Morel
2018-10-02 20:53:47 +02:00
committed by Olivier Dony
parent 22f037e3af
commit efb516e045
6 changed files with 81 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
from . import models
@@ -0,0 +1,6 @@
{
'name': "Password Policy",
"summary": "Implements basic password policy configuration & check",
'depends': ['base_setup', 'auth_crypt'],
'data': ['views/res_config_settings_views.xml']
}
@@ -0,0 +1,2 @@
from . import res_config_settings
from . import res_users
@@ -0,0 +1,27 @@
from odoo import api, fields, models, _
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
minlength = fields.Integer("Minimum Length", help="Minimum number of characters passwords must contain.")
@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)
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
from odoo import api, models, _
from odoo.exceptions import UserError
class ResUsers(models.Model):
_inherit = 'res.users'
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))
@@ -0,0 +1,19 @@
<odoo>
<record id="res_config_settings_view_form" model="ir.ui.view">
<field name="name">res.config.settings.form.auth_password_policy</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="base_setup.res_config_settings_view_form"/>
<field name="arch" type="xml">
<!-- add before the Access Rights section -->
<xpath expr="//div[@id='access_rights']" position="before">
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_left_pane"/>
<div class="o_setting_right_pane">
<label for="minlength"/>
<field name="minlength"/>
</div>
</div>
</xpath>
</field>
</record>
</odoo>