RIFT-16172: Project switch does not affect polling and socket pages.
authorKIRAN KASHALKAR <kiran.kashalkar@riftio.com>
Wed, 12 Apr 2017 03:00:06 +0000 (23:00 -0400)
committerKIRAN KASHALKAR <kiran.kashalkar@riftio.com>
Wed, 12 Apr 2017 03:00:06 +0000 (23:00 -0400)
Signed-off-by: KIRAN KASHALKAR <kiran.kashalkar@riftio.com>
skyquake/framework/core/api_utils/sockets.js
skyquake/framework/core/api_utils/utils.js
skyquake/framework/core/modules/api/sessions.js
skyquake/plugins/accounts/src/account/accountStore.js
skyquake/skyquake.js

index e2e4447..2e6779c 100644 (file)
@@ -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]
   }
index 185c7c6..06a4e72 100644 (file)
@@ -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;
index 2036030..9583241 100644 (file)
@@ -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);
     });
 }
 
index aeb5f48..22e8a17 100644 (file)
@@ -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,
index 4c5adcf..2396e1a 100644 (file)
@@ -110,7 +110,7 @@ if (cluster.isMaster && clusteredLaunch) {
 
        app.use(session({
          secret: 'ritio rocks',
-         resave: true,
+         resave: false,
          saveUninitialized: true
        }));
        app.use(bodyParser.json());