treewide: require ui.js explicitly
[project/luci.git] / modules / luci-mod-system / htdocs / luci-static / resources / view / system / password.js
1 'use strict';
2 'require ui';
3 'require form';
4 'require rpc';
5
6 var formData = {
7 password: {
8 pw1: null,
9 pw2: null
10 }
11 };
12
13 var callSetPassword = rpc.declare({
14 object: 'luci',
15 method: 'setPassword',
16 params: [ 'username', 'password' ],
17 expect: { result: false }
18 });
19
20 return L.view.extend({
21 checkPassword: function(section_id, value) {
22 var strength = document.querySelector('.cbi-value-description'),
23 strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g"),
24 mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g"),
25 enoughRegex = new RegExp("(?=.{6,}).*", "g");
26
27 if (strength && value.length) {
28 if (false == enoughRegex.test(value))
29 strength.innerHTML = '%s: <span style="color:red">%s</span>'.format(_('Password strength'), _('More Characters'));
30 else if (strongRegex.test(value))
31 strength.innerHTML = '%s: <span style="color:green">%s</span>'.format(_('Password strength'), _('Strong'));
32 else if (mediumRegex.test(value))
33 strength.innerHTML = '%s: <span style="color:orange">%s</span>'.format(_('Password strength'), _('Medium'));
34 else
35 strength.innerHTML = '%s: <span style="color:red">%s</span>'.format(_('Password strength'), _('Weak'));
36 }
37
38 return true;
39 },
40
41 render: function() {
42 var m, s, o;
43
44 m = new form.JSONMap(formData, _('Router Password'), _('Changes the administrator password for accessing the device'));
45 s = m.section(form.NamedSection, 'password', 'password');
46
47 o = s.option(form.Value, 'pw1', _('Password'));
48 o.password = true;
49 o.validate = this.checkPassword;
50
51 o = s.option(form.Value, 'pw2', _('Confirmation'), ' ');
52 o.password = true;
53 o.renderWidget = function(/* ... */) {
54 var node = form.Value.prototype.renderWidget.apply(this, arguments);
55
56 node.childNodes[1].addEventListener('keydown', function(ev) {
57 if (ev.keyCode == 13 && !ev.currentTarget.classList.contains('cbi-input-invalid'))
58 document.querySelector('.cbi-button-save').click();
59 });
60
61 return node;
62 };
63
64 return m.render();
65 },
66
67 handleSave: function() {
68 var map = document.querySelector('.cbi-map');
69
70 return L.dom.callClassMethod(map, 'save').then(function() {
71 if (formData.password.pw1 == null || formData.password.pw1.length == 0)
72 return;
73
74 if (formData.password.pw1 != formData.password.pw2) {
75 ui.addNotification(null, E('p', _('Given password confirmation did not match, password not changed!')), 'danger');
76 return;
77 }
78
79 return callSetPassword('root', formData.password.pw1).then(function(success) {
80 if (success)
81 ui.addNotification(null, E('p', _('The system password has been successfully changed.')), 'info');
82 else
83 ui.addNotification(null, E('p', _('Failed to change the system password.')), 'danger');
84
85 formData.password.pw1 = null;
86 formData.password.pw2 = null;
87
88 L.dom.callClassMethod(map, 'render');
89 });
90 });
91 },
92
93 handleSaveApply: null,
94 handleReset: null
95 });