Skip to content

Commit

Permalink
#1403 Add a guard configuration to all the routes of the UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
nadouani committed Jun 24, 2020
1 parent 5d56951 commit 8ab9785
Showing 1 changed file with 93 additions and 16 deletions.
109 changes: 93 additions & 16 deletions frontend/app/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,20 @@ angular.module('thehive', [
},
templateUrl: 'views/partials/main/list.html',
controller: 'MainPageCtrl',
controllerAs: '$vm'
controllerAs: '$vm',
guard: {
isSuperAdmin: false
}
})
.state('app.cases', {
url: 'cases',
templateUrl: 'views/partials/case/case.list.html',
controller: 'CaseListCtrl',
controllerAs: '$vm',
title: 'Cases'
title: 'Cases',
guard: {
isSuperAdmin: false
}
})
.state('app.search', {
url: 'search?q',
Expand All @@ -157,6 +163,9 @@ angular.module('thehive', [

return defer.promise;
}
},
guard: {
isSuperAdmin: false
}
})
.state('app.settings', {
Expand Down Expand Up @@ -201,7 +210,9 @@ angular.module('thehive', [
return VersionSrv.get();
}
},
permissions: ['manageProfile']
guard: {
permissions: ['manageProfile']
}
})
.state('app.administration.organisations', {
url: '/organisations',
Expand All @@ -214,7 +225,9 @@ angular.module('thehive', [
return VersionSrv.get();
}
},
permissions: ['manageOrganisation']
guard: {
permissions: ['manageOrganisation']
}
})
.state('app.administration.organisations-details', {
url: '/organisations/{organisation}/details',
Expand All @@ -238,15 +251,16 @@ angular.module('thehive', [
return VersionSrv.get();
}
},
permissions: ['manageOrganisation', 'manageUser', 'manageCaseTemplate']
guard: {
permissions: ['manageOrganisation', 'manageUser', 'manageCaseTemplate']
}
})
.state('app.administration.analyzer-templates', {
url: '/analyzer-templates',
templateUrl: 'views/partials/admin/analyzer-templates.html',
controller: 'AdminAnalyzerTemplatesCtrl',
controllerAs: 'vm',
title: 'Analyzer templates administration',
permissions: ['manageAnalyzerTemplate'],
resolve: {
appConfig: function($q, VersionSrv) {
var defer = $q.defer();
Expand All @@ -263,6 +277,9 @@ angular.module('thehive', [

return defer.promise;
},
},
guard: {
permissions: ['manageAnalyzerTemplate']
}
})
.state('app.administration.custom-fields', {
Expand All @@ -271,7 +288,9 @@ angular.module('thehive', [
controller: 'AdminCustomFieldsCtrl',
controllerAs: '$vm',
title: 'Custom fields administration',
permissions: ['manageCustomField']
guard: {
permissions: ['manageCustomField']
}
})
.state('app.administration.observables', {
url: '/observables',
Expand All @@ -287,7 +306,9 @@ angular.module('thehive', [
});
}
},
permissions: ['manageObservableTemplate']
guard: {
permissions: ['manageObservableTemplate']
}
})
// .state('app.administration.ui-settings', {
// url: '/ui-settings',
Expand Down Expand Up @@ -326,6 +347,9 @@ angular.module('thehive', [

return deferred.promise;
}
},
guard: {
isSuperAdmin: false
}
})
.state('app.case.details', {
Expand All @@ -334,6 +358,9 @@ angular.module('thehive', [
controller: 'CaseDetailsCtrl',
data: {
tab: 'details'
},
guard: {
isSuperAdmin: false
}
})
.state('app.case.tasks', {
Expand All @@ -342,12 +369,18 @@ angular.module('thehive', [
controller: 'CaseTasksCtrl',
data: {
tab: 'tasks'
},
guard: {
isSuperAdmin: false
}
})
.state('app.case.links', {
url: '/links',
templateUrl: 'views/partials/case/case.links.html',
controller: 'CaseLinksCtrl'
controller: 'CaseLinksCtrl',
guard: {
isSuperAdmin: false
}
})
.state('app.case.sharing', {
url: '/sharing',
Expand Down Expand Up @@ -382,7 +415,10 @@ angular.module('thehive', [
});
}
},
permissions: ['manageShare']
guard: {
isSuperAdmin: false,
permissions: ['manageShare']
}
})
.state('app.case.alerts', {
url: '/alerts',
Expand All @@ -396,6 +432,9 @@ angular.module('thehive', [
}
}).$promise;
}
},
guard: {
isSuperAdmin: false
}
})
.state('app.case.tasks-item', {
Expand All @@ -417,6 +456,9 @@ angular.module('thehive', [

return deferred.promise;
}
},
guard: {
isSuperAdmin: false
}
})
.state('app.case.observables', {
Expand All @@ -425,6 +467,9 @@ angular.module('thehive', [
controller: 'CaseObservablesCtrl',
data: {
tab: 'observables'
},
guard: {
isSuperAdmin: false
}
})
.state('app.case.observables-item', {
Expand All @@ -449,19 +494,28 @@ angular.module('thehive', [

return deferred.promise;
}
},
guard: {
isSuperAdmin: false
}
})
.state('app.alert-list', {
url: 'alert/list',
templateUrl: 'views/partials/alert/list.html',
controller: 'AlertListCtrl',
controllerAs: '$vm'
controllerAs: '$vm',
guard: {
isSuperAdmin: false
}
})
.state('app.dashboards', {
url: 'dashboards',
templateUrl: 'views/partials/dashboard/list.html',
controller: 'DashboardsCtrl',
controllerAs: '$vm'
controllerAs: '$vm',
guard: {
isSuperAdmin: false
}
})
.state('app.dashboards-view', {
url: 'dashboards/{id}',
Expand Down Expand Up @@ -495,6 +549,9 @@ angular.module('thehive', [

return defer.promise;
}
},
guard: {
isSuperAdmin: false
}
});
})
Expand Down Expand Up @@ -596,24 +653,44 @@ angular.module('thehive', [
}
]);
})
.run(function($rootScope, $state, AuthenticationSrv) {
.run(function($rootScope, $state, $q, AuthenticationSrv) {
'use strict';
$rootScope.async = 0;

$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams) {
// Check rout permissions
if(toState.permissions && !AuthenticationSrv.hasPermission(toState.permissions)) {
// Handle route guards
$rootScope.$on('$stateChangeSuccess', function(event, toState/*, toParams*/) {

if(!toState.guard){
return;
}

// Try Permissions
if(toState.guard.permissions !== undefined) {
var permissions = toState.guard.permissions;

if(permissions && !AuthenticationSrv.hasPermission(permissions)) {
event.preventDefault();
$state.go('app.index');
}
}

// Try isSupperAdmin
if(toState.guard.isSuperAdmin !== undefined && AuthenticationSrv.isSuperAdmin() !== toState.guard.isSuperAdmin) {
event.preventDefault();
$state.go('app.index');
}
});

// Update page title based on the route
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams) {
if (_.isFunction(toState.title)) {
$rootScope.title = toState.title(toParams);
} else {
$rootScope.title = toState.title;
}
});

// Handle 401 errors when navigating to a route
$rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error){
if(error && error.status && error.status === 401) {
event.preventDefault();
Expand Down

0 comments on commit 8ab9785

Please sign in to comment.