2019-02-08 23:20:57 +01:00
|
|
|
/*
|
2013-02-13 01:33:22 +00:00
|
|
|
* Stores session data in the database
|
|
|
|
* Source; https://github.com/edy-b/SciFlowWriter/blob/develop/available_plugins/ep_sciflowwriter/db/DirtyStore.js
|
|
|
|
* This is not used for authors that are created via the API at current
|
2019-01-23 16:58:43 +00:00
|
|
|
*
|
|
|
|
* RPB: this module was not migrated to Promises, because it is only used via
|
|
|
|
* express-session, which can't actually use promises anyway.
|
2013-02-13 01:33:22 +00:00
|
|
|
*/
|
|
|
|
|
2020-09-21 16:45:25 -04:00
|
|
|
const Store = require('ep_etherpad-lite/node_modules/express-session').Store;
|
|
|
|
const db = require('ep_etherpad-lite/node/db/DB').db;
|
|
|
|
const log4js = require('ep_etherpad-lite/node_modules/log4js');
|
2013-02-13 01:33:22 +00:00
|
|
|
|
2020-09-21 16:45:25 -04:00
|
|
|
const messageLogger = log4js.getLogger('SessionStore');
|
|
|
|
|
|
|
|
const SessionStore = module.exports = function SessionStore() {};
|
2013-02-13 01:33:22 +00:00
|
|
|
|
|
|
|
SessionStore.prototype.__proto__ = Store.prototype;
|
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
SessionStore.prototype.get = function(sid, fn) {
|
2013-02-13 01:33:22 +00:00
|
|
|
messageLogger.debug('GET ' + sid);
|
2020-09-21 16:41:45 -04:00
|
|
|
db.get('sessionstorage:' + sid, (err, sess) => {
|
2013-02-13 01:33:22 +00:00
|
|
|
if (sess) {
|
|
|
|
sess.cookie.expires = 'string' == typeof sess.cookie.expires ? new Date(sess.cookie.expires) : sess.cookie.expires;
|
2013-03-23 18:01:44 +00:00
|
|
|
if (!sess.cookie.expires || new Date() < sess.cookie.expires) {
|
2013-02-13 01:33:22 +00:00
|
|
|
fn(null, sess);
|
|
|
|
} else {
|
2020-09-21 16:41:45 -04:00
|
|
|
this.destroy(sid, fn);
|
2013-02-13 01:33:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fn();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
SessionStore.prototype.set = function(sid, sess, fn) {
|
2013-02-13 01:33:22 +00:00
|
|
|
messageLogger.debug('SET ' + sid);
|
2020-09-21 16:52:57 -04:00
|
|
|
db.set('sessionstorage:' + sid, sess);
|
2020-09-21 16:55:27 -04:00
|
|
|
if (fn) process.nextTick(fn);
|
2013-02-13 01:33:22 +00:00
|
|
|
};
|
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
SessionStore.prototype.destroy = function(sid, fn) {
|
2013-02-13 01:33:22 +00:00
|
|
|
messageLogger.debug('DESTROY ' + sid);
|
2020-09-21 16:52:57 -04:00
|
|
|
db.remove('sessionstorage:' + sid);
|
2020-09-21 16:55:27 -04:00
|
|
|
if (fn) process.nextTick(fn);
|
2013-02-13 01:33:22 +00:00
|
|
|
};
|