From ca8847c5f0dbc89a257384f2c156f61ddea512fa Mon Sep 17 00:00:00 2001 From: Marti Martz Date: Sun, 1 Jul 2018 23:09:04 -0600 Subject: [PATCH] Refactor to existing code style (#1479) * Should be functionally equivalent to the inline but using current structures e.g. match project code style placements. Needed for next stage. * Added one override on `parseDateProperty` since `date` sometimes gets written to a flat string object instead of `Date`. NOTE: These are no longer sorted as that's coming up in the future... although may reinstate if lack of time Post #1446 ... related to #604 --- controllers/admin.js | 110 ++++++------------------------------ libs/modelParser.js | 52 +++++++++++++++++ libs/modifySessions.js | 49 ++++++++++++++++ views/includes/session.html | 26 ++++----- 4 files changed, 130 insertions(+), 107 deletions(-) diff --git a/controllers/admin.js b/controllers/admin.js index 700c36d69..c261b7ed7 100644 --- a/controllers/admin.js +++ b/controllers/admin.js @@ -13,7 +13,6 @@ var exec = require('child_process').exec; var async = require('async'); var _ = require('underscore'); var moment = require('moment'); -var momentDurationFormatSetup = require("moment-duration-format"); var git = require('git-rev'); var useragent = require('useragent'); @@ -40,6 +39,7 @@ var loadPassport = require('../libs/passportLoader').loadPassport; var strategyInstances = require('../libs/passportLoader').strategyInstances; var statusCodePage = require('../libs/templateHelpers').statusCodePage; var updateSessions = require('../libs/modifySessions').update; +var listDataSessions = require('../libs/modifySessions').listData; var pageMetadata = require('../libs/templateHelpers').pageMetadata; //--- Configuration inclusions @@ -48,9 +48,6 @@ var userRoles = require('../models/userRoles.json'); var strategies = require('./strategies.json'); -// Initializations -momentDurationFormatSetup(moment); - //--- // This controller is only for use by users with a role of admin or above @@ -400,107 +397,32 @@ exports.adminSessionActiveView = function (aReq, aRes, aNext) { tasks.push(function (aCallback) { - sessionColl.find({ - }, function (aErr, aUserSessions) { + listDataSessions(store, options, function (aErr) { if (aErr) { statusCodePage(aReq, aRes, aNext, { - statusCode: 520, - statusMessage: 'Unknown error with `sessionColl.find`.' - }); - return; - } - - if (!aUserSessions) { - statusCodePage(aReq, aRes, aNext, { - statusCode: 200, - statusMessage: 'No sessions' + statusCode: 500, + statusMessage: aErr }); return; } - aUserSessions.toArray(function (aErr, aSessionsData) { - options.sessionList = []; - - if (aErr) { - // We want to know what the error value is in logging review - console.error('Unknown error with `toArray`.\n' + aErr); + options.sessionList = _.map(options.sessionList, function (aSession) { + var session = modelParser.parseSession(aSession); - statusCodePage(aReq, aRes, aNext, { - statusCode: 520, - statusMessage: 'Unknown error with `toArray`.' - }); - return; - } - - aSessionsData.forEach(function (aElement, aIndex) { - var data = JSON.parse(aElement.session); - - var user = null; - var username = null; - var cookie = null; - var oujsOptions = {}; - - var obj = null; - - if (data) { - user = data.user || {}; - username = user.name || data.username; - cookie = data.cookie || {}; - - if (data.passport && data.passport.oujsOptions) { - oujsOptions = data.passport.oujsOptions; - } - - obj = { - _id: aElement._id, - name: username, - role: userRoles[user.role], - strategy: oujsOptions.strategy, - canDestroyOne: true, // TODO: Perhaps do some further conditionals - remoteAddress: ( - username === authedUser.name && !oujsOptions.authFrom - ? oujsOptions.remoteAddress - : oujsOptions.authFrom - ? oujsOptions.authFrom - : null - ), - ua: { - raw: oujsOptions.userAgent, - class: 'fa-lg ua-' + useragent.parse(oujsOptions.userAgent) - .family.toLowerCase().replace(/\s+/g, '-') - }, - userPageUrl: user.userPageUrl, - cookie: { - since: oujsOptions.since ? new Date(oujsOptions.since) : oujsOptions.since, - expires: (cookie.expires ? new Date(cookie.expires) : false), - originalMaxAge: (cookie.originalMaxAge - ? moment.duration(cookie.originalMaxAge, "milliseconds") - .format('YYYY[y]DDD[d]H[h]m[m]', { trim: 'both' }) - : false), - secure: cookie.secure, - httpOnly: cookie.httpOnly, - sameSite: cookie.sameSite, - sameSiteStrict: cookie.sameSite === 'strict', - sameSiteLax: cookie.sameSite === 'lax' , - } - }; - - modelParser.parseDateProperty(obj.cookie, 'expires'); - modelParser.parseDateProperty(obj.cookie, 'since'); - - options.sessionList.push(obj); - } - }); + var oujsOptions = session.passport.oujsOptions; - // Sort the sessions for now - options.sessionList = _.sortBy(options.sessionList, function (aSession) { - return (aSession.name) ? aSession.name.toLowerCase() : aSession.name; - }); + session.canDestroyOne = true; // TODO: Perhaps do some further conditionals - aCallback(); + oujsOptions.remoteAddressMask = session.name === authedUser.name && !oujsOptions.authFrom + ? oujsOptions.remoteAddressMask + : oujsOptions.authFrom + ? oujsOptions.authFrom + : null; + return session; }); - }); + aCallback(); + }); }); //--- diff --git a/libs/modelParser.js b/libs/modelParser.js index c2eab4c77..eca66c513 100644 --- a/libs/modelParser.js +++ b/libs/modelParser.js @@ -9,6 +9,7 @@ var isDbg = require('../libs/debug').isDbg; //--- Dependency inclusions var moment = require('moment'); +var momentDurationFormatSetup = require("moment-duration-format"); var _ = require('underscore'); var util = require('util'); var useragent = require('useragent'); @@ -30,6 +31,9 @@ var isFQUrl = require('../libs/helpers').isFQUrl; //--- Configuration inclusions var userRoles = require('../models/userRoles.json'); +// --- Initializations +momentDurationFormatSetup(moment); + //--- var parseModelFnMap = {}; @@ -94,6 +98,7 @@ var parseDateProperty = function (aObj, aKey) { if (aObj[aKey]) { date = new Date(aObj[aKey]); if (date) { + aObj[aKey] = date; aObj[aKey + 'ISOFormat'] = date.toISOString(); aObj[aKey + 'Humanized'] = moment(date).locale('en-tiny').calendar(); } @@ -101,6 +106,15 @@ var parseDateProperty = function (aObj, aKey) { }; exports.parseDateProperty = parseDateProperty; +var parseDurationProperty = function (aObj, aKey, aUnits) { + if (aObj[aKey]) { + aObj[aKey + 'Humanized'] = moment.duration(aObj[aKey], aUnits || 'milliseconds') + .format('YYYY[y]DDD[d]H[h]m[m]', { trim: 'both' }); + } +}; +exports.parseDurationProperty = parseDurationProperty; + + // Parse persisted model data and return a new object // with additional generated fields used in view templates. @@ -880,3 +894,41 @@ var parseRemovedItem = function (aRemovedItem) { }; parseModelFnMap.Remove = parseRemovedItem; exports.parseRemovedItem = parseRemovedItem; + + +/** + * Session + */ +var parseSession = function (aSession) { + var session = null; + + var cookie = null; + var oujsOptions = null; + + if (!aSession) { + return; + } + + session = aSession.toObject ? aSession.toObject() : aSession; + + // + cookie = session.cookie; + oujsOptions = session.passport.oujsOptions; + + + session.name = session.user ? session.user.name : oujsOptions.username; + + oujsOptions.remoteAddressMask = oujsOptions.remoteAddress; + oujsOptions.userAgentFamily = useragent + .parse(oujsOptions.userAgent).family.toLowerCase().replace(/\s+/g, '-'); + parseDateProperty(oujsOptions, 'since'); + + cookie.sameSiteStrict = cookie.sameSite === 'strict'; + cookie.sameSiteLax = cookie.sameSite === 'lax'; + parseDateProperty(cookie, 'expires'); + parseDurationProperty(cookie, 'originalMaxAge'); + + return session; +}; +parseModelFnMap.Session = parseSession; +exports.parseSession = parseSession; diff --git a/libs/modifySessions.js b/libs/modifySessions.js index 644181839..9bc0dc226 100644 --- a/libs/modifySessions.js +++ b/libs/modifySessions.js @@ -199,3 +199,52 @@ exports.destroy = function (aReq, aUser, aCallback) { store.destroy(aId, aInnerCallback); }, aCallback); }; + +exports.listData = function (aStore, aOptions, aCallback) { + var sessionColl = aStore.db.collection('sessions'); + + sessionColl.find({ + }, function (aErr, aUserSessions) { + if (aErr) { + aCallback(aErr); + return; + } + + if (!aUserSessions) { + aCallback('No sessions'); + return; + } + + aUserSessions.toArray(function (aErr, aSessionsData) { + aOptions.sessionList = []; + + if (aErr) { + aCallback(aErr); + return; + } + + aSessionsData.forEach(function (aElement, aIndex) { + var data = JSON.parse(aElement.session); + + if (data) { + + if (!data.passport) { + data.passport = {}; + } + + if (!data.passport.oujsOptions) { + data.passport.oujsOptions = {}; + } + + data.passport.oujsOptions.username = data.username; + data.passport.oujsOptions.sid = aElement._id; + } + + aOptions.sessionList.push(data); + }); + + aCallback(); + }); + }); +} + diff --git a/views/includes/session.html b/views/includes/session.html index cd1c25c7c..6f5501eb0 100644 --- a/views/includes/session.html +++ b/views/includes/session.html @@ -2,16 +2,16 @@
- {{#userPageUrl}}{{/userPageUrl}} - {{^userPageUrl}}{{/userPageUrl}} + {{#user.userPageUrl}}{{/user.userPageUrl}} + {{^user.userPageUrl}}{{/user.userPageUrl}}
@@ -45,7 +45,7 @@
- +