可以对特定的role进行授权,但需要修改一些代码。
将新字段access 添加到您的state 配置文件中,如下所示。让我们将authRequiredFor 数组存储在其中包含需要授权才能访问特定状态myState 的角色。
angular.module('myApp')
.config(function ($stateProvider) {
$stateProvider
.state('myState', {
url: '...',
templateUrl: '...',
controller: '...',
access: {
authRequiredFor: ['role1', 'role2']
}
});
});
在你的app.js文件的run()函数中,你需要添加和修改$stateChangeStart回调函数来检查用户在访问任何状态之前是否需要认证。
.run(function ($rootScope, $location, Auth, $state) {
// Redirect to login if route requires auth and you're not logged in
$rootScope.$on('$stateChangeStart', function (event, next) {
Auth.isLoggedInAsync(function(loggedIn) {
if (next.authenticate && !loggedIn) {
$location.url('/login');
}
if (next.access) { // check if the state config contains the field `access`
var permissions = next.access;
var userRole = Auth.getCurrentUser().role;
if (permissions.authRequiredFor) {
// check if the logged in user's role matches with the roles in the array
if (permissions.authRequiredFor.indexOf(userRole) >= 0) {
$location.url('/login'); // or redirect him to some other url/state
}
}
}
});
});
});
希望这能解决问题。