Skip to content

Commit

Permalink
#1316 Implement the switch org feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nadouani committed May 19, 2020
1 parent bda2247 commit e130c3d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 22 deletions.
27 changes: 16 additions & 11 deletions frontend/app/scripts/controllers/RootCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Controller for main page
*/
angular.module('theHiveControllers').controller('RootCtrl',
function($scope, $rootScope, $uibModal, $location, $state, AuthenticationSrv, AlertingSrv, StreamSrv, StreamStatSrv, CaseSrv, CaseTemplateSrv, CustomFieldsSrv, NotificationSrv, AppLayoutSrv, VersionSrv, currentUser, appConfig) {
function($scope, $rootScope, $timeout, $uibModal, $location, $state, AuthenticationSrv, AlertingSrv, StreamSrv, StreamStatSrv, CaseSrv, CaseTemplateSrv, CustomFieldsSrv, NotificationSrv, AppLayoutSrv, VersionSrv, currentUser, appConfig) {
'use strict';

if(currentUser === 520) {
Expand Down Expand Up @@ -145,24 +145,29 @@ angular.module('theHiveControllers').controller('RootCtrl',

modal.result
.then(function(organisation) {
console.log(organisation);

$rootScope.isLoading = true;

AuthenticationSrv.current(organisation)
.then(function(/*userData*/) {
$rootScope.isLoading = false;
$state.go('app.index');
})
.catch( function(err) {
NotificationSrv.error('App', err.data, err.status);
$rootScope.isLoading = false;
return AuthenticationSrv.current(organisation)
.then(function(userData) {
$scope.currentUser = userData;
StreamSrv.cancelPoll();
});
})
.then(function() {
$state.go('app.index', {}, {reload:true});
})
.catch(function(err) {
if(err && !_.isString(err)) {
NotificationSrv.error('Switch organisation', err.data, err.status);
}

NotificationSrv.error('App', err.data, err.status);
})
.finally(function() {
$timeout(function() {
$rootScope.isLoading = false;
}, 500);

});

};
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/scripts/directives/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
scope.$watch('user', function(value) {
if(!value) {
return;
}
}
scope.userInfo(value).then(function(userData) {
scope.userData = userData;
scope.userData = userData;
});
});
}
Expand Down
9 changes: 6 additions & 3 deletions frontend/app/scripts/services/api/AuthenticationSrv.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';
angular
.module('theHiveServices')
.factory('AuthenticationSrv', function($http, $q, UtilsSrv, SecuritySrv) {
.factory('AuthenticationSrv', function($http, $q, UtilsSrv, SecuritySrv, UserSrv) {
var self = {
currentUser: null,
login: function(username, password, code) {
Expand Down Expand Up @@ -46,8 +46,11 @@
return $http
.get('./api/v1/user/current', options)
.then(function(response) {
self.currentUser = response.data;
UtilsSrv.shallowClearAndCopy(response.data, result);
var userData = response.data;

self.currentUser = userData;
UserSrv.updateCache(userData.login, userData);
UtilsSrv.shallowClearAndCopy(userData, result);

return $q.resolve(result);
})
Expand Down
29 changes: 25 additions & 4 deletions frontend/app/scripts/services/common/data/StreamSrv.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
(function() {
'use strict';
angular.module('theHiveServices').factory('StreamSrv', function($rootScope, $http, $timeout, UserSrv, AuthenticationSrv, AfkSrv, NotificationSrv) {
angular.module('theHiveServices').factory('StreamSrv', function($q, $rootScope, $http, $timeout, UserSrv, AuthenticationSrv, AfkSrv, NotificationSrv) {

var self = {
isPolling: false,
streamId: null,
httpRequestCanceller: $q.defer(),
disabled: true,

init: function() {
self.streamId = null;
self.disabled = false;
self.requestStream();
},

Expand Down Expand Up @@ -82,6 +85,14 @@
self.runCallbacks('any', 'any', data);
},

cancelPoll: function() {
if(self.httpRequestCanceller) {
self.httpRequestCanceller.resolve('cancel');
}

self.disabled = true;
},

poll: function() {
// Skip polling is a poll is already running
if (self.streamId === null || self.isPolling === true) {
Expand All @@ -91,8 +102,13 @@
// Flag polling start
self.isPolling = true;

// Initiate stream canceller
self.httpRequestCanceller = $q.defer();

// Poll stream changes
$http.get('./api/stream/' + self.streamId).then(function(res) {
self.pollPromise = $http.get('./api/stream/' + self.streamId, {
timeout: self.httpRequestCanceller.promise
}).then(function(res) {
// Flag polling end
self.isPolling = false;

Expand All @@ -116,6 +132,10 @@
// Initialize the stream;
self.isPolling = false;

if(err && err.xhrStatus === 'abort') {
return;
}

if (err.status !== 404) {
NotificationSrv.error('StreamSrv', err.data, err.status);

Expand Down Expand Up @@ -160,9 +180,10 @@

var eventName = 'stream:' + config.rootId + '-' + config.objectType;
config.scope.$on(eventName, function(event, data) {
config.callback(data);
if(!self.disabled) {
config.callback(data);
}
});

}
};

Expand Down
2 changes: 1 addition & 1 deletion frontend/app/views/components/header.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
</li>
</ul>
</li>
<li ng-if="currentUser.organisations.length > 0">
<li ng-if="currentUser.organisations.length > 1">
<a href ng-click="switchOrg()">
<i class="fa fa-exchange"></i>
</a>
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/views/components/org/orgSwitch.modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h3 class="modal-title">Switch organisation</h3>
<a href ng-click="$dialog.selectOrg(item.organisation)" class="list-group-item"
ng-class="{'active': $dialog.currentUser.organisation === item.organisation}"
ng-repeat="item in $dialog.currentUser.organisations | filter:$modal.filter track by $index">
<h4 class="list-group-item-heading">{{item.organisation}}</h4>
<h4 class="list-group-item-heading">{{item.organisation}} <em class="pull-right" ng-if="$dialog.currentUser.organisation === item.organisation">(current)</em></h4>
<p class="list-group-item-text">Profile: <strong>{{item.role}}</strong></p>
</a>
</div>
Expand Down

0 comments on commit e130c3d

Please sign in to comment.