检查用户是否有权访问特定状态 [英] Check if user has permission to access specific states
问题描述
我使用角度许可,我想检查用户是否有权访问特定状态.
I use angular-permission and I want to check if user has access to specific states.
我的状态配置:
$stateProvider
.state('app', {
url: '/',
abstract: true,
data: {
permissions: {
only: ['BASE']
}
}
})
是否可以在控制器中使用一种方法或某种方法来确定它?
Is there a method or something which I can use in controllers to determine it?
// somewhere in controller
if (Permission.hasAccessToState('app')) {
// I want to check if user has access to state without navigating to it
};
如果有人感兴趣,这是在github上的相应讨论../p>
If anyone is interested, here is a corresponding discussion on github.
推荐答案
我在文档Wiki中找不到任何内容.但是根据消息来源和评论,似乎有一种Authorization
服务可供您利用.
I did not find something in the documentation wiki. But according to the source and comment there seems to be an Authorization
Service available which u can take advantage of.
//inject PermissionMap and Authorization
App.controller('Controller',function(PermissionMap,Authorization,$scope) {
//get the state by name an assign it to `state`
var permissionMap = new PermissionMap(state.data.permissions);
var authorizationResult = Authorization.authorize(permissionMap);
authorizationResult
.then(function () {
//authorized
})
.catch(function (rejectedPermission) {
//unauthorized
});
});
查看此代码,看看它是否有效
Check out this code and see if it works
更新
在您回复之后,我对代码进行了更深入的研究. 也许我们需要StatePermissionMap和StateAuthorization.
After ur reply, i looked deeper into the code. Maybe we need StatePermissionMap and StateAuthorization instead.
var statePermissionMap = new StatePermissionMap(state);
StateAuthorization
.authorize(statePermissionMap)
.then(function () {
//authorized
})
.catch(function (rejectedPermission) {
//unauthorized
})
这篇关于检查用户是否有权访问特定状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!