From 82636716208a765fb63214d958cb7bc73fe19fc2 Mon Sep 17 00:00:00 2001 From: KIRAN KASHALKAR Date: Tue, 11 Apr 2017 23:00:06 -0400 Subject: [PATCH] RIFT-16172: Project switch does not affect polling and socket pages. Signed-off-by: KIRAN KASHALKAR --- skyquake/framework/core/api_utils/sockets.js | 1 - skyquake/framework/core/api_utils/utils.js | 13 ++++++- .../framework/core/modules/api/sessions.js | 39 ++++++++++++------- .../accounts/src/account/accountStore.js | 9 +++-- skyquake/skyquake.js | 2 +- 5 files changed, 43 insertions(+), 21 deletions(-) diff --git a/skyquake/framework/core/api_utils/sockets.js b/skyquake/framework/core/api_utils/sockets.js index e2e444735..2e6779c04 100644 --- a/skyquake/framework/core/api_utils/sockets.js +++ b/skyquake/framework/core/api_utils/sockets.js @@ -92,7 +92,6 @@ Subscriptions.prototype.subscribe = function(req, callback) { var a = url.resolve(origin, req.baseUrl); var b = url.resolve(a, URL); URL = b; - console.log('DEBUG URL IS', URL); } else { protocol = protocol[1] } diff --git a/skyquake/framework/core/api_utils/utils.js b/skyquake/framework/core/api_utils/utils.js index 185c7c6e3..06a4e7263 100644 --- a/skyquake/framework/core/api_utils/utils.js +++ b/skyquake/framework/core/api_utils/utils.js @@ -50,8 +50,17 @@ var confdPort = function(api_server) { }; var projectContextUrl = function(req, url) { - if (req.session && req.session.projectId) { - return url.replace(/(\/api\/operational\/|\/api\/config\/|\/api\/operations\/)(.*)/, '$1project/' + req.session.projectId + '/$2'); + //NOTE: We need to go into the sessionStore because express-session + // does not reliably update the session. + // See https://github.com/expressjs/session/issues/450 + var projectId = (req.session && + req.sessionStore && + req.sessionStore.sessions && + req.sessionStore.sessions[req.session.id] && + JSON.parse(req.sessionStore.sessions[req.session.id])['projectId']) || + (null); + if (projectId) { + return url.replace(/(\/api\/operational\/|\/api\/config\/|\/api\/operations\/)(.*)/, '$1project/' + projectId + '/$2'); } return url; diff --git a/skyquake/framework/core/modules/api/sessions.js b/skyquake/framework/core/modules/api/sessions.js index 2036030d5..9583241a4 100644 --- a/skyquake/framework/core/modules/api/sessions.js +++ b/skyquake/framework/core/modules/api/sessions.js @@ -33,10 +33,11 @@ var base64 = require('base-64'); var APIVersion = '/v2'; var configurationAPI = require('./configuration'); -function logAndReject(mesg, reject) { +function logAndReject(mesg, reject, errCode) { res.errorMessage = { error: mesg } + res.statusCode = errCode || constants.HTTP_RESPONSE_CODES.ERROR.BAD_REQUEST; console.log(mesg); reject(res); } @@ -116,8 +117,8 @@ sessionsAPI.create = function(req, res) { username: username, // project: req.session.projectId }; - var successMsg = 'User =>' + username + ' successfully logged in.'; - successMsg += req.session.projectId ? 'Project =>' + req.session.projectId + ' set as default.' : ''; + var successMsg = 'User => ' + username + ' successfully logged in.'; + successMsg += req.session.projectId ? 'Project => ' + req.session.projectId + ' set as default.' : ''; console.log(successMsg); @@ -127,6 +128,13 @@ sessionsAPI.create = function(req, res) { status: successMsg }) }; + + req.session.save(function(err) { + if (err) { + console.log('Error saving session to store', err); + } + }) + resolve(response); }).catch(function(error) { @@ -142,19 +150,24 @@ sessionsAPI.addProjectToSession = function(req, res) { return new Promise(function(resolve, reject) { if (req.session && req.session.loggedIn == true) { req.session.projectId = req.params.projectId; - var successMsg = 'Added project' + req.session.projectId + ' to session' + req.sessionID; - console.log(successMsg); + req.session.save(function(err) { + if (err) { + console.log('Error saving session to store', err); + } + var successMsg = 'Added project ' + req.session.projectId + ' to session ' + req.sessionID; + console.log(successMsg); + + return resolve ({ + statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK, + data: JSON.stringify({ + status: successMsg + }) + }); - return resolve ({ - statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK, - data: JSON.stringify({ - status: successMsg - }) + var errorMsg = 'Session does not exist or not logged in'; + logAndReject(errorMsg, reject, constants.HTTP_RESPONSE_CODES.ERROR.NOT_FOUND); }); } - - var errorMsg = 'Session does not exist or not logged in'; - logAndReject(errorMsg, reject); }); } diff --git a/skyquake/plugins/accounts/src/account/accountStore.js b/skyquake/plugins/accounts/src/account/accountStore.js index aeb5f4817..22e8a17a9 100644 --- a/skyquake/plugins/accounts/src/account/accountStore.js +++ b/skyquake/plugins/accounts/src/account/accountStore.js @@ -21,6 +21,7 @@ import AccountSource from './accountSource.js'; var Utils = require('utils/utils.js'); var rw = require('utils/rw.js'); var altImage = rw.getSearchParams(window.location).alt_image; +var _ = require('lodash'); let Params = { //Config Agent @@ -291,7 +292,7 @@ export default class AccountStore { if(self.currentAccount) { let Account = self.getAccountFromStream(data[self.currentAccount.type].data, self.currentAccount.name); newState.account = self.account; - newState.account['connection-status'] = Account['connection-status'] + newState.account['connection-status'] = Account && Account['connection-status'] } self.setState(newState) } catch(error) { @@ -332,7 +333,7 @@ export default class AccountStore { } getAccountFromStream(data, name) { let result = null; - data.map(function(a) { + data && _.isArray(data) && data.map(function(a) { if(a.name == name) { result = a; } @@ -342,7 +343,7 @@ export default class AccountStore { viewAccount = ({type, name}) => { var data = null; var accounts = null; - if(this && this[type].length) { + if(this && this[type] && this[type].length) { accounts = this[type]; data = this.getAccountFromStream(accounts, name); if(data) { @@ -364,7 +365,7 @@ export default class AccountStore { } generateOptionsByName(data) { let results = []; - if (data && data.constructor.name == "Array") { + if (data && _.isArray(data)) { data.map(function(d) { results.push({ label: d.name, diff --git a/skyquake/skyquake.js b/skyquake/skyquake.js index 4c5adcf93..2396e1a17 100644 --- a/skyquake/skyquake.js +++ b/skyquake/skyquake.js @@ -110,7 +110,7 @@ if (cluster.isMaster && clusteredLaunch) { app.use(session({ secret: 'ritio rocks', - resave: true, + resave: false, saveUninitialized: true })); app.use(bodyParser.json()); -- 2.25.1