Wrapping timer init in promise

to ensure that timer is started before initializing websockets. This was leading to instances where the websocket thought that the session
hadn't been started yet. Also fixed an issue where the session wasn't tearing down the $interval when AUTH_TOKEN_PER_USER had been exceeded.
This commit is contained in:
Jared Tabor 2015-10-27 13:51:54 -07:00
parent 80eeda1c8c
commit 6c8597cf95
5 changed files with 29 additions and 16 deletions

View File

@ -947,7 +947,8 @@ var tower = angular.module('Tower', [
control_socket.init();
control_socket.on("limit_reached", function(data) {
$log.debug(data.reason);
Timer.expireSession('session_limit');
$rootScope.sessionTimer.expireSession('session_limit');
$location.url('/login');
});
}
openSocket();
@ -1023,9 +1024,11 @@ var tower = angular.module('Tower', [
$rootScope.user_is_superuser = Authorization.getUserInfo('is_superuser');
// when the user refreshes we want to open the socket, except if the user is on the login page, which should happen after the user logs in (see the AuthService module for that call to OpenSocket)
if(!_.contains($location.$$url, '/login')){
$rootScope.sessionTimer = Timer.init();
$rootScope.$emit('OpenSocket');
pendoService.issuePendoIdentity();
Timer.init().then(function(timer){
$rootScope.sessionTimer = timer;
$rootScope.$emit('OpenSocket');
pendoService.issuePendoIdentity();
});
}
}

View File

@ -82,7 +82,7 @@ export default
x = Store('sessionTime');
x[$rootScope.current_user.id].loggedIn = false;
Store('sessionTime', x);
$rootScope.lastUser = $cookieStore.get('current_user').id;
$cookieStore.remove('token_expires');
$cookieStore.remove('current_user');
@ -98,7 +98,7 @@ export default
$rootScope.token_expires = null;
$rootScope.login_username = null;
$rootScope.login_password = null;
$rootScope.sessionTimer.expireSession();
$rootScope.sessionTimer.clearTimers();
},
getLicense: function () {
@ -153,7 +153,6 @@ export default
// 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 () {

View File

@ -23,9 +23,9 @@
*/
export default
['$rootScope', '$cookieStore', 'transitionTo', 'CreateDialog', 'Authorization',
'Store', '$interval',
'Store', '$interval', '$location', '$q',
function ($rootScope, $cookieStore, transitionTo, CreateDialog, Authorization,
Store, $interval) {
Store, $interval, $location, $q) {
return {
sessionTime: null,
@ -82,7 +82,6 @@ export default
this.sessionTime = 0;
this.clearTimers();
$cookieStore.put('sessionExpired', true);
transitionTo('signOut');
},
moveForward: function () {
@ -158,9 +157,12 @@ export default
$('#idle-modal').dialog('close');
}
that.expireSession('idle');
$location.url('/login');
}
if(Store('sessionTime')[$rootScope.current_user.id].loggedIn === false){
that.expireSession();
$location.url('/login');
}
}, 1000);
@ -169,11 +171,15 @@ export default
clearTimers: function(){
$interval.cancel($rootScope.expireTimer);
delete $rootScope.expireTimer;
},
init: function () {
var deferred = $q.defer();
this.moveForward();
return this;
deferred.resolve(this);
return deferred.promise;
}
};
}

View File

@ -165,9 +165,12 @@ export default ['$log', '$cookieStore', '$compile', '$window', '$rootScope', '$l
Authorization.getUser()
.success(function (data) {
Authorization.setUserInfo(data);
$rootScope.sessionTimer = Timer.init();
$rootScope.user_is_superuser = data.results[0].is_superuser;
scope.$emit('AuthorizationGetLicense');
Timer.init().then(function(timer){
$rootScope.sessionTimer = timer;
$rootScope.$emit('OpenSocket');
$rootScope.user_is_superuser = data.results[0].is_superuser;
scope.$emit('AuthorizationGetLicense');
});
})
.error(function (data, status) {
Authorization.logout();

View File

@ -11,8 +11,8 @@
*************************************************/
export default
[ '$rootScope', '$q',
function ($rootScope, $q) {
[ '$rootScope', '$q', '$injector',
function ($rootScope, $q, $injector) {
return {
response: function(config) {
if(config.headers('auth-token-timeout') !== null){
@ -23,6 +23,8 @@
responseError: function(rejection){
if( rejection.data && !_.isEmpty(rejection.data.detail) && rejection.data.detail === "Maximum per-user sessions reached"){
$rootScope.sessionTimer.expireSession('session_limit');
var location = $injector.get('$location');
location.url('/login');
return $q.reject(rejection);
}
return $q.reject(rejection);