Files
Metadata_PHP/metadata/dbms/login.js

362 lines
12 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//Шаблон класса для авторизации пользователя (под проекты переделывается)
class DBMSUser
{
constructor(){
this.divsh=null;
this.id=null;
this.name='';
this.surname='';
this.patronymic='';
this.temp=false;
this.role='';
this.m_ls=new Array(); //Listeners called OnLogin function.
this.divsh = null;
//Запрашиваю данные о текущем пользователе с сервера
//this.LoadData();
}
applyReq(req,fn,node)
{
this.showShadow(false);
if(node.error_code>0) {
alert2(trt('Alert'), node.error_message);
return;
}
if(fn==7)
{
var nCmd=findFirstNode(node, "cmd");
if(nCmd!=null)
{
if(findFirstNode(nCmd,'#cdata-section').nodeValue=="1") //Logout
{
location.reload();
//window.location.href = '../';
}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.surname=getCdataValue(findNode(node,'surname'));
this.patronymic=getCdataValue(findNode(node,'patronymic'));
if (typeof this.onUpdateName !== 'undefined') {
this.onUpdateName(this.name,this.surname,this.patronymic);
}
//configGUIbyAccessLevel();
//var shadow=document.getElementById("shadow");
//if(shadow.parentNode!=null) shadow.parentNode.removeChild(shadow);
//Информируем слушатеелй о логине
for(i=0;i<this.m_ls.length;i++){
if(this.m_ls[i]!==null){
this.m_ls[i].OnLogin(true); //0 - authorized, 1 - an incorrect username or password, 2 - left.
}
}
}
}else if(findFirstNode(nCmd,'#cdata-section').nodeValue=="3") //Login
{
if(findNode(node,'#cdata-section').nodeValue=="0") //if not logged
{
alert2(trt('Attention'),trt('Invalid_username_and_or_password'));
}else
{
//location.reload();
//Информируем слушатеелй о логине
for(i=0;i<this.m_ls.length;i++){
if(this.m_ls[i]!==null){
this.m_ls[i].OnLogin(true); //0 - authorized, 1 - an incorrect username or password, 2 - left.
}
}
}
}else if(findFirstNode(nCmd,'#cdata-section').nodeValue=="4") //Register
{
//if(findNode(node,'#cdata-section').nodeValue=="1") //if register
//{
// alert2(trt('Attention'),trt('New_user_is_registered')+'\n'+trt('The_password_has_been_sent_to_you_by_Email'),function(){ location.reload(); });
//}
}
}
}
};
//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);
deleteHTML('TWin_CL_'+this.win.uid); //Удаляю кнопку закрыть
this.win.setCaption(trt('Authorization'));
this.win.setSize("350px","184px");
let str=`<div style="width: 100%; height: 100%; padding: 3px; text-align: left;">
<table cellpadding="0" cellspacing="0" style="width: 100%; height: 100%;">
<tbody>
<tr><td>
<table style="width: 100%; height: 100%;">
<tr>
<td style="padding: 2px; width: 30%; white-space: nowrap"><b>`+trt('Login')+` (E-mail):</b></td>
<td style="padding: 2px;"><input type="text" maxlength="50" style="width: 100%; padding: 2px; display: inline;" id="tcLogin`+this.uid+`" name="login"><br></td>
</tr>
<tr>
<td style="padding: 2px;"><b>Password:</b></td>
<td style="padding: 2px;"><input type="password" maxlength="33" style="width: 100%; padding: 2px; display: inline;" id="tcPassword`+this.uid+`" name="password"><br></td>
</tr>
<tr>
<td style="padding: 2px;" colspan="2"><table style="width: 100%;"><tr><td><label for="save0" style="white-space: nowrap;"><input id="save_`+this.uid+`" type="checkbox" style="display: inline;" checked/>&nbsp;Remember&nbsp;(<a href="#" onclick="g_user.showRestoreForm();">Forgot your password?</a>)</label></td></tr></table></td>
</tr>
<tr><td colspan="2" style="text-align: right;"><input class="button-secondary" id="TLogin_E`+this.uid+`" type="button" style="display: inline;" value="`+trt('Log_in')+`"></td></tr>
</table>
</td></tr>
</tbody>
</table>
</div>`;
this.win.setContent(str);
//Center the window and display the shadow
//if(this.win.tbl.offsetHeight>this.win.div.offsetHeight) this.win.div.style.height=this.win.tbl.offsetHeight+"px";
//if(this.win.tbl.offsetWidth>this.win.div.offsetWidth) this.win.div.style.width=this.win.tbl.offsetWidth+"px";
this.win.setCenter();
this.win.shadow=true;
this.win.hide(false);
//Нажатие на кнопку авторизоваться
var obj=null;
obj=document.getElementById('TLogin_E'+this.uid);
if(obj!==null)
{
obj.onclick=()=>
{
let xs=`<?xml version="1.0" encoding="utf-8"?>
<metadata fn="7">
<cmd><![CDATA[3]]></cmd>
<login><![CDATA[`+document.getElementById('tcLogin'+this.uid).value+`]]></login>
<password><![CDATA[`+document.getElementById('tcPassword'+this.uid).value+`]]></password>
</metadata>`;
let request=new TRequest(this);
if(request.callServer(ScriptName,xs))
{
this.win.showProgressBar();
}
};
}
}
};*/
//logout current user
/*Logout()
{
let xs='<?xml version="1.0" encoding="utf-8"?><metadata fn="7"><cmd><![CDATA[1]]></cmd></metadata>';
let request=new TRequest(this);
if(request.callServer(ScriptName,xs))
{
this.showShadow();
}
};*/
showLock(visible) {
if(this.divsh==null) {
this.divsh = document.createElement('div'); //Shadow
this.divsh.style.cssText = "display: none; position: fixed; z-index: 1000; top:0; left:0; height: 100%; width: 100%; background: rgba(0,0,0,0.3);";
document.body.append(this.divsh);
}
if(!visible)
this.divsh.style.display='none';
else
this.divsh.style.display='block';
}
//Checking the session without its extension, if it is completed, we display the authorization window.
/*checkSession()
{
$.ajax({
url: '../session',
data: "{}",
type: "POST",
dataType: "json",
success: (data,status) => {
if(status=='success')
{
if(data.result=='ERROR'){
this.showLoginForm();
}else
if(data.result=='OK'){
this.id=data.user_id;
}
this.showLock(false);
}else
{
this.showLock(true);
}
},
error: (jqXHR, exception)=>
{
this.showLock(true);
}
});
setTimeout(()=>this.checkSession(), 10000);
};*/
showShadow(visible)
{
if(visible)
{
this.divsh=document.createElement('div'); //Тень для модального окна
this.divsh.style.cssText="display: block; position: fixed; z-index:9999; top:0; left:0; height: 100%; width: 100%; background: rgba(0,0,0,0.3);";
document.body.appendChild( this.divsh );
}else
{
deleteHTML(this.divsh);
}
};
//Функция запрашивает информацию о текущем пользователе с сервера
LoadData()
{
let r = new TRequest(this);
let xs='<?xml version="1.0" encoding="utf-8"?><metadata fn="7"><cmd><![CDATA[2]]></cmd></metadata>';
if(r.callServer(ScriptName,xs))
{
this.showShadow(true);
}
};
//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>';
let request=new TRequest(this);
if(request.callServer(ScriptName,xs))
{
this.showShadow(true);
alert(ScriptName+" = "+xs);
//m_winPP.showProgressBar();
}
};*/
addListener(l)
{ if(l.OnLogin==null) alert('Object does not have the function "OnLogin()"!');
this.m_ls[this.m_ls.length]=l;
};
remListener(l){
for(let i=0;i<this.m_ls.length;i++){
if(this.m_ls[i]==l)
this.m_ls[i]=null;
}
};
//Display password recovery form
showRestoreForm()
{
let win=new TWin(true);
win.BuildGUI(10,10);
win.setCaption(trt("Password_recovery"));
let str=`<div style="width: 100%; height: 100%; padding: 4px; text-align: left;">
<table cellpadding="0" cellspacing="0" style="width: 100%; height: 100%; padding: 0px;">
<tr>
<td style="padding: 2px; white-space: nowrap"><b>`+trt('Login')+` (E-mail)</b></td>
<td style="padding: 2px;"><input type="text" maxlength="40" style="width: 100%; display: inline;" id="`+win.uid+`_email" name="login">
<table id="`+win.uid+`_phone" style="width: 100%; display: none;" cellpadding="0" cellspacing="0"><tr><td style="white-space: nowrap;"><b>+7 ( </b><input type="text" maxlength="3" onkeydown="this.value = this.value.replace(/[^0-9]/g, \'\')" onkeyup="this.value = this.value.replace(/[^0-9]/g, \'\')" style="width: 35px; display: inline;" id="`+win.uid+`_phone0" value=""><b> ) </b></td><td style="width: 90%;"><input type="text" onkeydown="this.value = this.value.replace(/[^0-9]/g, \'\')" onkeyup="this.value = this.value.replace(/[^0-9]/g, \'\')" maxlength="7" style="width: 100%;display: inline;" id="`+win.uid+`_phone1" value=""></td></tr></table><br></td>
</tr>
<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>
</table>
</td></tr>
</div>`;
win.setContent(str);
//Центрируем окно и отображаем тень
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()
{
win.showProgressBar();
let em=document.getElementById(win.uid+'_email').value;
if(em==''){ document.getElementById(win.uid+'_email').select(); alert(trt('Not_filled_Email_address')); win.hideProgressBar(); return; }
if(! isEmail(em)){ document.getElementById(win.uid+'_email').select(); alert(trt('Please_enter_a_valid_email_address')); win.hideProgressBar(); return; }
//Send AJAX reqwest to server
let xml=`<?xml version="1.0" encoding="utf-8"?>
<metadata fn="7">
<cmd><![CDATA[0]]></cmd>
<login><![CDATA[`+em+`]]></login>
<captcha><![CDATA[]]></captcha>
</metadata>`;
//Anonymous object
obj=new function(win)
{
this.processReqChange = function(xmlHttpRequest, url, xmlString)
{
if(typeof(xmlHttpRequest.status)=='undefined' || xmlHttpRequest.status == 200)
{
if(typeof(xmlHttpRequest.responseXML)=='undefined' && xmlHttpRequest.contentType.match(/\/xml/)) //For IE XDomainRequest
xmlHttpRequest.responseXML=CreateXMLDOC(xmlHttpRequest.responseText);
//загрузился xml документ начинаем его разбирать (по id функции в документе)
let xmldoc = xmlHttpRequest.responseXML
if(xmldoc==null) {
alert2(trt('Alert'), trt('Wrong_XML_document') + "!\n" + xmlHttpRequest.responseText);
}else {
var node = xmldoc.documentElement;
if ((node == null) || (node.getAttribute("fn") == null)) alert(trt('Error') + "\n " + trt("No_data") + "!\n" + xmlHttpRequest.responseText);
else {
//alert("Принятый браузером XML=\n"+getXMLNodeSerialisation(node));
var fn = node.getAttribute("fn");
if (fn == -1) {
alert(findFirstNode(node, '#cdata-section').nodeValue);
this.win.hideProgressBar();
} else if (fn == 7) {
alert(findFirstNode(node, '#cdata-section').nodeValue);
this.win.Close();
}
}
}
}else
{
if(confirm(trt('Failed_to_get_data')+"\n URL: "+url+"\n"+xmlHttpRequest.statusText+"\nRedo the request?"))
{
var call=new myXMLHttpRequest(this);
call.callServer(url,xmlString);
}
}
return null;
};
this.win=win; //For close after showing message.
}
(win);
var call=new myXMLHttpRequest(obj);
call.callServer(ScriptName,xml);
};
}(win);
}
}
}