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