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