From 5bbbd25edf129912ed7cdccd1005ab61bfaf2124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Tue, 9 Oct 2018 13:38:29 +0000 Subject: [PATCH] [FIX] auth_password_policy: Cannot read property 'decorations' of undefined Revision on https://github.com/odoo/odoo/commit/a47f6093f80cefa51c5a1bc1301db01007f97761 The commit above introduced a visual 'password strength' next to the password wizard in Settings > Users, when the module `auth_password_policy` is installed. However, there was an traceback when trying to access the 'General Settings' when some specific apps and modules were installed. This issue comes from the small design issue with the new PasswordField: it was designed as a field object that was instantiated and returned in the `init` function of an InputField with the password attribute set. This is not a solid design, because the documentation of the `init` function does not specify that it should return the newly instantiated object. As a result, the implementation was very fragile and only worked on FieldChar but not FieldText. This commit fixes the issue by turning the FieldPassword a field widget. As a result, the enabling of the password meter next to a password input has changed: Before this commit: ``` ``` With this commit: ``` ``` Closes #27440 closes odoo/odoo#27586 --- .../static/src/js/password_field.js | 21 ++++--------------- .../auth_password_policy/views/res_users.xml | 2 +- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/addons/auth_password_policy/static/src/js/password_field.js b/addons/auth_password_policy/static/src/js/password_field.js index 93a623acc1c..fa8cfd20ff2 100644 --- a/addons/auth_password_policy/static/src/js/password_field.js +++ b/addons/auth_password_policy/static/src/js/password_field.js @@ -7,6 +7,7 @@ odoo.define('auth_password_policy.PasswordField', function (require) { "use strict"; var fields = require('web.basic_fields'); +var registry = require('web.field_registry'); var policy = require('auth_password_policy'); var Meter = require('auth_password_policy.Meter'); var _formatValue = require('web.AbstractField').prototype._formatValue; @@ -21,12 +22,12 @@ var PasswordField = fields.InputField.extend({ }, willStart: function () { var _this = this; - var getPolicy = this.nodeOptions['password_meter'] ? this._rpc({ + var getPolicy = this._rpc({ model: 'res.users', method: 'get_password_policy', }).then(function (p) { _this._meter = new Meter(_this, new policy.Policy(p), policy.recommendations); - }) : undefined; + }); return $.when( this._super.apply(this, arguments), getPolicy @@ -46,9 +47,6 @@ var PasswordField = fields.InputField.extend({ // insertAfter doesn't work and appendTo means the meter is // ignored (as this.$el is an input[type=password]) _this.$el = t.add(meter.$el); - if (!_this.nodeOptions['password_meter']) { - meter.$el.hide(); - } }, _this.$el); }).then(function () { // initial meter update when re-editing @@ -84,17 +82,6 @@ var PasswordField = fields.InputField.extend({ } }); -fields.InputField.include({ - init: function (parent, name, record, options) { - var fieldsInfo = record.fieldsInfo[options.viewType]; - var attrs = options.attrs || (fieldsInfo && fieldsInfo[name]) || {}; - if ('password' in attrs - && Object.getPrototypeOf(this) !== PasswordField.prototype) { - return new PasswordField(parent, name, record, options); - } - this._super.apply(this, arguments); - } -}); - +registry.add("password_meter", PasswordField); return PasswordField; }); diff --git a/addons/auth_password_policy/views/res_users.xml b/addons/auth_password_policy/views/res_users.xml index 7bc4dc91103..e0a2ffc3acc 100644 --- a/addons/auth_password_policy/views/res_users.xml +++ b/addons/auth_password_policy/views/res_users.xml @@ -5,7 +5,7 @@ change.password.user - {'password_meter': True} + password_meter