Merge "Avoid forced logout when 403 error encountered" into stable/queens
This commit is contained in:
commit
560ffbbb74
|
@ -25,7 +25,7 @@
|
||||||
.run(run)
|
.run(run)
|
||||||
.factory('horizon.framework.redirect', httpRedirectLogin)
|
.factory('horizon.framework.redirect', httpRedirectLogin)
|
||||||
.constant('horizon.framework.events', {
|
.constant('horizon.framework.events', {
|
||||||
FORCE_LOGOUT: 'FORCE_LOGOUT'
|
AUTH_ERROR: 'AUTH_ERROR'
|
||||||
});
|
});
|
||||||
|
|
||||||
config.$inject = [
|
config.$inject = [
|
||||||
|
@ -128,25 +128,28 @@
|
||||||
responseError: function (error) {
|
responseError: function (error) {
|
||||||
if (error.status === 401) {
|
if (error.status === 401) {
|
||||||
var msg = gettext('Unauthorized. Redirecting to login');
|
var msg = gettext('Unauthorized. Redirecting to login');
|
||||||
handleRedirectMessage(msg, $rootScope, $window, frameworkEvents, toastService);
|
handleRedirectMessage(msg, $rootScope, $window, frameworkEvents, toastService, true);
|
||||||
}
|
}
|
||||||
if (error.status === 403) {
|
if (error.status === 403) {
|
||||||
var msg2 = gettext('Forbidden. Redirecting to login');
|
var msg2 = gettext('Forbidden. Insufficient permissions of the requested operation');
|
||||||
handleRedirectMessage(msg2, $rootScope, $window, frameworkEvents, toastService);
|
handleRedirectMessage(msg2, $rootScope, $window, frameworkEvents, toastService, false);
|
||||||
}
|
}
|
||||||
return $q.reject(error);
|
return $q.reject(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleRedirectMessage(msg, $rootScope, $window, frameworkEvents, toastService) {
|
function handleRedirectMessage(
|
||||||
|
msg, $rootScope, $window, frameworkEvents, toastService, forceLogout) {
|
||||||
var toast = toastService.find('error', msg);
|
var toast = toastService.find('error', msg);
|
||||||
//Suppress the multiple duplicate redirect toast messages.
|
//Suppress the multiple duplicate redirect toast messages.
|
||||||
if (!toast) {
|
if (!toast) {
|
||||||
toastService.add('error', msg);
|
toastService.add('error', msg);
|
||||||
$rootScope.$broadcast(frameworkEvents.FORCE_LOGOUT, msg);
|
$rootScope.$broadcast(frameworkEvents.AUTH_ERROR, msg);
|
||||||
|
}
|
||||||
|
if (forceLogout) {
|
||||||
|
$window.location.replace($window.WEBROOT + 'auth/logout');
|
||||||
}
|
}
|
||||||
$window.location.replace($window.WEBROOT + 'auth/logout');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -56,7 +56,7 @@
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when forbidden', function() {
|
describe('when forbidden', function() {
|
||||||
it('should redirect to /auth/logout and add a forbidden toast message ', inject(
|
it('should add a forbidden toast message ', inject(
|
||||||
function($http, $httpBackend, $window, $injector, $rootScope) {
|
function($http, $httpBackend, $window, $injector, $rootScope) {
|
||||||
$window.WEBROOT = '/dashboard/';
|
$window.WEBROOT = '/dashboard/';
|
||||||
$httpBackend.when('GET', '/api').respond(403, '');
|
$httpBackend.when('GET', '/api').respond(403, '');
|
||||||
|
@ -69,7 +69,6 @@
|
||||||
$http.get('/api').error(function() {
|
$http.get('/api').error(function() {
|
||||||
expect(toastService.add).toHaveBeenCalled();
|
expect(toastService.add).toHaveBeenCalled();
|
||||||
expect($rootScope.$broadcast).toHaveBeenCalled();
|
expect($rootScope.$broadcast).toHaveBeenCalled();
|
||||||
expect($window.location.replace).toHaveBeenCalledWith('/dashboard/auth/logout');
|
|
||||||
});
|
});
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
})
|
})
|
||||||
|
|
|
@ -142,7 +142,7 @@
|
||||||
$scope.$broadcast(wizardEvents.ON_INIT_ERROR);
|
$scope.$broadcast(wizardEvents.ON_INIT_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
$scope.$on(frameworkEvents.FORCE_LOGOUT, function(evt, arg) {
|
$scope.$on(frameworkEvents.AUTH_ERROR, function(evt, arg) {
|
||||||
viewModel.hasError = true;
|
viewModel.hasError = true;
|
||||||
viewModel.errorMessage = arg;
|
viewModel.errorMessage = arg;
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -62,14 +62,14 @@
|
||||||
expect(scope.viewModel.errorMessage).toBe('in valid');
|
expect(scope.viewModel.errorMessage).toBe('in valid');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('call onInitSuccess with logout event', function() {
|
it('call onInitSuccess with auth_error event', function() {
|
||||||
rootScope.$broadcast(frameworkEvents.FORCE_LOGOUT, 'logout');
|
rootScope.$broadcast(frameworkEvents.AUTH_ERROR, 'auth_error');
|
||||||
ctrl.onInitSuccess();
|
ctrl.onInitSuccess();
|
||||||
scope.$apply();
|
scope.$apply();
|
||||||
expect(scope.viewModel.hasError).toBe(true);
|
expect(scope.viewModel.hasError).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('call onInitSuccess without logout event', function() {
|
it('call onInitSuccess without auth_error event', function() {
|
||||||
spyOn(scope, '$broadcast');
|
spyOn(scope, '$broadcast');
|
||||||
ctrl.onInitSuccess();
|
ctrl.onInitSuccess();
|
||||||
scope.$apply();
|
scope.$apply();
|
||||||
|
@ -77,8 +77,8 @@
|
||||||
expect(scope.$broadcast).toHaveBeenCalledWith(wizardEvents.ON_INIT_SUCCESS);
|
expect(scope.$broadcast).toHaveBeenCalledWith(wizardEvents.ON_INIT_SUCCESS);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('call onInitError with logout event', function() {
|
it('call onInitError with auth_error event', function() {
|
||||||
rootScope.$broadcast(frameworkEvents.FORCE_LOGOUT, 'logout');
|
rootScope.$broadcast(frameworkEvents.AUTH_ERROR, 'auth_error');
|
||||||
ctrl.onInitError();
|
ctrl.onInitError();
|
||||||
scope.$apply();
|
scope.$apply();
|
||||||
expect(scope.viewModel.hasError).toBe(true);
|
expect(scope.viewModel.hasError).toBe(true);
|
||||||
|
|
Loading…
Reference in New Issue