Passed
Push — develop ( 0366aa...0ecac7 )
by Nikolay
05:05
created

sites/admin-cabinet/assets/js/src/Security/check-passwords.js   A

Complexity

Total Complexity 20
Complexity/F 1.82

Size

Lines of Code 93
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 20
eloc 66
c 0
b 0
f 0
dl 0
loc 93
rs 10
mnd 9
bc 9
fnc 11
bpm 0.8181
cpm 1.8181
noi 6
1
/*
2
 * MikoPBX - free phone system for small business
3
 * Copyright © 2017-2022 Alexey Portnov and Nikolay Beketov
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along with this program.
16
 * If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
const checkPasswordWorker = {
20
    initialize() {
21
        $(window).on('SecurityWarning', checkPasswordWorker.onWarning);
22
    },
23
    onWarning(event, data) {
24
        let needShow = false;
25
        $("#updatePasswordWindow div.miko-settings-container").hide();
26
        $.each(data.needUpdate, (key, value) => {
27
            $(`#updatePasswordWindow #${value}-container`).show();
28
            needShow = true;
29
        });
30
        if(needShow){
31
            $('#updatePasswordWindow #savePassword').on('click', checkPasswordWorker.cbOnClickSavePassword);
32
            let modalWindow = $('#updatePasswordWindow');
33
            modalWindow.on('keyup', () => {
34
                PasswordScore.checkPassStrength({
0 ignored issues
show
Bug introduced by
The variable PasswordScore seems to be never declared. If this is a global, consider adding a /** global: PasswordScore */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
35
                    pass: $(`#updatePasswordWindow #WebAdminPassword`).val(),
36
                    bar: $('.WebAdminPassword-score'),
37
                    section: modalWindow,
38
                });
39
                PasswordScore.checkPassStrength({
40
                    pass: $(`#updatePasswordWindow #SSHPassword`).val(),
41
                    bar: $('.SSHPassword'),
42
                    section: modalWindow,
43
                });
44
            });
45
            modalWindow.modal({ closable : false, }).modal('show')
46
        }
47
    },
48
49
    /**
50
     * Отправка формы обновления паролей SSH и Web.
51
     */
52
    cbOnClickSavePassword(){
53
        $('#updatePasswordWindowResult').hide();
54
        let errors = '';
55
        let params = {};
56
        $.each(['WebAdminPassword', 'SSHPassword'], (key, value) => {
57
            if(!$(`#updatePasswordWindow #${value}`).is(":visible")){
58
                return;
59
            }
60
            let pass 	= $(`#updatePasswordWindow #${value}`).val();
61
            let passRep 	= $(`#updatePasswordWindow #${value}Repeat`).val();
62
            if( pass !== passRep){
63
                errors+='<li>'+globalTranslate[`pass_Check${value}DontMatch`]+'</li>';
0 ignored issues
show
Bug introduced by
The variable globalTranslate seems to be never declared. If this is a global, consider adding a /** global: globalTranslate */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
64
            }else if(pass.trim() === ''){
65
                errors+='<li>'+globalTranslate[`pass_Check${value}Empty`]+'</li>';
66
            }else if(PasswordScore.scorePassword(pass) < 50){
0 ignored issues
show
Bug introduced by
The variable PasswordScore seems to be never declared. If this is a global, consider adding a /** global: PasswordScore */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
67
                errors+=`<li>${globalTranslate['pass_Check${value}Simple']}</li>`;
68
            }else{
69
                params[value] = pass;
70
            }
71
        });
72
        if(errors.trim() !== ''){
73
            errors = `<ul class="ui list">${errors}</ul>`;
74
            checkPasswordWorker.showPasswordError(globalTranslate['pass_CheckWebPassErrorChange'], errors);
0 ignored issues
show
Bug introduced by
The variable globalTranslate seems to be never declared. If this is a global, consider adding a /** global: globalTranslate */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
75
        }else{
76
            checkPasswordWorker.savePasswords(params);
77
        }
78
    },
79
    savePasswords(params){
80
        $.post('/admin-cabinet/general-settings/save', params, function( data ) {
81
            if(data.success === false){
82
                let errors = '';
83
                if(typeof data.passwordCheckFail !== 'undefined'){
84
                    $.each(data.passwordCheckFail, (key, value) => {
85
                        errors+='<li>'+globalTranslate[`pass_Check${value}Simple`]+'</li>';
0 ignored issues
show
Bug introduced by
The variable globalTranslate seems to be never declared. If this is a global, consider adding a /** global: globalTranslate */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
86
                    });
87
                }else{
88
                    errors+='<li>'+globalTranslate['er_InternalServerError']+'</li>';
0 ignored issues
show
Bug introduced by
The variable globalTranslate seems to be never declared. If this is a global, consider adding a /** global: globalTranslate */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
89
                }
90
                if(errors.trim() !== ''){
91
                    checkPasswordWorker.showPasswordError(globalTranslate['pass_CheckWebPassErrorChange'], errors);
92
                }
93
            }else{
94
                $('#updatePasswordWindow').modal({ closable : false, }).modal('hide')
95
                let event = document.createEvent('Event');
96
                event.initEvent('ConfigDataChanged', false, true);
97
                window.dispatchEvent(event);
98
            }
99
        });
100
    },
101
    showPasswordError(header, body){
102
        $('#updatePasswordWindowResult div').html(header);
103
        $('#updatePasswordWindowResult p').html(body);
104
        $('#updatePasswordWindowResult').show();
105
    },
106
};
107
108
109
$(document).ready(() => {
110
    checkPasswordWorker.initialize();
111
});