Files
2023-11-07 19:51:49 +06:00

600 lines
29 KiB
JavaScript

class TUser //Current user with role name and access
{
constructor() {
//this.name="TUser"; //User name
this.company_id = null;
this.win = null;
this.access = {}; //Associative array
this.onUpdate = null; //Listener update access array
this.timezone = ""; // +06:00:00 временая зона текущего пользователя
this.checkSession();
}
loadAccess() {
let xs = '<?xml version="1.0" encoding="utf-8"?>\n\
<metadata fn="4">\n\
<type n="SysAccessList" pp="-1">\n\
<objects-list><filter></filter></objects-list>\n\
</type>\n\
</metadata>';
var request = new TRequest(this);
if (request.callServer(ScriptName, xs)) {
//m_winPP.showProgressBar();
}
}
applyReq(req, fn, node) {
if (fn == -1) {
alert2(trt("Alert"),findFirstNode(node, '#cdata-section').nodeValue);
} else if (fn == 4) {
var nodeType = findFirstNode(node, "type");
if (nodeType != null) {
if (nodeType.getAttribute("n") == "_ChangePassword") {
if (findFirstNode(node, '#cdata-section').nodeValue == 't') {
alert2(trt("Alert"),trt('Password_changed_successfully'));
//this.win.hideProgressBar();
this.win.Close();
this.win = null;
} else {
alert2(trt("Alert"),trt('Failed_to_change_password') + '\n' + trt('Check_the_entered_data'));
this.win.hideProgressBar();
}
} else {
this.updateAccess(node);
}
}
} else if (fn == 7) {
var nCmd = findFirstNode(node, "cmd");
if (nCmd != null) {
if (findFirstNode(nCmd, '#cdata-section').nodeValue == "1") //Logout
{
location.reload();
} else if (findFirstNode(nCmd, '#cdata-section').nodeValue == "2") //Check if user not logged
{
if (findNode(node, '#cdata-section').nodeValue == "0") //if not logged
{
this.showLoginForm();
} else {
this.name = getCdataValue(findNode(node, 'name'));
this.company_id = getCdataValue(findNode(node, 'company_id'));
//alert2(trt("Alert"),'this.company_id = '+this.company_id);
configGUIbyAccessLevel();
//var shadow=document.getElementById("shadow");
//if(shadow.parentNode!=null) shadow.parentNode.removeChild(shadow);
g_trips.filtering();
g_geofences.filtering();
g_objects.filtering();
g_routes.filtering();
g_detours.filtering();
}
} else if (findFirstNode(nCmd, '#cdata-section').nodeValue == "3") //Login
{
if (findNode(node, '#cdata-section').nodeValue == "0") //if not logged
{
alert2(trt("Alert"),trt('Invalid_username_and_or_password'));
} else {
location.reload();
}
} else if (findFirstNode(nCmd, '#cdata-section').nodeValue == "4") //Register
{
if (findNode(node, '#cdata-section').nodeValue == "1") //if register
{
alert2(trt("Alert"),trt('New_user_is_registered') + '\n' + trt('The_password_has_been_sent_to_you_by_Email'));
location.reload();
}
}
}
} else {
alert2(trt("Alert"),"Unknown function! fn=\"" + fn + "\"");
}
if (this.win != null) this.win.hideProgressBar();
}
updateAccess(node) {
this.access = {};
var nodeType = findFirstNode(node, "type");
var nodeRecord = nodeType.firstChild;
while (nodeRecord != null) {
if (nodeRecord.nodeName == "record") {
var key = '';
var val = '';
i = 0; //column number
var cdataNode = nodeRecord.firstChild;
while (cdataNode != null) {
if (cdataNode.nodeName == "#cdata-section") {
if (i == 0) key = cdataNode.nodeValue;
if (i == 1) val = cdataNode.nodeValue;
i++;
}
cdataNode = cdataNode.nextSibling;
}
if (val == 'f' || val == 0 || val == 'false') val = false;
else if (val = 't' || val == 1 || val == 'true') val = true;
else val = null;
this.access[key] = val;
}
nodeRecord = nodeRecord.nextSibling;
}
if (this.onUpdate != null) this.onUpdate(this);
return true;
}
getAccess(name) {
var rez = this.access[name];
if (rez === undefined) {
alert2(trt("Alert"),'Access key "' + name + '" not found!');
return false;
}
if (rez != true) return false;
return rez;
};
changePassword() {
this.win = new TWin();
this.win.BuildGUI(10, 10);
this.win.setCaption(trt('Change_password'));
let str = '<div style="width: 100%; height: 100%; padding: 3px; text-align: left;">\n\
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%;">\n\
<tbody>\n\
<tr><td>\n\
<table border="0" style="width: 100%; height: 100%;">\n\
<tr>\n\
<td style="padding: 2px; width: 30%; white-space: nowrap"><b>' + trt('Login') + ' (email):</b></td>\n\
<td style="padding: 2px;"><input type="text" maxlength="50" style="width: 100%; padding: 2px; display: inline;" id="id_ch_login" name="login"><br></td>\n\
</tr>\n\
<tr>\n\
<td style="padding: 2px;"><b>' + trt('Old_password') + ':</b></td>\n\
<td style="padding: 2px;"><input type="password" maxlength="33" style="width: 100%; padding: 2px; display: inline;" id="id_ch_oldpassword" name="password"><br></td>\n\
</tr>\n\
<tr>\n\
<td style="padding: 2px;"><b>' + trt('New_password') + ':</b></td>\n\
<td style="padding: 2px;"><input type="password" maxlength="33" style="width: 100%; padding: 2px; display: inline;" id="id_ch_newpassword" name="password"><br></td>\n\
</tr>\n\
<tr>\n\
<td style="padding: 2px;"><b>' + trt('Repeat_password') + ':</b></td>\n\
<td style="padding: 2px;"><input type="password" maxlength="33" style="width: 100%; padding: 2px; display: inline;" id="id_ch_newrpassword" name="password"><br></td>\n\
</tr>\n\
<tr><td colspan="2" style="text-align: right;"><input class="button-secondary" id="id_change_pass" type="button" style="display: inline;" value="' + trt('Apply') + '"><input class="button-secondary" id="exit_pass_' + this.win.uid + '" type="button" style="display: inline;" value="' + trt('Cancel') + '"></td></tr>\n\
</table>\n\
</td></tr>\n\
</tbody>\n\
</table>\n\
</div>';
this.win.setContent(str);
this.win.setSize("500px", "180px");
this.win.setCenter();
this.win.shadow = true;
this.win.hide(false);
document.getElementById('id_change_pass').onclick = function (thiz) {
return function () {
let data = {
email: document.getElementById('id_ch_login').value,
password: document.getElementById('id_ch_oldpassword').value,
new_password: document.getElementById('id_ch_newpassword').value,
repeat_new_password: document.getElementById('id_ch_newrpassword').value
};
if (data.email == '') {
document.getElementById('id_ch_login').select();
alert2(trt("Alert"),trt('Not_filled_Email_address'));
return;
}
if (!isEmail(data.email)) {
alert2(trt("Alert"),sprintf(trt('The_s_is_not_Email'), data.email));
return;
}
if (data.new_password != data.repeat_new_password) {
alert2(trt("Alert"),trt('Passwords_did_not_match'));
return;
}
thiz.win.showProgressBar();
$.ajax({
url: "/monitoring/pscripts/user.php?fn=3",
type: "POST",
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: JSON.stringify(data),
success: function (win) {
return function (response, status) {
if (status == 'success') {
if (response.errorCode == '0') {
alert2(trt("Alert"),trt('Password_changed_successfully'));
} else {
alert2(trt("Alert"),response.errorMessage);
}
} else {
alert2(trt("Alert"),status);
}
win.hideProgressBar();
}
}(thiz.win)
});
};
}(this);
document.getElementById('exit_pass_' + this.win.uid).onclick = function (win) {
return function () {
win.Close();
};
}(this.win);
}
//logout current user
Logout() {
let xs = '<?xml version="1.0" encoding="utf-8"?><metadata fn="7"><cmd><![CDATA[1]]></cmd></metadata>';
var request = new TRequest(this);
if (request.callServer(ScriptName, xs)) {
//m_winPP.showProgressBar();
}
}
//Display login and registration form
showLoginForm() {
if (this.win == null || this.win.closed) {
this.win = new TWin(true);
this.win.disableClosing = true;
this.win.BuildGUI(10, 10);
this.win.setCaption(trt('Authorization') + ' / ' + trt('Registration'));
this.win.setSize("350px", "150px");
/*let str='<div style="width: 100%; height: 100%; padding: 3px; text-align: left;">\n\
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%;">\n\
<tbody>\n\
<tr><td>\n\
<table border="0" style="width: 100%; height: 100%;">\n\
<tr>\n\
<td style="padding: 2px; width: 30%; white-space: nowrap"><b>'+trt('Login')+' (email):</b></td>\n\
<td style="padding: 2px;"><input type="text" maxlength="50" style="width: 100%; padding: 2px; display: inline;" id="login0" name="login"><br></td>\n\
</tr>\n\
<tr>\n\
<td style="padding: 2px;"><b>'+trt('Password')+':</b></td>\n\
<td style="padding: 2px;"><input type="password" maxlength="33" style="width: 100%; padding: 2px; display: inline;" id="password0" name="password"><br></td>\n\
</tr>\n\
<tr>\n\
<td style="padding: 2px;" colspan="2"><table cellspacing="0" cellpadding="0" border="0" style="width: 100%;"><tr><td><!--label for="save0" style="white-space: nowrap;"><input id="save0" type="checkbox" style="display: inline;" checked/>&nbsp;'+trt('Remember')+'&nbsp;(--><a href="#" onclick="showRestoreForm();">'+trt('Password_recovery')+'</a><!--)</label>--></td></tr></table></td>\n\
</tr>\n\
<tr><td colspan="2" style="text-align: right;"><input class="button-secondary" id="'+this.win.uid+'_login" type="button" style="display: inline;" value="'+trt('Log_in')+'"></td></tr>\n\
</table>\n\
</td></tr>\n\
</tbody>\n\
</table>\n\
</div>';
this.win.setContent(str);*/
let str;
var tab = new tcTabs(document.getElementById('TWin_Co_' + this.win.tWinId));
tb = tab.addTab({caption: trt('Authorization')});
str = '<div style="width: 100%; height: 100%; padding: 3px; text-align: left;">\n\
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%;">\n\
<tbody>\n\
<tr><td>\n\
<table border="0" style="width: 100%; height: 100%;">\n\
<tr>\n\
<td style="padding: 2px; width: 30%; white-space: nowrap"><b>' + trt('Login') + ' (' + trt('E_mail') + '):</b></td>\n\
<td style="padding: 2px;"><input type="text" maxlength="50" style="width: 100%; padding: 2px; display: inline;" id="login0" name="login"><br></td>\n\
</tr>\n\
<tr>\n\
<td style="padding: 2px;"><b>' + trt('Password') + ':</b></td>\n\
<td style="padding: 2px;"><input type="password" maxlength="33" style="width: 100%; padding: 2px; display: inline;" id="password0" name="password"><br></td>\n\
</tr>\n\
<tr>\n\
<td style="padding: 2px;" colspan="2"><table cellspacing="0" cellpadding="0" border="0" style="width: 100%;"><tr><td><label for="save0" style="white-space: nowrap;"><input id="save0" type="checkbox" style="display: inline;" checked/>&nbsp;' + trt('Remember') + '&nbsp;</label>(<a href="#" id="' + this.win.uid + '_restore">' + trt('Forgot_your_password') + '</a>)</td></tr></table></td>\n\
</tr>\n\
<tr><td colspan="2" style="text-align: right;"><input class="button-secondary" id="' + this.win.uid + '_login" type="button" style="display: inline;" value="' + trt('Log_in') + '"></td></tr>\n\
</table>\n\
</td></tr>\n\
</tbody>\n\
</table>\n\
</div>';
tb.setConText(str);
tb.setSel();
tb = tab.addTab({caption: trt('Registration')});
str = '<div style="width: 100%; height: 100%; padding: 3px; text-align: left;">\n\
<form action="./?path=f1;0;" id="create_order_form" method="post" name="create_order_form">\n\
<input type="hidden" name="fn" value="2">\n\
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%;">\n\
<tbody>\n\
<tr><td style="padding: 2px;">' + trt('Surname') + '</td><td style="padding: 2px;"><input type="text" maxlength="50" style="width: 100%; display: inline;" id="' + this.win.uid + '_lastname" value=""></td></tr>\n\
<tr><td style="padding: 2px;"><strong>' + trt('Name') + ' *</strong></td><td style="padding: 2px;"><input type="text" maxlength="50" style="width: 100%; display: inline;" id="' + this.win.uid + '_firstname" value=""></td></tr>\n\
<tr><td style="padding: 2px;">' + trt('Company') + ' </td><td style="padding: 2px;"><input type="text" maxlength="255" style="width: 100%; display: inline;" id="' + this.win.uid + '_company" value=""></td></tr>\n\
<tr><td style="padding: 2px;">' + trt('Position') + ' </td><td style="padding: 2px;"><input type="text" maxlength="255" style="width: 100%; display: inline;" id="' + this.win.uid + '_position" value=""></td></tr>\n\
<tr><td style="padding: 2px;">' + trt('Phone') + ' </td><td style="padding: 2px;"><input type="text" maxlength="50" style="width: 100%; display: inline;" id="' + this.win.uid + '_phone" value=""></td></tr>\n\
<tr><td style="padding: 2px;"><strong>' + trt('E_mail') + ' *</strong></td><td style="padding: 2px;"><input type="text" maxlength="50" style="width: 100%; display: inline;" id="' + this.win.uid + '_email" value=""></td></tr>\n\
<tr><td colspan="2" style="padding: 2px;"><b>' + trt('Enter_the_numbers_from_the_image') + ' *</b></td></tr>\n\
<tr><td style="padding: 2px;"><img id="l_img" src="/resources/metadata/include/captcha.php?id=1" alt="' + trt('Security_Code') + '" /></td><td style="padding: 2px;"><input type="text" maxlength="5" style="width: 100%; display: inline;" id="' + this.win.uid + '_captcha" name="captcha"><br><a href="#" style="white-space: nowrap" onclick="document.getElementById(\'l_img\').src = \'/resources/metadata/include/captcha.php?id=2&time=' + (new Date()).getTime() + '\'">&#x21bb; ' + trt('Another_picture') + '</a></td></tr>\n\
<tr><td colspan="2" style="text-align: right;"><input class="button-secondary" id="' + this.win.uid + '_registration" type="button" style="display: inline;" value="' + trt('Registration') + '"></td></tr>\n\
</tbody>\n\
</table>\n\
</form>\n\
</div>';
tb.setConText(str);
//tb.setSel()
//Center the window and display the shadow
this.win.setCenter();
this.win.shadow = true;
this.win.hide(false);
//Clicking on the button login
var obj = null;
obj = document.getElementById(this.win.uid + '_login');
if (obj !== null) {
obj.onclick = function (thiz) {
return function () {
let login=document.getElementById('login0').value;
let password=document.getElementById('password0').value;
if(login==''){ alert2(trt("Alert"),trt('Please_fill_in_the_login_field')); return; }
if(password==''){ alert2(trt("Alert"),trt('Please_fill_in_the_password_field')); return; }
let xs = '<?xml version="1.0" encoding="utf-8"?>\
<metadata fn="7">\
<cmd><![CDATA[3]]></cmd>\n\
<login><![CDATA[' + login + ']]></login>\
<password><![CDATA[' + password + ']]></password>\
</metadata>';
let request = new TRequest(thiz);
if (request.callServer(ScriptName, xs)) {
thiz.win.showProgressBar();
}
};
}(this);
}
//Щелчёк по нажатию Enter
obj = document.getElementById('password0');
if (obj != null) {
obj.onkeypress = function (thiz) {
return function (e) {
if (e.keyCode == 13) {
let xs = '<?xml version="1.0" encoding="utf-8"?>\
<metadata fn="7">\
<cmd><![CDATA[3]]></cmd>\n\
<login><![CDATA[' + document.getElementById('login0').value + ']]></login>\
<password><![CDATA[' + document.getElementById('password0').value + ']]></password>\
</metadata>';
let request = new TRequest(thiz);
if (request.callServer(ScriptName, xs)) {
thiz.win.showProgressBar();
}
}
};
}(this);
}
//Регистрация нового пользователя и компании
obj = document.getElementById(this.win.uid + '_registration');
if (obj !== null) {
obj.onclick = function (thiz) {
return function () {
//showProgressBar(document.getElementById('TWin_Co_'+thiz.win.tWinId),thiz.win.uid);
let data = {
lastname: document.getElementById(thiz.win.uid + '_lastname').value,
firstname: document.getElementById(thiz.win.uid + '_firstname').value,
company: document.getElementById(thiz.win.uid + '_company').value,
position: document.getElementById(thiz.win.uid + '_position').value,
phone: document.getElementById(thiz.win.uid + '_phone').value,
email: document.getElementById(thiz.win.uid + '_email').value,
captcha: document.getElementById(thiz.win.uid + '_captcha').value
};
//Check for completeness of the fields
if (data.firstname == '') {
document.getElementById(thiz.win.uid + '_firstname').select();
alert2(trt("Alert"),sprintf(trt('The_s_field_is_empty'), trt('Name')));
return;
}
if (data.email == '') {
document.getElementById(thiz.win.uid + '_email').select();
alert2(trt("Alert"),sprintf(trt('The_s_field_is_empty'), trt('Email')));
return;
}
if (!isEmail(data.email)) {
document.getElementById(thiz.win.uid + '_email').select();
alert2(trt("Alert"),trt('Please_enter_a_valid_email_address'));
return;
}
thiz.win.showProgressBar();
$.ajax({
url: "/monitoring/pscripts/user.php?fn=1",
type: "POST",
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: JSON.stringify(data),
success: function (win) {
return function (response, status) {
if (status == 'success') {
if (response.errorCode == '0') {
alert2(trt("Alert"),trt('New_user_is_registered') + '\n' + trt('The_password_has_been_sent_to_you_by_Email'));
} else {
alert2(trt("Alert"),response.errorMessage);
}
} else {
alert2(trt("Alert"),status);
}
win.hideProgressBar();
}
}(thiz.win)
});
};
}(this);
}
obj = document.getElementById(this.win.uid + '_restore');
if (obj !== null) {
obj.onclick = function (thiz) {
return function () {
thiz.showRestoreForm();
};
}(this);
}
}
/*
log.bind(this, 'Hello world')
let user = {
firstName: "Вася"
};
function func() {
alert2(trt("Alert"),this.firstName);
}
let funcUser = func.bind(user);
funcUser(); // Вася
*/
/*var field1=document.getElementById("lang_1");
if(field1!=null)
{
field1.value=getURLParam("lng");
}
var field2=document.getElementById("lang_2");
if(field2!=null)
{
field2.value=getURLParam("lng");
}*/
}
//Display password recovery form
showRestoreForm() {
let win = new TWin(true);
win.BuildGUI(10, 10);
win.setCaption(trt("Password_recovery"));
let str = '<div id="TWin_DT_' + win.tWinId + '" style="width: 100%; height: 100%; padding: 4px; text-align: left;">\n\
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%; padding: 0px;">\n\
<tr>\n\
<td style="padding: 2px; white-space: nowrap"><b>' + trt('Login') + ' (E-mail):</b></td>\n\
<td style="padding: 2px;"><input type="text" maxlength="40" style="width: 100%; display: inline;" id="' + win.uid + '_email" name="login"></td>\n\
</tr>\n\
<tr>\n\
<td colspan="2" style="padding: 2px;"><b>' + trt('Enter_the_numbers_from_the_image') + ' *</b></td>\n\
</tr>\n\
<tr>\n\
<td style="width: 10px; padding: 2px;"><img id="r_img" src="/resources/metadata/include/captcha.php?id=2" alt="' + trt('Security_Code') + '" /></td>\n\
<td style="padding: 2px;"><input type="text" maxlength="5" style="width: 100%; display: inline;" id="' + win.uid + '_captcha" name="captcha"><br><a href="#" style="white-space: nowrap" onclick="document.getElementById(\'r_img\').src = \'/resources/metadata/include/captcha.php?id=2&time=\'+(new Date()).getTime()">&#x21bb; ' + trt('Another_picture') + '</a></td>\n\
</tr>\n\
<tr><td colspan="2" style="text-align: right; padding: 2px;"><input class="button-secondary" id="' + win.uid + '_restore" type="button" style="display: inline;" value="' + trt('Restore') + '"></td></tr>\n\
</table>\n\
</td></tr>\n\
</div>';
document.getElementById('TWin_Co_' + win.tWinId).innerHTML = str;
win.setSize("300px", "170px");
//Центрируем окно и отображаем тень
if (win.tbl.offsetHeight > win.div.offsetHeight) win.div.style.height = win.tbl.offsetHeight + "px";
if (win.tbl.offsetWidth > win.div.offsetWidth) win.div.style.width = win.tbl.offsetWidth + "px";
win.setCenter();
win.shadow = true;
win.hide(false);
//Click on restore button
var obj = null;
obj = document.getElementById(win.uid + '_restore');
if (obj != null) {
obj.onclick = function (win) {
return function () {
let data = {
email: document.getElementById(win.uid + '_email').value,
captcha: document.getElementById(win.uid + '_captcha').value
};
if (data.email == '') {
document.getElementById(win.uid + '_email').select();
alert2(trt("Alert"),trt('Not_filled_Email_address'));
return;
}
if (!isEmail(data.email)) {
document.getElementById(win.uid + '_email').select();
alert2(trt("Alert"),trt('Please_enter_a_valid_email_address'));
return;
}
win.showProgressBar();
$.ajax({
url: "/monitoring/pscripts/user.php?fn=2",
type: "POST",
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: JSON.stringify(data),
success: function (win) {
return function (response, status) {
if (status == 'success') {
if (response.errorCode == '0') {
alert2(trt("Alert"),trt('A_new_password_has_been_sent_to_your_email') + '.');
} else {
alert2(trt("Alert"),response.errorMessage);
}
} else {
alert2(trt("Alert"),status);
}
win.hideProgressBar();
}
}(win)
});
};
}(win);
}
}
//Check whether the already authorized (+ attempt to log in through "hash").
isLogined() {
let xs = '<?xml version="1.0" encoding="utf-8"?><metadata fn="7"><cmd><![CDATA[2]]></cmd></metadata>';
//var xs='{"fn":7,"cmd":2}';
let request = new TRequest(this);
if (request.callServer(ScriptName, xs)) {
//m_winPP.showProgressBar();
}
};
//Checking the session without its extension, if it is completed, we display the authorization window.
checkSession() {
let jqxhr = $.getJSON('./session.php', {}, function (thiz) {
return function (data) {
if (data.result == 'ERROR') {
thiz.showLoginForm();
}
divsh.style.display = 'none';
};
}(this));
jqxhr.fail(function () {
divsh.style.display = 'block';
});
setTimeout(function (thiz) {
return function () {
thiz.checkSession();
};
}(this), 10000);
}
}