diff --git a/node/db/SessionManager.js b/node/db/SessionManager.js index a394f5442..e98100c67 100644 --- a/node/db/SessionManager.js +++ b/node/db/SessionManager.js @@ -17,24 +17,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + var ERR = require("async-stacktrace"); var customError = require("../utils/customError"); 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) { if(ERR(err, callback)) return; - callback(null, session != null); + callback(null, session ? true : false); }); -} - +}; + /** * Creates a new session between an author and a group */ @@ -49,9 +49,9 @@ exports.createSession = function(groupID, authorID, validUntil, callback) groupMangager.doesGroupExist(groupID, function(err, exists) { if(ERR(err, callback)) return; - + //group does not exist - if(exists == false) + if(!exists) { callback(new customError("groupID does not exist","apierror")); } @@ -68,9 +68,9 @@ exports.createSession = function(groupID, authorID, validUntil, callback) authorMangager.doesAuthorExists(authorID, function(err, exists) { if(ERR(err, callback)) return; - + //author does not exist - if(exists == false) + if(!exists) { callback(new customError("authorID does not exist","apierror")); } @@ -88,9 +88,9 @@ exports.createSession = function(groupID, authorID, validUntil, callback) if(typeof validUntil != "number") { //try to parse the number - if(!isNaN(parseInt(validUntil))) + if(!isNaN(parseInt(validUntil, 10))) { - validUntil = parseInt(validUntil); + validUntil = parseInt(validUntil, 10); } else { @@ -98,34 +98,34 @@ exports.createSession = function(groupID, authorID, validUntil, callback) return; } } - + //ensure this is not a negativ number if(validUntil < 0) { callback(new customError("validUntil is a negativ number","apierror")); return; } - + //ensure this is not a float value if(!is_int(validUntil)) { callback(new customError("validUntil is a float value","apierror")); return; } - + //check if validUntil is in the future if(Math.floor(new Date().getTime()/1000) > validUntil) { callback(new customError("validUntil is in the past","apierror")); 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 @@ -135,19 +135,19 @@ exports.createSession = function(groupID, authorID, validUntil, callback) db.get("group2sessions:" + groupID, function(err, group2sessions) { if(ERR(err, callback)) return; - + //the entry doesn't exist so far, let's create it - if(group2sessions == null) + if(!group2sessions) { group2sessions = {sessionIDs : {}}; } - + //add the entry for this session group2sessions.sessionIDs[sessionID] = 1; - + //save the new element back db.set("group2sessions:" + groupID, group2sessions); - + callback(); }); }, @@ -158,30 +158,30 @@ exports.createSession = function(groupID, authorID, validUntil, callback) db.get("author2sessions:" + authorID, function(err, author2sessions) { if(ERR(err, callback)) return; - + //the entry doesn't exist so far, let's create it - if(author2sessions == null) + if(!author2sessions) { author2sessions = {sessionIDs : {}}; } - + //add the entry for this session author2sessions.sessionIDs[sessionID] = 1; - + //save the new element back db.set("author2sessions:" + authorID, author2sessions); - + callback(); }); } ], function(err) { if(ERR(err, callback)) return; - + //return error and sessionID callback(null, {sessionID: sessionID}); - }) -} + }); +}; exports.getSessionInfo = function(sessionID, callback) { @@ -189,11 +189,11 @@ exports.getSessionInfo = function(sessionID, callback) db.get("session:" + sessionID, function (err, session) { if(ERR(err, callback)) return; - + //session does not exists - if(session == null) + if(session === null) { - callback(new customError("sessionID does not exist","apierror")) + callback(new customError("sessionID does not exist","apierror")); } //everything is fine, return the sessioninfos else @@ -201,7 +201,7 @@ exports.getSessionInfo = function(sessionID, callback) callback(null, session); } }); -} +}; /** * Deletes a session @@ -218,18 +218,18 @@ exports.deleteSession = function(sessionID, callback) db.get("session:" + sessionID, function (err, session) { if(ERR(err, callback)) return; - + //session does not exists - if(session == null) + if(!session) { - callback(new customError("sessionID does not exist","apierror")) + callback(new customError("sessionID does not exist","apierror")); } //everything is fine, return the sessioninfos else { authorID = session.authorID; groupID = session.groupID; - + callback(); } }); @@ -259,32 +259,32 @@ exports.deleteSession = function(sessionID, 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) { if(ERR(err, callback)) return; callback(); - }) -} + }); +}; exports.listSessionsOfGroup = function(groupID, callback) { groupMangager.doesGroupExist(groupID, function(err, exists) { if(ERR(err, callback)) return; - + //group does not exist - if(exists == false) + if(!exists) { callback(new customError("groupID does not exist","apierror")); } @@ -294,16 +294,16 @@ exports.listSessionsOfGroup = function(groupID, callback) listSessionsWithDBKey("group2sessions:" + groupID, callback); } }); -} +}; exports.listSessionsOfAuthor = function(authorID, callback) -{ +{ authorMangager.doesAuthorExists(authorID, function(err, exists) { if(ERR(err, callback)) return; - + //group does not exist - if(exists == false) + if(!exists) { callback(new customError("authorID does not exist","apierror")); } @@ -313,7 +313,7 @@ exports.listSessionsOfAuthor = function(authorID, callback) listSessionsWithDBKey("author2sessions:" + authorID, callback); } }); -} +}; //this function is basicly the code listSessionsOfAuthor and listSessionsOfGroup has in common function listSessionsWithDBKey (dbkey, callback) @@ -332,14 +332,14 @@ function listSessionsWithDBKey (dbkey, callback) }); }, function(callback) - { + { //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) { @@ -361,7 +361,7 @@ function listSessionsWithDBKey (dbkey, callback) /** * Generates a random String with the given length. Is needed to generate the SessionIDs */ -function randomString(len) +function randomString(len) { var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var randomstring = ''; @@ -375,6 +375,6 @@ function randomString(len) //checks if a number is an int function is_int(value) -{ - return (parseFloat(value) == parseInt(value)) && !isNaN(value) +{ + return (parseFloat(value) == parseInt(value, 10)) && !isNaN(value); }