Skip to content

Commit

Permalink
Some Code styling and refactor (#1410)
Browse files Browse the repository at this point in the history
* Make this code look more like the others and place some in sessions lib
* Remove some unused identifiers from prior patch
* Retag some comments a bit
* Some STYLEGUIDE.md compliance
* Stop slinging around some functions on return again

Post #1393

Auto-merge
  • Loading branch information
Martii committed Jun 8, 2018
1 parent 6d72dec commit b48dd88
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
39 changes: 15 additions & 24 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ var countTask = require('../libs/tasks').countTask;
var pageMetadata = require('../libs/templateHelpers').pageMetadata;
var orderDir = require('../libs/templateHelpers').orderDir;

var extendSession = require('../libs/modifySessions').extend;

//--- Configuration inclusions
var userRoles = require('../models/userRoles.json');
var strategies = require('./strategies.json');
Expand Down Expand Up @@ -115,7 +117,6 @@ exports.exist = function (aReq, aRes) {

// API - Request for extending a logged in user session
exports.extend = function (aReq, aRes, aNext) {
//
var authedUser = aReq.session.user;

if (!authedUser) {
Expand All @@ -127,32 +128,22 @@ exports.extend = function (aReq, aRes, aNext) {
_id: authedUser._id,
sessionsIds: { "$in": [ aReq.session._id ] }
}, function (aErr, aUser) {
var options = {};
var user = null;

if (aErr) {
console.error(aErr);
statusCodePage(aReq, aRes, aNext, {
statusCode: 500,
statusMessage: 'Server Error'
});
return;
}

if (!aUser) {
aNext();
return;
}
extendSession(aReq, aUser, function (aErr) {
if (aErr) {
if (aErr === 'Already extended') {
aNext();
return;
}

if (aReq.session.cookie.expires) {
aReq.session.cookie.expires = false;
aReq.session.save();
statusCodePage(aReq, aRes, aNext, {
statusCode: 500,
statusMessage: aErr
});
return;
}

aRes.redirect('back');
} else {
aNext();
return;
}
});
});
};

Expand Down
36 changes: 31 additions & 5 deletions libs/modifySessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function serializeUser(aUser) {
return userObj;
}

// Add a new session id to the user model
// Add a single new session id to the user model
exports.add = function (aReq, aUser, aCallback) {
var store = aReq.sessionStore;

Expand Down Expand Up @@ -62,7 +62,26 @@ exports.add = function (aReq, aUser, aCallback) {
}
};

// Remove a session id from the user model **and** the session store
// Extend a single session
exports.extend = function (aReq, aUser, aCallback) {
if (!aUser) {
aCallback('No User');
return;
}

if (!aReq.session.cookie.expires) {
aCallback('Already extended');
return;
}

// NOTE: Currently allow on any session with
// no additional User restrictions yet

aReq.session.cookie.expires = false;
aReq.session.save(aCallback);
};

// Remove a single session id from the user model **and** the session store
exports.remove = function (aReq, aUser, aCallback) {
var pos = aUser && aUser.sessionIds ?
aUser.sessionIds.indexOf(aReq.sessionID) : -1;
Expand All @@ -86,13 +105,17 @@ exports.update = function (aReq, aUser, aCallback) {
var store = aReq.sessionStore;
var userObj = aUser ? serializeUser(aUser) : null;

if (!aUser || !aUser.sessionIds) { return aCallback('No sessions', null); }
if (!aUser || !aUser.sessionIds) {
aCallback('No sessions', null);
return;
}

async.each(aUser.sessionIds, function (aId, aCb) {
store.get(aId, function (aErr, aSess) {
// Invalid session, will be removed on login
if (aErr || !aSess) {
return aCb(null);
aCb(null);
return;
}

aSess.user = userObj;
Expand All @@ -113,7 +136,10 @@ exports.destroy = function (aReq, aUser, aCallback) {
}
};

if (!aUser || !aUser.sessionIds) { return aCallback('No sessions', null); }
if (!aUser || !aUser.sessionIds) {
aCallback('No sessions', null);
return;
}

async.each(aUser.sessionIds, function (aId, aCb) {
store.set(aId, emptySess, aCb);
Expand Down

0 comments on commit b48dd88

Please sign in to comment.