From 9848a37b02ee25f1cb7488d4384f28d150494597 Mon Sep 17 00:00:00 2001 From: Jared Tabor Date: Tue, 6 Jan 2015 16:58:59 -0500 Subject: [PATCH] Force user to login before opening web sockets Rearranged the series of events so that the socket handshake happens after the user logs in. --- awx/ui/static/js/app.js | 173 +++++++++++++++------- awx/ui/static/js/lists/CustomInventory.js | 8 +- awx/ui/static/lib/ansible/AuthService.js | 2 + 3 files changed, 127 insertions(+), 56 deletions(-) diff --git a/awx/ui/static/js/app.js b/awx/ui/static/js/app.js index 0940ef32ab..d551d6af11 100644 --- a/awx/ui/static/js/app.js +++ b/awx/ui/static/js/app.js @@ -509,6 +509,67 @@ angular.module('Tower', [ $rootScope.browser = detectBrowser(); + if ($rootScope.removeOpenSocket) { + $rootScope.removeOpenSocket(); + } + $rootScope.removeOpenSocket = $rootScope.$on('OpenSocket', function() { + html = ""; + e = angular.element(document.getElementById('socket-beacon-div')); + e.empty().append(html); + $compile(e)($rootScope); + + e = angular.element(document.getElementById('socket-beacon-li')); + e.empty().append(html); + $compile(e)($rootScope); + + // Listen for job changes and issue callbacks to initiate + // DOM updates + function openSocket() { + sock = Socket({ scope: $rootScope, endpoint: "jobs" }); + sock.init(); + sock.on("status_changed", function(data) { + $log.debug('Job ' + data.unified_job_id + ' status changed to ' + data.status); + $rootScope.$emit('JobStatusChange', data); + }); + sock.on("summary_complete", function(data) { + $log.debug('Job summary_complete ' + data.unified_job_id); + $rootScope.$emit('JobSummaryComplete', data); + }); + sock.on("schedule_change", function(data) { + $log.debug('schedule changed to ' + data.status); + $rootScope.$emit('ScheduleChange', data); + }); + } + openSocket(); + + setTimeout(function() { + $rootScope.$apply(function() { + sock.checkStatus(); + $log.debug('socket status: ' + $rootScope.socketStatus); + }); + },2000); + + // monitor socket status + checkCount = 0; + setInterval(function() { + if (sock.checkStatus() === 'error' || checkCount > 5) { + // there's an error or we're stuck in a 'connecting' state. attempt to reconnect + $log.debug('socket status: ' + sock.checkStatus()); + $log.debug('attempting new socket connection'); + sock = null; + openSocket(); + checkCount = 0; + } + else if (sock.checkStatus() === 'connecting') { + checkCount++; + } + else { + checkCount = 0; + } + }, 5000); + }); + $rootScope.$on("$routeChangeStart", function (event, next) { // Before navigating away from current tab, make sure the primary view is visible if ($('#stream-container').is(':visible')) { @@ -605,63 +666,71 @@ angular.module('Tower', [ }); }; - html = ""; - e = angular.element(document.getElementById('socket-beacon-div')); - e.empty().append(html); - $compile(e)($rootScope); + // html = ""; + // e = angular.element(document.getElementById('socket-beacon-div')); + // e.empty().append(html); + // $compile(e)($rootScope); - e = angular.element(document.getElementById('socket-beacon-li')); - e.empty().append(html); - $compile(e)($rootScope); + // e = angular.element(document.getElementById('socket-beacon-li')); + // e.empty().append(html); + // $compile(e)($rootScope); - // Listen for job changes and issue callbacks to initiate - // DOM updates - function openSocket() { - sock = Socket({ scope: $rootScope, endpoint: "jobs" }); - sock.init(); - sock.on("status_changed", function(data) { - $log.debug('Job ' + data.unified_job_id + ' status changed to ' + data.status); - $rootScope.$emit('JobStatusChange', data); - }); - sock.on("summary_complete", function(data) { - $log.debug('Job summary_complete ' + data.unified_job_id); - $rootScope.$emit('JobSummaryComplete', data); - }); - sock.on("schedule_change", function(data) { - $log.debug('schedule changed to ' + data.status); - $rootScope.$emit('ScheduleChange', data); - }); - } + // // Listen for job changes and issue callbacks to initiate + // // DOM updates + // function openSocket() { + // sock = Socket({ scope: $rootScope, endpoint: "jobs" }); + // sock.init(); + // sock.on("status_changed", function(data) { + // $log.debug('Job ' + data.unified_job_id + ' status changed to ' + data.status); + // $rootScope.$emit('JobStatusChange', data); + // }); + // sock.on("summary_complete", function(data) { + // $log.debug('Job summary_complete ' + data.unified_job_id); + // $rootScope.$emit('JobSummaryComplete', data); + // }); + // sock.on("schedule_change", function(data) { + // $log.debug('schedule changed to ' + data.status); + // $rootScope.$emit('ScheduleChange', data); + // }); + // } - openSocket(); + // if ($rootScope.removeOpenSocket) { + // $rootScope.removeOpenSocket(); + // } + // $rootScope.removeOpenSocket = $rootScope.$on('OpenSocket', function() { - setTimeout(function() { - $rootScope.$apply(function() { - sock.checkStatus(); - $log.debug('socket status: ' + $rootScope.socketStatus); - }); - },2000); + // openSocket(); + + // setTimeout(function() { + // $rootScope.$apply(function() { + // sock.checkStatus(); + // $log.debug('socket status: ' + $rootScope.socketStatus); + // }); + // },2000); + + // // monitor socket status + // checkCount = 0; + // setInterval(function() { + // if (sock.checkStatus() === 'error' || checkCount > 5) { + // // there's an error or we're stuck in a 'connecting' state. attempt to reconnect + // $log.debug('socket status: ' + sock.checkStatus()); + // $log.debug('attempting new socket connection'); + // sock = null; + // openSocket(); + // checkCount = 0; + // } + // else if (sock.checkStatus() === 'connecting') { + // checkCount++; + // } + // else { + // checkCount = 0; + // } + // }, 5000); + // }); + + }); // end of 'ConfigReady' - // monitor socket status - checkCount = 0; - setInterval(function() { - if (sock.checkStatus() === 'error' || checkCount > 5) { - // there's an error or we're stuck in a 'connecting' state. attempt to reconnect - $log.debug('socket status: ' + sock.checkStatus()); - $log.debug('attempting new socket connection'); - sock = null; - openSocket(); - checkCount = 0; - } - else if (sock.checkStatus() === 'connecting') { - checkCount++; - } - else { - checkCount = 0; - } - }, 5000); - }); if (!$AnsibleConfig) { // there may be time lag when loading the config file, so temporarily use what's in local storage diff --git a/awx/ui/static/js/lists/CustomInventory.js b/awx/ui/static/js/lists/CustomInventory.js index c6b7ed7b8a..b37191399a 100644 --- a/awx/ui/static/js/lists/CustomInventory.js +++ b/awx/ui/static/js/lists/CustomInventory.js @@ -16,11 +16,11 @@ angular.module('CustomInventoryListDefinition', []) iterator: 'custom_inventory', selectTitle: 'Add custom inventory', editTitle: 'Custom Inventories', - selectInstructions: "

Select existing credentials by clicking each credential or checking the related checkbox. When " + - "finished, click the blue Select button, located bottom right.

Create a brand new credential by clicking " + - "the button.

esc or click to close
", + // selectInstructions: "

Select existing credentials by clicking each credential or checking the related checkbox. When " + + // "finished, click the blue Select button, located bottom right.

Create a brand new credential by clicking " + + // "the button.

esc or click to close
", index: false, - hover: true, + hover: false, fields: { name: { diff --git a/awx/ui/static/lib/ansible/AuthService.js b/awx/ui/static/lib/ansible/AuthService.js index 6da3ec3297..df50cc45a2 100644 --- a/awx/ui/static/lib/ansible/AuthService.js +++ b/awx/ui/static/lib/ansible/AuthService.js @@ -136,10 +136,12 @@ angular.module('AuthService', ['ngCookies', 'Utilities']) // store the response values in $rootScope so we can get to them later $rootScope.current_user = response.results[0]; $cookieStore.put('current_user', response.results[0]); //keep in session cookie in the event of browser refresh + $rootScope.$emit('OpenSocket'); }, restoreUserInfo: function () { $rootScope.current_user = $cookieStore.get('current_user'); + $rootScope.$emit('OpenSocket'); }, getUserInfo: function (key) {