diff options
author | Jo-Philipp Wich <jo@mein.io> | 2019-09-16 07:54:25 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2019-09-16 07:54:25 +0200 |
commit | 9ae9657a8585b794bc93b993d83e64b6d7f65b92 (patch) | |
tree | 123624bc665e5a72291e07663399962ae46fd5bb /modules/luci-mod-system/htdocs | |
parent | a31d1d10e09183a999ab9736e0625415bd87fe25 (diff) |
luci-mod-system: remplement password change as client side view
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-mod-system/htdocs')
-rw-r--r-- | modules/luci-mod-system/htdocs/luci-static/resources/view/system/password.js | 123 |
1 files changed, 93 insertions, 30 deletions
diff --git a/modules/luci-mod-system/htdocs/luci-static/resources/view/system/password.js b/modules/luci-mod-system/htdocs/luci-static/resources/view/system/password.js index 7a79d7e2da..6c5ffa1b26 100644 --- a/modules/luci-mod-system/htdocs/luci-static/resources/view/system/password.js +++ b/modules/luci-mod-system/htdocs/luci-static/resources/view/system/password.js @@ -1,31 +1,94 @@ -function submitPassword(ev) { - var pw1 = document.body.querySelector('[name="pw1"]'), - pw2 = document.body.querySelector('[name="pw2"]'); - - if (!pw1.value.length || !pw2.value.length) - return; - - if (pw1.value === pw2.value) { - L.showModal(_('Change login password'), - E('p', { class: 'spinning' }, _('Changing password…'))); - - L.post('admin/system/admin/password/json', { password: pw1.value }, - function() { - showModal(_('Change login password'), [ - E('div', _('The system password has been successfully changed.')), - E('div', { 'class': 'right' }, - E('div', { class: 'btn', click: L.hideModal }, _('Dismiss'))) - ]); - - pw1.value = pw2.value = ''; - }); - } - else { - L.showModal(_('Change login password'), [ - E('div', { class: 'alert-message warning' }, - _('Given password confirmation did not match, password not changed!')), - E('div', { 'class': 'right' }, - E('div', { class: 'btn', click: L.hideModal }, _('Dismiss'))) - ]); +'use strict'; +'require form'; +'require rpc'; + +var formData = { + password: { + pw1: null, + pw2: null } -} +}; + +var callSetPassword = rpc.declare({ + object: 'luci', + method: 'setPassword', + params: [ 'username', 'password' ], + expect: { result: false } +}); + +return L.view.extend({ + checkPassword: function(section_id, value) { + var strength = document.querySelector('.cbi-value-description'), + strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g"), + mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g"), + enoughRegex = new RegExp("(?=.{6,}).*", "g"); + + if (strength && value.length) { + if (false == enoughRegex.test(value)) + strength.innerHTML = '%s: <span style="color:red">%s</span>'.format(_('Password strength'), _('More Characters')); + else if (strongRegex.test(value)) + strength.innerHTML = '%s: <span style="color:green">%s</span>'.format(_('Password strength'), _('Strong')); + else if (mediumRegex.test(value)) + strength.innerHTML = '%s: <span style="color:orange">%s</span>'.format(_('Password strength'), _('Medium')); + else + strength.innerHTML = '%s: <span style="color:red">%s</span>'.format(_('Password strength'), _('Weak')); + } + + return true; + }, + + render: function() { + var m, s, o; + + m = new form.JSONMap(formData, _('Router Password'), _('Changes the administrator password for accessing the device')); + s = m.section(form.NamedSection, 'password', 'password'); + + o = s.option(form.Value, 'pw1', _('Password')); + o.password = true; + o.validate = this.checkPassword; + + o = s.option(form.Value, 'pw2', _('Confirmation'), ' '); + o.password = true; + o.renderWidget = function(/* ... */) { + var node = form.Value.prototype.renderWidget.apply(this, arguments); + + node.childNodes[1].addEventListener('keydown', function(ev) { + if (ev.keyCode == 13 && !ev.currentTarget.classList.contains('cbi-input-invalid')) + document.querySelector('.cbi-button-save').click(); + }); + + return node; + }; + + return m.render(); + }, + + handleSave: function() { + var map = document.querySelector('.cbi-map'); + + return L.dom.callClassMethod(map, 'save').then(function() { + if (formData.password.pw1 == null || formData.password.pw1.length == 0) + return; + + if (formData.password.pw1 != formData.password.pw2) { + L.ui.addNotification(null, E('p', _('Given password confirmation did not match, password not changed!')), 'danger'); + return; + } + + return callSetPassword('root', formData.password.pw1).then(function(success) { + if (success) + L.ui.addNotification(null, E('p', _('The system password has been successfully changed.')), 'info'); + else + L.ui.addNotification(null, E('p', _('Failed to change the system password.')), 'danger'); + + formData.password.pw1 = null; + formData.password.pw2 = null; + + L.dom.callClassMethod(map, 'render'); + }); + }); + }, + + handleSaveApply: null, + handleReset: null +}); |