2011-08-03 18:31:25 +00:00
|
|
|
/**
|
|
|
|
* The Session Manager provides functions to manage session in the database
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 14:26:41 +00:00
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2011-08-03 18:31:25 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2011-08-09 15:45:49 +00:00
|
|
|
|
2011-12-04 15:50:02 +00:00
|
|
|
var ERR = require("async-stacktrace");
|
2011-12-10 15:46:47 +00:00
|
|
|
var customError = require("../utils/customError");
|
2011-08-09 15:45:49 +00:00
|
|
|
var db = require("./DB").db;
|
|
|
|
var async = require("async");
|
|
|
|
var groupMangager = require("./GroupManager");
|
|
|
|
var authorMangager = require("./AuthorManager");
|
|
|
|
|
|
|
|
exports.doesSessionExist = function(sessionID, callback)
|
|
|
|
{
|
|
|
|
//check if the database entry of this session exists
|
|
|
|
db.get("session:" + sessionID, function (err, session)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
callback(null, session != null);
|
2011-08-09 15:45:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new session between an author and a group
|
|
|
|
*/
|
|
|
|
exports.createSession = function(groupID, authorID, validUntil, callback)
|
|
|
|
{
|
|
|
|
var sessionID;
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
//check if group exists
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
groupMangager.doesGroupExist(groupID, function(err, exists)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
|
2011-08-09 15:45:49 +00:00
|
|
|
//group does not exist
|
2011-12-04 15:50:02 +00:00
|
|
|
if(exists == false)
|
2011-08-09 15:45:49 +00:00
|
|
|
{
|
2011-12-10 15:46:47 +00:00
|
|
|
callback(new customError("groupID does not exist","apierror"));
|
2011-08-09 15:45:49 +00:00
|
|
|
}
|
|
|
|
//everything is fine, continue
|
|
|
|
else
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
//check if author exists
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
authorMangager.doesAuthorExists(authorID, function(err, exists)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
|
2011-08-09 15:45:49 +00:00
|
|
|
//author does not exist
|
2011-12-04 15:50:02 +00:00
|
|
|
if(exists == false)
|
2011-08-09 15:45:49 +00:00
|
|
|
{
|
2011-12-10 15:46:47 +00:00
|
|
|
callback(new customError("authorID does not exist","apierror"));
|
2011-08-09 15:45:49 +00:00
|
|
|
}
|
|
|
|
//everything is fine, continue
|
|
|
|
else
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
//check validUntil and create the session db entry
|
|
|
|
function(callback)
|
|
|
|
{
|
2011-08-09 19:14:32 +00:00
|
|
|
//check if rev is a number
|
2011-08-09 15:45:49 +00:00
|
|
|
if(typeof validUntil != "number")
|
|
|
|
{
|
|
|
|
//try to parse the number
|
|
|
|
if(!isNaN(parseInt(validUntil)))
|
|
|
|
{
|
|
|
|
validUntil = parseInt(validUntil);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-10 15:46:47 +00:00
|
|
|
callback(new customError("validUntil is not a number","apierror"));
|
2011-08-09 15:45:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//ensure this is not a negativ number
|
|
|
|
if(validUntil < 0)
|
|
|
|
{
|
2011-12-10 15:46:47 +00:00
|
|
|
callback(new customError("validUntil is a negativ number","apierror"));
|
2011-08-09 15:45:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//ensure this is not a float value
|
|
|
|
if(!is_int(validUntil))
|
|
|
|
{
|
2011-12-10 15:46:47 +00:00
|
|
|
callback(new customError("validUntil is a float value","apierror"));
|
2011-08-09 15:45:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//check if validUntil is in the future
|
2011-08-09 19:14:32 +00:00
|
|
|
if(Math.floor(new Date().getTime()/1000) > validUntil)
|
2011-08-09 15:45:49 +00:00
|
|
|
{
|
2011-12-10 15:46:47 +00:00
|
|
|
callback(new customError("validUntil is in the past","apierror"));
|
2011-08-09 15:45:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//generate sessionID
|
|
|
|
sessionID = "s." + randomString(16);
|
|
|
|
|
|
|
|
//set the session into the database
|
|
|
|
db.set("session:" + sessionID, {"groupID": groupID, "authorID": authorID, "validUntil": validUntil});
|
|
|
|
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
//set the group2sessions entry
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
//get the entry
|
|
|
|
db.get("group2sessions:" + groupID, function(err, group2sessions)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
2011-08-09 15:45:49 +00:00
|
|
|
|
|
|
|
//the entry doesn't exist so far, let's create it
|
|
|
|
if(group2sessions == null)
|
|
|
|
{
|
2011-08-09 19:14:32 +00:00
|
|
|
group2sessions = {sessionIDs : {}};
|
2011-08-09 15:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//add the entry for this session
|
2011-08-09 19:14:32 +00:00
|
|
|
group2sessions.sessionIDs[sessionID] = 1;
|
|
|
|
|
|
|
|
//save the new element back
|
|
|
|
db.set("group2sessions:" + groupID, group2sessions);
|
2011-08-09 15:45:49 +00:00
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
//set the author2sessions entry
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
//get the entry
|
|
|
|
db.get("author2sessions:" + authorID, function(err, author2sessions)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
2011-08-09 15:45:49 +00:00
|
|
|
|
|
|
|
//the entry doesn't exist so far, let's create it
|
|
|
|
if(author2sessions == null)
|
|
|
|
{
|
2011-08-09 19:14:32 +00:00
|
|
|
author2sessions = {sessionIDs : {}};
|
2011-08-09 15:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//add the entry for this session
|
2011-08-09 19:14:32 +00:00
|
|
|
author2sessions.sessionIDs[sessionID] = 1;
|
|
|
|
|
|
|
|
//save the new element back
|
|
|
|
db.set("author2sessions:" + authorID, author2sessions);
|
2011-08-09 15:45:49 +00:00
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
], function(err)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
|
2011-08-09 15:45:49 +00:00
|
|
|
//return error and sessionID
|
2011-12-04 15:50:02 +00:00
|
|
|
callback(null, {sessionID: sessionID});
|
2011-08-09 15:45:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.getSessionInfo = function(sessionID, callback)
|
|
|
|
{
|
|
|
|
//check if the database entry of this session exists
|
|
|
|
db.get("session:" + sessionID, function (err, session)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
|
2011-08-09 15:45:49 +00:00
|
|
|
//session does not exists
|
2011-12-04 15:50:02 +00:00
|
|
|
if(session == null)
|
2011-08-09 15:45:49 +00:00
|
|
|
{
|
2011-12-10 15:46:47 +00:00
|
|
|
callback(new customError("sessionID does not exist","apierror"))
|
2011-08-09 15:45:49 +00:00
|
|
|
}
|
|
|
|
//everything is fine, return the sessioninfos
|
|
|
|
else
|
|
|
|
{
|
|
|
|
callback(null, session);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a session
|
|
|
|
*/
|
|
|
|
exports.deleteSession = function(sessionID, callback)
|
|
|
|
{
|
2011-08-10 13:04:28 +00:00
|
|
|
var authorID, groupID;
|
|
|
|
var group2sessions, author2sessions;
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
//get the session entry
|
|
|
|
db.get("session:" + sessionID, function (err, session)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
|
2011-08-10 13:04:28 +00:00
|
|
|
//session does not exists
|
2011-12-04 15:50:02 +00:00
|
|
|
if(session == null)
|
2011-08-10 13:04:28 +00:00
|
|
|
{
|
2011-12-10 15:46:47 +00:00
|
|
|
callback(new customError("sessionID does not exist","apierror"))
|
2011-08-10 13:04:28 +00:00
|
|
|
}
|
|
|
|
//everything is fine, return the sessioninfos
|
|
|
|
else
|
|
|
|
{
|
|
|
|
authorID = session.authorID;
|
|
|
|
groupID = session.groupID;
|
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
//get the group2sessions entry
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
db.get("group2sessions:" + groupID, function (err, _group2sessions)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
2011-08-10 13:04:28 +00:00
|
|
|
group2sessions = _group2sessions;
|
2011-12-04 15:50:02 +00:00
|
|
|
callback();
|
2011-08-10 13:04:28 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
//get the author2sessions entry
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
db.get("author2sessions:" + authorID, function (err, _author2sessions)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
2011-08-10 13:04:28 +00:00
|
|
|
author2sessions = _author2sessions;
|
2011-12-04 15:50:02 +00:00
|
|
|
callback();
|
2011-08-10 13:04:28 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
//remove the values from the database
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
//remove the session
|
|
|
|
db.remove("session:" + sessionID);
|
|
|
|
|
|
|
|
//remove session from group2sessions
|
|
|
|
delete group2sessions.sessionIDs[sessionID];
|
|
|
|
db.set("group2sessions:" + groupID, group2sessions);
|
|
|
|
|
|
|
|
//remove session from author2sessions
|
|
|
|
delete author2sessions.sessionIDs[sessionID];
|
|
|
|
db.set("author2sessions:" + authorID, author2sessions);
|
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
], function(err)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
callback();
|
2011-08-10 13:04:28 +00:00
|
|
|
})
|
2011-08-09 15:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.listSessionsOfGroup = function(groupID, callback)
|
|
|
|
{
|
2011-08-09 19:14:32 +00:00
|
|
|
groupMangager.doesGroupExist(groupID, function(err, exists)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
|
2011-08-09 19:14:32 +00:00
|
|
|
//group does not exist
|
2011-12-04 15:50:02 +00:00
|
|
|
if(exists == false)
|
2011-08-09 19:14:32 +00:00
|
|
|
{
|
2011-12-10 15:46:47 +00:00
|
|
|
callback(new customError("groupID does not exist","apierror"));
|
2011-08-09 19:14:32 +00:00
|
|
|
}
|
|
|
|
//everything is fine, continue
|
|
|
|
else
|
|
|
|
{
|
|
|
|
listSessionsWithDBKey("group2sessions:" + groupID, callback);
|
|
|
|
}
|
|
|
|
});
|
2011-08-09 15:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.listSessionsOfAuthor = function(authorID, callback)
|
2011-08-10 13:04:28 +00:00
|
|
|
{
|
2011-08-09 19:14:32 +00:00
|
|
|
authorMangager.doesAuthorExists(authorID, function(err, exists)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
|
2011-08-09 19:14:32 +00:00
|
|
|
//group does not exist
|
2011-12-04 15:50:02 +00:00
|
|
|
if(exists == false)
|
2011-08-09 19:14:32 +00:00
|
|
|
{
|
2011-12-10 15:46:47 +00:00
|
|
|
callback(new customError("authorID does not exist","apierror"));
|
2011-08-09 19:14:32 +00:00
|
|
|
}
|
|
|
|
//everything is fine, continue
|
|
|
|
else
|
|
|
|
{
|
|
|
|
listSessionsWithDBKey("author2sessions:" + authorID, callback);
|
|
|
|
}
|
|
|
|
});
|
2011-08-09 15:45:49 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 13:04:28 +00:00
|
|
|
//this function is basicly the code listSessionsOfAuthor and listSessionsOfGroup has in common
|
2011-08-09 19:14:32 +00:00
|
|
|
function listSessionsWithDBKey (dbkey, callback)
|
2011-08-09 15:45:49 +00:00
|
|
|
{
|
2011-08-09 19:14:32 +00:00
|
|
|
var sessions;
|
2011-08-09 15:45:49 +00:00
|
|
|
|
2011-08-09 19:14:32 +00:00
|
|
|
async.series([
|
|
|
|
function(callback)
|
|
|
|
{
|
|
|
|
//get the group2sessions entry
|
|
|
|
db.get(dbkey, function(err, sessionObject)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
2011-08-09 19:14:32 +00:00
|
|
|
sessions = sessionObject ? sessionObject.sessionIDs : null;
|
2011-12-04 15:50:02 +00:00
|
|
|
callback();
|
2011-08-09 19:14:32 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function(callback)
|
2011-08-10 13:04:28 +00:00
|
|
|
{
|
2011-08-09 19:14:32 +00:00
|
|
|
//collect all sessionIDs in an arrary
|
|
|
|
var sessionIDs = [];
|
|
|
|
for (var i in sessions)
|
|
|
|
{
|
|
|
|
sessionIDs.push(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
//foreach trough the sessions and get the sessioninfos
|
|
|
|
async.forEach(sessionIDs, function(sessionID, callback)
|
|
|
|
{
|
|
|
|
exports.getSessionInfo(sessionID, function(err, sessionInfo)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
2011-08-09 19:14:32 +00:00
|
|
|
sessions[sessionID] = sessionInfo;
|
2011-12-04 15:50:02 +00:00
|
|
|
callback();
|
2011-08-09 19:14:32 +00:00
|
|
|
});
|
|
|
|
}, callback);
|
|
|
|
}
|
|
|
|
], function(err)
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
callback(null, sessions);
|
2011-08-09 19:14:32 +00:00
|
|
|
});
|
2011-08-09 15:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a random String with the given length. Is needed to generate the SessionIDs
|
|
|
|
*/
|
|
|
|
function randomString(len)
|
|
|
|
{
|
2011-08-10 13:24:21 +00:00
|
|
|
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
|
|
var randomstring = '';
|
|
|
|
for (var i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
var rnum = Math.floor(Math.random() * chars.length);
|
|
|
|
randomstring += chars.substring(rnum, rnum + 1);
|
2011-08-09 15:45:49 +00:00
|
|
|
}
|
2011-08-10 13:24:21 +00:00
|
|
|
return randomstring;
|
2011-08-09 15:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//checks if a number is an int
|
|
|
|
function is_int(value)
|
|
|
|
{
|
|
|
|
return (parseFloat(value) == parseInt(value)) && !isNaN(value)
|
|
|
|
}
|