152 lines
4.4 KiB
JavaScript
152 lines
4.4 KiB
JavaScript
"use strict";
|
|
|
|
import {Common} from "../Utils/Common.ecma.js";
|
|
|
|
/**
|
|
* @class SessionModel
|
|
* @constructor
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} inputs
|
|
* @return {void}
|
|
* @access private
|
|
* @static
|
|
*/
|
|
export const SessionModel = (function(){
|
|
|
|
/**
|
|
* @constructs SessionModel
|
|
* @param {!number} i
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} inputs
|
|
* @return {void}
|
|
* @access private
|
|
* @static
|
|
*/
|
|
const SessionModel = function(i, inputs){
|
|
|
|
/** @type {SessionModel} */
|
|
const self = this,
|
|
/** @type {Object.<string, any|null>} */
|
|
data = {};
|
|
/** @type {number|null} */
|
|
let id = null,
|
|
/** @type {Array.<string>} */
|
|
permissions = [],
|
|
/** @type {number} */
|
|
last_check = 0,
|
|
/** @type {string|null} */
|
|
cookie_key = null,
|
|
/** @type {integer|null} */
|
|
timeout = null;
|
|
|
|
/** @type {string|null} */
|
|
this.nick = null;
|
|
/** @type {number} */
|
|
this.type = SessionModel.LOCAL;
|
|
/** @type {number} */
|
|
this.i = i;
|
|
|
|
/**
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const constructor = () => {
|
|
|
|
/** @type {string|null} */
|
|
const cookie = anp.cookies.get(cookie_key = Common.get_value("cookie", inputs, null));
|
|
|
|
if(cookie){
|
|
|
|
/** @type {string} */
|
|
let raw_permissions,
|
|
/** @type {string} */
|
|
raw_data,
|
|
/** @type {string} */
|
|
raw_type,
|
|
/** @type {string} */
|
|
raw_id;
|
|
|
|
[raw_id, self.nick, raw_type, raw_permissions, raw_data] = cookie.split("|");
|
|
permissions = raw_permissions.split(",");
|
|
Object.assign(Common.data_encode(raw_data)).forEach(([key, value]) => self.set(key, value, false));
|
|
self.type = Number(raw_type) || SessionModel.LOCAL;
|
|
id = Number(raw_id) || null;
|
|
|
|
}else{
|
|
id = Common.get_value("id", inputs, null);
|
|
permissions = Common.get_value("permissions", inputs, []);
|
|
self.nick = Common.get_value("nick", inputs, null);
|
|
self.type = Number(Common.get_value("type", inputs, self.type)) || SessionModel.LOCAL;
|
|
};
|
|
last_check = Date.now();
|
|
timeout = Common.get_value(["session_timeout", "timeout"], inputs, 300000);
|
|
|
|
};
|
|
|
|
/**
|
|
* @param {!(string|Array.<string>)} permissions
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.check_permissions = permissions => permissions.some(self.permissions.includes);
|
|
|
|
/**
|
|
* @param {!string} key
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.has = key => data[key] !== undefined;
|
|
|
|
/**
|
|
* @param {!string} key
|
|
* @param {?any} [value = null]
|
|
* @param {!boolean} [overwrite = true]
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.set = (key, value = null, overwrite = true) => {
|
|
if(Check.is_key(key) && (overwrite || !self.has(key))){
|
|
data[key] = value;
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
/**
|
|
* @param {!string} key
|
|
* @returns {any|null}
|
|
* @access public
|
|
*/
|
|
this.get = key => self.has(key) ? data[key] : null;
|
|
|
|
/**
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.save = () => {
|
|
if(cookie_key){
|
|
anp.cookies.set(cookie_key, Common.data_encode([
|
|
id,
|
|
self.nick,
|
|
self.type,
|
|
permissions.join(","),
|
|
Common.data_encode(data)
|
|
]).join("|"), {
|
|
overwrite : true,
|
|
expires : new Date(Date.now() + timeout),
|
|
path : "/"
|
|
});
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
constructor();
|
|
|
|
};
|
|
|
|
/** @type {number} */
|
|
SessionModel.LOCAL = 1 << 0;
|
|
/** @type {number} */
|
|
SessionModel.REMOTE = 1 << 1;
|
|
|
|
return SessionModel;
|
|
})(); |