133 lines
3.5 KiB
JavaScript
133 lines
3.5 KiB
JavaScript
"use strict";
|
|
|
|
import {Utils} from "../Utils/Utils.ecma.js";
|
|
|
|
/**
|
|
* @typedef {import("../OpoTests.ecma.js").OpoTests} OpoTests
|
|
*/
|
|
|
|
/**
|
|
* @class UserView
|
|
* @constructor
|
|
* @param {!OpoTests} ot
|
|
* @returns {void}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
export const UserView = (function(){
|
|
|
|
/**
|
|
* @constructs UserView
|
|
* @param {!OpoTests} ot
|
|
* @returns {void}
|
|
* @access private
|
|
* @static
|
|
*/
|
|
const UserView = function(ot){
|
|
|
|
/** @type {Object.<string, string>} */
|
|
const data = {},
|
|
/** @type {Object.<string, integer|string|boolean|null>} */
|
|
session = {
|
|
/** @type {number} */
|
|
i : 0,
|
|
/** @type {string|null} */
|
|
user : null,
|
|
/** @type {boolean} */
|
|
ok : false
|
|
};
|
|
/** @type {number} */
|
|
let maximum = 5,
|
|
/** @type {boolean} */
|
|
started = false;
|
|
|
|
/**
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const constructor = () => {
|
|
Object.entries(Utils.decrypt_data(UserView.DATA)).forEach(([key, value]) => {
|
|
data[key] = value;
|
|
});
|
|
};
|
|
|
|
/**
|
|
* @returns {Array.<any|null>}
|
|
* @access public
|
|
*/
|
|
this.build_login = callback => {
|
|
|
|
/** @type {HTMLMainElement} */
|
|
const main = document.querySelector("main");
|
|
|
|
if(!started){
|
|
|
|
started = true;
|
|
maximum = ot.settings.get("maximum_sessions_tries", null, maximum);
|
|
|
|
if(ot.cookies.has("ot")){
|
|
Object.entries(Utils.decrypt_data(ot.cookies.get("ot"))).forEach(([key, value]) => {
|
|
session[key] = value;
|
|
});
|
|
if(session.ok){
|
|
Utils.execute(callback);
|
|
return;
|
|
};
|
|
}else
|
|
ot.cookies.set("ot", Utils.encrypt_data(session));
|
|
|
|
};
|
|
|
|
Utils.html(main, ot.comp.form("login", [
|
|
["text", "username"],
|
|
["password", "password"]
|
|
], (item, event) => {
|
|
event.preventDefault();
|
|
|
|
if(session.i < maximum){
|
|
|
|
/** @type {Object.<string, string|number|boolean|Array.<string|number|boolean>>} */
|
|
const subdata = ot.comp.get_form_data(item);
|
|
|
|
if(data[subdata.username] && data[subdata.username] == subdata.password){
|
|
session.user = subdata.username;
|
|
session.ok = true;
|
|
}else
|
|
session.i ++;
|
|
|
|
ot.cookies.set("ot", Utils.encrypt_data(session));
|
|
|
|
};
|
|
|
|
if(session.ok)
|
|
Utils.execute(callback);
|
|
else{
|
|
|
|
/** @type {HTMLUListElement} */
|
|
const errors_box = document.querySelector(".form-errors");
|
|
|
|
errors_box.innerHTML = ``;
|
|
Utils.html(errors_box, ot.comp.i18n("login_error", "li"));
|
|
|
|
};
|
|
|
|
return false;
|
|
}));
|
|
|
|
};
|
|
|
|
/**
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.is_ok = () => !!session.ok;
|
|
|
|
constructor();
|
|
|
|
};
|
|
|
|
/** @type {string} */
|
|
UserView.DATA = "rqRBrhEHg0/t/VTQvcDpXjoE/0A0SZa8rX/t/Z+qFg/AkOh0Lvzz";
|
|
|
|
return UserView;
|
|
})(); |