检查用户是否有权访问特定状态 [英] Check if user has permission to access specific states
问题描述
我使用 angular-permission 并且我想检查用户是否可以访问特定状态.
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上的讨论.
推荐答案
我在文档 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屋!