There is nothing as such mentioned in the documentation, but here is how I achieved it.
Basically, I had user role in my $rootScope (as I fetched them from backend)
1) Define in all your state definitions, what all users have access to that state. Example:
.state('state1', {
url: '/state1',
views: {
content: {
templateUrl: 'state1-partial.html',
controller: 'StateOneController'
}
},
data: {
access:['Admin','Manager'] //only admin and manager can access this state
}
})
.state('state2', {
url: '/state2',
views: {
content: {
templateUrl: 'state2-partial.html',
controller: 'StateTwoController'
}
},
data: {
access:['Admin'] //only admin can access this state
}
})
2) Then in angular.module run function I access these when state change event occurs as:
Also I use a service isAuthorized here to validate if the user is authorized to access that state. If yes I navigate the user to that state else I throw an error.
angular.module('myApp').run(function($rootScope, $state,$stateParams,isAuthorized){
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState,fromParams) {
var isAccessRequired = toState.data.access;
var isAccessRequired = toState.data && toState.data.access;
//prevent default routing
if(isAccessRequired){
//I stored userRoles in $rootScope.userRole in an array when fetching from backend.
var hasAccess = isAuthorized($rootScope.userRole,toState.data.access);
if(!hasAccess){
event.preventDefault();
//user doesnt have access, show error and dont take him anywhere
}
}
});
});
3) In my service (isAuthorized):
(function(){
'use strict';
angular.module('myApp')
.service('isAuthorized', function() {
return function(role,access){
//logic here that will see if data.access is present in the user roles, if yes it will return true else return false
return flag;
}
});
})();